g.annunziata
g.annunziata

Reputation: 3266

elasticsearch select which field use for boost

Given an elasticsearch document like this:


{
  "name": "bob",
  "title": "long text",
  "text": "long text bla bla...",
  "val_a1": 0.3,
  "val_a2": 0.7,
  "val_a3": 1.1,
  ...
  "val_az": 0.65
}

I need to make a search on Elastisearch with a given boost value on text field plus a boost value on document got from a named field val_xy.
In example, a search could be:
"long" with boost value on text: 2.0 and general boost val_a6

So if found "long" on text field I use a boost of 2.0, and using a boost value from field value val_a6.

How can I do this search on a java Elasticsearch client? It's possible?

Upvotes: 0

Views: 163

Answers (1)

Garry Welding
Garry Welding

Reputation: 3609

What you want is a function_score query. The documentation isn't the best and can be highly confusing. But using your example above you'd do something like the following:

"function_score": {
    "query": {
        "term": {
            "title": "long"
        }
    },
    "functions": [
        {
            "filter": {
                "term": {
                    "title": "long"
                }
            },
            "script_score": {
                "script": "_score*2.0*doc['val_a6'].value"
            }
        }
    ],
    "score_mode": "max",
    "boost_mode": "replace"
}

My eureka moment with function_score queries was figuring out you could do filters, including bool filters, within the "functions" part.

Upvotes: 2

Related Questions