1

We use AWS Elasticsearch, which support Painless scripting from version 5. I need an access to term positions/offsets in my custom score script.

In old Groovy scripting it worked:

"query": {
    "function_score": {
        "query": {"match_phrase": {"text": "life"} },
        "script_score": {
            "script": {
                "lang": "groovy",
                "inline": "termInfo=_index['text'].get('life', _POSITIONS);"
            }
        },
        "boost_mode": "multiply"
    }
}

But it does not work with Painless. It returns 'compile error'.

Andrii Skaliuk
  • 428
  • 1
  • 6
  • 14

1 Answers1

1

I hope this helps, I wanted scores based on the position of the query text in the document

{
    "query": {
        "bool": {
            "should": [
                {
                    "function_score": {
                        "query": {
                            "match_phrase_prefix": {
                                "field": "query"
                            }
                        },
                        "script_score": {
                            "script": {
                                "lang": "painless",
                                "source": "(params['_source']['field'].toLowerCase().indexOf('query'.toLowerCase())+1)"
                            }
                        },
                        "boost_mode": "max"
                    }
                }
            ]
        }
    }
}
karthikcru
  • 67
  • 8