2
votes

I have two documents with a field country which can contain repeated values, e.g.

Doc1:

country: [US, US, GB, US]

Doc2:

country: [US, GB]

I need a query that when looking for country:US will assign a higher score to Doc1 than Doc2 since US appears multiple times in the country field of Doc1, while it will assign the same score to the two documents when looking for country:GB as it appears the same number of times in both documents. Is this something achievable with Elasticsearch?

1
What is the type of country field in mapping?Sathishkumar Rakkiasamy

1 Answers

0
votes

If you are doing a simple match search on US

GET countryindex/_search
{
  "query": {
   "match": {
     "country": "US"
   }
  }
}

It will give more score to more frequency of elements so [US, US, GB, US] will get more score than "[US, GB]" If you will search for "GB" -->"[US, GB]" will get more score than [US, US, GB, US], since shorter field length gets more score.

If you want to give same score when number of matches is same , you need to give norms: false in your mapping.

{
  "properties": {
    "title": {
      "type": "text",
      "norms": false
    }
  }
}