# Accessing query from within painless script?

**URL:** <https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225>\
**Category:** Elasticsearch\
**Created:** [April 12, 2017, 9:09pm UTC](https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225 "2017-04-12T21:09:59Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![olalonde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalonde/32/17118_2.png) [@olalonde](https://discuss.elastic.co/u/olalonde)\
**Post date:** [April 12, 2017, 9:09pm UTC](https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225/1 "2017-04-12T21:09:59Z")

</div>

I have a query which looks like this:

```python
        query = {
                'from': start,
                'size': limit,
                'query': {
                    'function_score': {
                        'query': {
                            'match': {
                                'some_field': search,
                            },
                        },
                        'script_score': {
                            'script': {
                                'lang': 'painless',
                                'inline': 'freq_payloads_cosine',
                                'params': {
                                    'query_words': search,
                                }
                            }
                        }
                    }
                }
        }

```

And a `freq_payloads_cosine.painless` script. Right now, I'm repeating "search" in both the query and the script score. I was wondering if instead there was a way to access the query from within the painless script without passing it as a parameter explicitly. Preferably, I'd like to access the analyzed/tokenized representation of the query since "some\_field" has a custom analyzer. Is that possible?

Thanks.

---

<div class="post-metadata">

**Author:** ![nik9000](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/nik9000/32/44947_2.png) [@nik9000](https://discuss.elastic.co/u/nik9000)\
**Post date:** [April 12, 2017, 9:58pm UTC](https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225/2 "2017-04-12T21:58:46Z")

</div>

That isn't build at this point, no. It almost feels like you'd be better off writing a custom similarity rather than a script score. We've talked in the past about maybe making that a thing you can do with painless, but we haven't worked on it.

---

<div class="post-metadata">

**Author:** ![olalonde](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/olalonde/32/17118_2.png) [@olalonde](https://discuss.elastic.co/u/olalonde)\
**Post date:** [April 13, 2017, 3:52am UTC](https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225/3 "2017-04-13T03:52:18Z")

</div>

Ah ok, so write a similarity plugin in Java?

**Edit** : I ended up writing a Java plugin, thanks 🙂 I'm still repeating the query as a param to the script though.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [May 11, 2017, 4:06am UTC](https://discuss.elastic.co/t/accessing-query-from-within-painless-script/82225/4 "2017-05-11T04:06:29Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
