Pillowchewer
Pillowchewer

Reputation: 3

Elasticsearch boost fields that are matched in their entirety

Let's say there's a field with 2 records, values are:

r1: "HELLO MISTER" r2: "HELLO MISTER PAUL"

If I were to query "HELLO MISTER" is there any way to boost a document based on the percentage of the field that matches? I know it already scores it higher by default, but I have multiple fields with different boosts, so I need to boost a document when it's field matches exactly (or at least percentage based, boosting docs where the terms match 50%+ of the field for example)

I've tried adding multiple queries to all fields with match_phrase, match_phrase_prefix, terms, etc. But I can't seem to find a way to boost a field that matches exactly over fields that match partially.

Upvotes: 0

Views: 24

Answers (1)

rabbitbr
rabbitbr

Reputation: 3271

I did this example, maybe help you started.

POST idx_test/_doc
{
  "name":"HELLO MISTER"
}

POST idx_test/_doc
{
  "name":"HELLO MISTER PAUL"
}

GET idx_test/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "name": "HELLO MISTER"
          }
        },
        {
          "term": {
            "name.keyword": {
              "value": "HELLO MISTER",
              "boost": 2
            }
          }
        }
      ]
    }
  }
}

Response

    "hits" : [
  {
    "_index" : "idx_test",
    "_type" : "_doc",
    "_id" : "XiH664YB1-vHHCnRzwwF",
    "_score" : 0.8630463,
    "_source" : {
      "name" : "HELLO MISTER"
    }
  },
  {
    "_index" : "idx_test",
    "_type" : "_doc",
    "_id" : "JBX664YB3mDFYoZj1MyG",
    "_score" : 0.5753642,
    "_source" : {
      "name" : "HELLO MISTER PAUL"
    }
  }
]

}

Upvotes: 0

Related Questions