user1445470
user1445470

Reputation: 55

How to boost some (but not all) terms in elasticsearch query

I have a list of words that I would like to pass through elasticsearch:

wordlist = ['my', 'list', 'of', 'words']

What I want to do is boost 'my' more than than the other words. The field I'm searching through is called 'text'.

I've read the documentation which makes very little sense without an implemented example that I can actually run.

Here is what I think is close

{
          "query": {
            "custom_filters_score": {
              "query": { "match": { "_all": ' '.join(wordlist) } },
              "filters": [
                {
                  "filter": {
                    "term": {
                      "text": wordlist[0]
                    }
                  },
                  "boost": 2
                },
                {
                  "filter": {
                    "term": {
                      "text": wordlist[1]
                    }
                  },
                  "boost": 1.5
                }
              ],
            "score_mode" : "multiply"
            }
          }
        }

I've modeled this from this blog post but I can't seem to figure out what " ...the main query... " is meant to look like.

Upvotes: 1

Views: 140

Answers (1)

rebeling
rebeling

Reputation: 728

The blogpost is a good read but outdated - see the comments from 2015 "ES 1.0+ (ie 1.4, 1.6, 1.7) no longer support custom_script".

What about function_score query like this:

{
"query": {
    "function_score": {
        "query": {
            "match": {
                 "title": {      
                      "query": " ".join(wordlist),
                      "operator": "and"
                 }
            }
        },
        "functions": [
                {
                "filter": {"term": {"text": wordlist[0]}},
                "weight": "2.0"
                },
                {
                "filter": {"term": {"text": wordlist[1]}},
                "weight": "1.5"
                },
...

Upvotes: 1

Related Questions