Sharmila Gaddipati
Sharmila Gaddipati

Reputation: 15

How to boost elastic search results based on a field value

I am using Query String to query elastic search and below is my sample query.

{
    "query_string" : {
        "query" : "Sharmila"
    }
}

I've field called FLAG in the documents. I want to boost the results if the FLAG== Y. As custom_score is deprecated, I want to use function_score

{
"query": {
    "function_score": {
        "query": {
            "query_string": {
                "query": "Sharmila"
            },
            "functions": [{
                "script_score": {
                    "script": "_score * (doc['FLAG'].value == 'Y' ? 1.2 : 1)"
                }
            }],
        }
    }
}
}

I am getting exception saying No query registered for [script_score]]

I am using elasticsearch 1.2.2

Any thoughts where I am doing wrong?

Upvotes: 0

Views: 676

Answers (1)

Val
Val

Reputation: 217254

You're almost there, you just need to move the functions property just below the function_score one and not bury it inside your query:

{
"query": {
    "function_score": {
        "functions": [{
            "script_score": {
                "script": "_score * (doc['FLAG'].value == 'Y' ? 1.2 : 1)"
            }
        }],
        "query": {
            "query_string": {
                "query": "Sharmila"
            }
        }
    }
}
}

Upvotes: 1

Related Questions