# Results have a similar score, but the number of fields matched varies

**URL:** <https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815>\
**Category:** Elasticsearch\
**Created:** [March 11, 2019, 5:08pm UTC](https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815 "2019-03-11T17:08:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![RMNYC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rmnyc/32/41785_2.png) [@RMNYC](https://discuss.elastic.co/u/RMNYC)\
**Post date:** [March 11, 2019, 5:08pm UTC](https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815/1 "2019-03-11T17:08:02Z")

</div>

To simplify:

```auto
PUT /test/vendors/1
{
  "type": "doctor",
  "name": "Ron",
  "place": "Boston"  
}

PUT /test/vendors/2
{
  "type": "doctor",
  "name": "Tom",
  "place": "Boston"  

}

PUT /test/vendors/3
{
  "type": "doctor",
  "name": "Jack",
  "place": "San Fran"  

}

```

then I search...

```auto
GET /test/_search
{
  "query": {
    "multi_match" : {
      "query": "doctor in Boston", 
      "fields": ["type", "place"] 
    }
  }
}

```

To simplify:

```auto
PUT /test/vendors/1
{
  "type": "doctor",
  "name": "Ron",
  "place": "Boston"  
}

PUT /test/vendors/2
{
  "type": "doctor",
  "name": "Tom",
  "place": "Boston"  

}

PUT /test/vendors/3
{
  "type": "doctor",
  "name": "Jack",
  "place": "San Fran"  

}

```

Then search:

```auto
GET /test/_search
{
  "query": {
    "multi_match" : {
      "query": "doctor in Boston", 
      "fields": ["type", "place"] 
    }
  }
}

```

I understand why I get `Jack` who works in San Fran -- it's because he's a `doctor` too. However, I can't figure out why the match score is the SAME for him. The other two were matched with the `place` too, weren't they? why aren't `Ron` and `Tom` scored higher?

```auto
{
  "took": 11,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 3,
    "max_score": 0.9245277,
    "hits": [
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "2",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Tom",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "1",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Ron",
          "place": "Boston"
        }
      },
      {
        "_index": "test",
        "_type": "vendors",
        "_id": "3",
        "_score": 0.9245277,
        "_source": {
          "type": "doctor",
          "name": "Jack",
          "place": "San Fran"
        }
      }
    ]
  }
}

```

Is there a way to force it to score less when less search keywords are found? Also, If I'n going to wrong way about this kind of search and there's a better pattern/way to do it -- I'd appreciate to be pointed in the right direction.

---

<div class="post-metadata">

**Author:** ![abdon](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/abdon/32/9195_2.png) [@abdon](https://discuss.elastic.co/u/abdon)\
**Post date:** [March 11, 2019, 6:47pm UTC](https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815/2 "2019-03-11T18:47:00Z")

</div>

There are a few things going on here. One of those things is that you're going with the default of 5 shards. With a small dataset like this (3 documents), this will distort the scoring. Scoring is done at the shard-level, and having 3 documents distributed over 5 shards will cause scoring to be inconsistent from one shard to another. I would recommend creating the index with only one shard before indexing the documents:

```auto
PUT test
{
  "settings": {
    "number_of_shards": 1
  }
}

```

This will already partially solve the issue, as the two doctors from Boston will now rank highest.

There is another thing going on though. The default score calculation mechanism of a multi\_match query is of the so called "[best\_fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-best-fields)" type. That means Elasticsearch will calculate a score for each of the individual fields "type" and "place", and whichever score is the highest will be the overall score of a document.

You can see how the score is calculated by adding `"explain": true` to a search request:

```auto
GET /test/_search
{
  "explain": true, 
  "query": {
    "multi_match" : {
      "query": "doctor in Boston", 
      "fields": ["type", "place"] 
    }
  }
}

```

Now, you'll be able to see the detailed breakdown of each document's score.

You can change the way the score is calculated from "best\_fields" into "[most\_fields](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-multi-match-query.html#type-most-fields)". Now, the more fields match your query, the higher the score is going to be (Elasticsearch will sum all of the individual scores of each of the fields).

```auto
GET /test/_search
{
  "query": {
    "multi_match" : {
      "query": "doctor in Boston", 
      "fields": ["type", "place"],
      "type": "most_fields"
    }
  }
}

```

This is probably what you will want to do.

---

<div class="post-metadata">

**Author:** ![RMNYC](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rmnyc/32/41785_2.png) [@RMNYC](https://discuss.elastic.co/u/RMNYC)\
**Post date:** [March 11, 2019, 6:59pm UTC](https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815/3 "2019-03-11T18:59:25Z")

</div>

"best\_fields" did the job. I appreciate you taking the time to explain about this option and the shards. Thanks!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [April 8, 2019, 7:04pm UTC](https://discuss.elastic.co/t/results-have-a-similar-score-but-the-number-of-fields-matched-varies/171815/4 "2019-04-08T19:04:40Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
