Accessing query from within painless script?

I have a query which looks like this:

        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.

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.

Ah ok, so write a similarity plugin in Java?

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

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