# Inconsistent results for the same query on an index with 0 replicas

**URL:** https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715
**Category:** Elasticsearch
**Created:** [December 15, 2020, 1:20pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715 "2020-12-15T13:20:10Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Marten](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Marten](https://discuss.elastic.co/u/Marten)
#### Post date: [December 15, 2020, 1:20pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/1 "2020-12-15T13:20:10Z")

</div>

Hi there,

I have a problem with inconsistent ranking of the results when submitting the same query multiple times.  
I'm using ES 5.6.8 on an index with 5 primary shards.  
I have set the number of replicas from 1 to 0 to avoid the "deleted documents" issue, but still have the same problem.

It seems that the score for the 1st document lowers with each subsequent search (same query).  
The score of the second hit stays the same.  
With each search, the score from the 1st hit drops until it is lower than the 2nd hit.  
When that happens, the ranking of the results changes and the 1st hit becomes the 2nd one.

During all this time, the data in the index didn't change.

Does anybody know what this can be and how I can fix it?

Thanks,

Marten

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [December 15, 2020, 11:19pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/2 "2020-12-15T23:19:01Z")

</div>

5.X is [EOL](https://www.elastic.co/support/eol), please upgrade ASAP 🙂

Can you show some examples of the query and the responses with changing scores?

---

<div class="post-metadata">

### Author: ![Marten](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Marten](https://discuss.elastic.co/u/Marten)
#### Post date: [January 8, 2021, 3:45pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/3 "2021-01-08T15:45:46Z")

</div>

Hi Mark,

Sorry for the delay.  
This is the query:  
{  
"query": ▿{  
"bool": ▿{  
"must": ▿[  
▿{  
"bool": ▿{  
"should": ▿[  
▿[  
▿{  
"multi\_match": ▿{  
"query": "promotie",  
"fields": ▿[  
"d\_author^1",  
"d\_body^1",  
"d\_snippetContent^1"  
]  
}  
}  
]  
]  
}  
}  
],  
"should": ▿[  
▿{  
"bool": ▿{  
"must": ▿{  
"match": ▿{  
"d\_content\_type": ▿{  
"query": "html",  
"boost": 1000  
}  
}  
}  
}  
},  
▿{  
"bool": ▿{  
"should": ▿[  
▿{  
"match\_phrase": ▿{  
"d\_author.keyword^100": "promotie"  
}  
},  
▿{  
"match\_phrase": ▿{  
"d\_body.keyword^100": "promotie"  
}  
},  
▿{  
"match\_phrase": ▿{  
"d\_snippetContent.keyword^100": "promotie"  
}  
}  
]  
}  
}  
],  
"must\_not": ,  
"filter": ▿[  
▿{  
"bool": ▿{  
"must": ▿[  
▿[  
▿{  
"term": ▿{  
"d\_allow\_token\_document.keyword": " **nosecurity**"  
}  
}  
]  
]  
}  
}  
]  
}  
},  
"\_source": ▿{  
"excludes": ▿[  
"d\_body",  
"d\_sublinks"  
]  
},  
"highlight": ▿{  
"require\_field\_match": false,  
"pre\_tags": ▿[  
""  
],  
"post\_tags": ▿[  
""  
],  
"fields": ▿{  
"d\_body": ▿{  
"type": "plain",  
"fragment\_size": 200,  
"number\_of\_fragments": 2,  
"highlight\_query": ▿{  
"match\_phrase": ▿{  
"d\_body": "promotie"  
}  
}  
},  
"d\_title": ▿{  
"type": "plain",  
"fragment\_size": 200,  
"number\_of\_fragments": 2,  
"highlight\_query": ▿{  
"match\_phrase": ▿{  
"d\_title": "promotie"  
}  
}  
}  
}  
},  
"suggest": ▿{  
"text": "promotie",  
"phrase\_suggestion": ▿{  
"phrase": ▿{  
"field": "d\_title.trigram",  
"direct\_generator": ▿[  
▿{  
"field": "d\_title.trigram",  
"suggest\_mode": "always",  
"prefix\_length": 0  
}  
]  
}  
}  
}  
}

(sorry for the layout)

---

<div class="post-metadata">

### Author: ![Marten](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Marten](https://discuss.elastic.co/u/Marten)
#### Post date: [January 8, 2021, 3:53pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/4 "2021-01-08T15:53:43Z")

</div>

Examples of the response:  
1st search:  
▿{  
"took": 37,  
"timed\_out": false,  
"\_shards": ▿{  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": ▿{  
"total": 6506,  
"max\_score": 194.98152,  
"hits": ▿[  
▿{  
"\_index": "rugsearchv22",  
"\_type": "rugsearchv22",  
"\_id": "[https://www.rug.nl/staff/s.s.m.peters/projects](https://www.rug.nl/staff/s.s.m.peters/projects)",  
"\_score": 194.98152,  
"\_source": ▿{ ....

2nd search:  
▿{  
"took": 33,  
"timed\_out": false,  
"\_shards": ▿{  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": ▿{  
"total": 6506,  
"max\_score": 194.77722,  
"hits": ▿[  
▿{  
"\_index": "rugsearchv22",  
"\_type": "rugsearchv22",  
"\_id": "[https://www.rug.nl/staff/s.s.m.peters/projects](https://www.rug.nl/staff/s.s.m.peters/projects)",  
"\_score": 194.77722,  
"\_source": ▿{ ....

3rd search:  
▿{  
"took": 69,  
"timed\_out": false,  
"\_shards": ▿{  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": ▿{  
"total": 6506,  
"max\_score": 194.68721,  
"hits": ▿[  
▿{  
"\_index": "rugsearchv22",  
"\_type": "rugsearchv22",  
"\_id": "[https://www.rug.nl/staff/s.s.m.peters/projects](https://www.rug.nl/staff/s.s.m.peters/projects)",  
"\_score": 194.68721,  
"\_source": ▿{ ....

4th search:  
▿{  
"took": 25,  
"timed\_out": false,  
"\_shards": ▿{  
"total": 5,  
"successful": 5,  
"skipped": 0,  
"failed": 0  
},  
"hits": ▿{  
"total": 6506,  
"max\_score": 194.64012,  
"hits": ▿[  
▿{  
"\_index": "rugsearchv22",  
"\_type": "rugsearchv22",  
"\_id": "[https://www.rug.nl/staff/s.s.m.peters/projects](https://www.rug.nl/staff/s.s.m.peters/projects)",  
"\_score": 194.64012,  
"\_source": ▿{ ....

At a certain moment the score of the 1st hit drops below the second hit and the results are shown in a different order.

Data hasn't changed in the meantime.

Best Regards,

Marten

---

<div class="post-metadata">

### Author: ![myspacebarisbroken](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@myspacebarisbroken](https://discuss.elastic.co/u/myspacebarisbroken)
#### Post date: [January 9, 2021, 6:41am UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/5 "2021-01-09T06:41:13Z")

</div>

Try running a explain query to get some details on why it's scoring it the way it is.

Something like this:

```auto
curl -XGET 'https://localhost:9200/rugsearchv22/rugsearchv22/<somedocid>/_explain?pretty=1' -d '  
{
   "query" : {
      "match" : {
         "title" : "life"
      }
   }
}

```

I'm not sure whats up in your **\_id** field, and why it's a URL, but this should help give some insight on why it's scoring that document the way it is.

Read more on scoring here: [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/recipes.html#consistent-scoring](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/recipes.html#consistent-scoring)

That link and the paragraph directly below it [https://www.elastic.co/guide/en/elasticsearch/reference/5.6/recipes.html#\_relevancy\_looks\_wrong](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/recipes.html#_relevancy_looks_wrong) explains what's happening.

To put it simply, scores are calculated according to each shards statistics. Which can change.

There are two fixes for this:

(_Not optimal_): Use only a single shard.  
(_Better Solution_) Use _[`dfs_query_then_fetch`]_ ([https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-search-type.html#dfs-query-then-fetch](https://www.elastic.co/guide/en/elasticsearch/reference/5.6/search-request-search-type.html#dfs-query-then-fetch))

This is because ES will perform an initial "trip" to involve all shards, gathering index statistics and what not relative to the query, then the node will merge those statistics and send a merged statistic alongside the request when asking each shard to perform the _query_ phase. That way, all the shards use a single global statistic, rather than their own, thus producing consistent scoring.

---

<div class="post-metadata">

### Author: ![warkolm](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/warkolm/32/39224_2.png) [@warkolm](https://discuss.elastic.co/u/warkolm)
#### Post date: [January 11, 2021, 12:00am UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/6 "2021-01-11T00:00:32Z")

</div>

Please format your code/logs/config using the `</>` button, or markdown style back ticks. It helps to make things easy to read which helps us help you 🙂

---

<div class="post-metadata">

### Author: ![Marten](https://avatars.discourse-cdn.com/v4/letter/m/4af34b/32.png) [@Marten](https://discuss.elastic.co/u/Marten)
#### Post date: [January 11, 2021, 4:28pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/7 "2021-01-11T16:28:00Z")

</div>

@warkolm, I tried that but I didn't get it right, will try again next time.  
@myspacebarisbroken, I didn't look at the explain but I will try that.  
The reason I didn't look at explain is that the data didn't change and I have zero replica's, so it's always accessing the same shards when issuing the same query.

I will try to find a reason in the explain 🤔

Thanks so far anyway.

Marten

---

<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: [February 8, 2021, 4:28pm UTC](https://discuss.elastic.co/t/inconsistent-results-for-the-same-query-on-an-index-with-0-replicas/258715/8 "2021-02-08T16:28:07Z")

</div>

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