# Inconsistency in no of total hits in results

**URL:** <https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023>\
**Category:** Elasticsearch\
**Created:** [December 8, 2020, 4:00pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023 "2020-12-08T16:00:06Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Paresh\_Vaniya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paresh_vaniya/32/79807_2.png) [@Paresh\_Vaniya](https://discuss.elastic.co/u/Paresh_Vaniya)\
**Post date:** [December 8, 2020, 4:00pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/1 "2020-12-08T16:00:06Z")

</div>

Hi All,

I am using below query for one of the our use cases & we are observing weird thing on total hits count - means assume I hit the below request first time then total hits in result would be 64 and if I hit second time then it is 47, again if I hit third time then result is 64 & fourth time it would be 47.. and so on. So can anyone please help me how can we solve this issue.

{

```
"query": {
    "bool": {
        "filter": [
            {
                "match_phrase_prefix": {
                    "field1": {
                        "query": "D"
                    }
                }
            },
            {
                "match_phrase_prefix": {
                    "field2": {
                        "query": "66"
                    }
                }
            }
        ]
    }
},
"aggs": {
    "result": {
        "terms": {
            "field": "field1.keyword",
            "order": {
                "_term": "asc"
            },
            "size": 30
        }
    }
}

```

}

---

<div class="post-metadata">

**Author:** ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)\
**Post date:** [December 8, 2020, 4:11pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/2 "2020-12-08T16:11:10Z")

</div>

Which version are you running?

I saw something like this in the past with a very old version where I suffered from a split brain issue and the shards (primaries and replicas) were not really in sync. I had to rebuild my indices to fix that issue.

---

<div class="post-metadata">

**Author:** ![Paresh\_Vaniya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paresh_vaniya/32/79807_2.png) [@Paresh\_Vaniya](https://discuss.elastic.co/u/Paresh_Vaniya)\
**Post date:** [December 8, 2020, 4:16pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/4 "2020-12-08T16:16:39Z")

</div>

Very latest version - 7.x and I have rebuilt the indexes thrice but no luck.

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 8, 2020, 4:23pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/5 "2020-12-08T16:23:43Z")

</div>

> [@Paresh\_Vaniya](#):
>
> `match_phrase_prefix`

Could be because the query you are using is a somewhat random grab-bag of terms found in the index (see max expansions part of [the docs](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query-phrase-prefix.html)).

When you hit one replica it might not have merged out some terms from deleted docs in the index and so the set of 50 expansion terms used by one replica may differ to the set used by the other replica.  
If you try a more straight-forward query e.g a `match` query results should be more deterministic.

---

<div class="post-metadata">

**Author:** ![Paresh\_Vaniya](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/paresh_vaniya/32/79807_2.png) [@Paresh\_Vaniya](https://discuss.elastic.co/u/Paresh_Vaniya)\
**Post date:** [December 8, 2020, 4:28pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/6 "2020-12-08T16:28:20Z")

</div>

But as per my requirement I have to use Match Phrase Prefix query only so please suggest how can I solve this issue ?

---

<div class="post-metadata">

**Author:** ![Mark\_Harwood](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/mark_harwood/32/10538_2.png) [@Mark\_Harwood](https://discuss.elastic.co/u/Mark_Harwood)\
**Post date:** [December 8, 2020, 4:47pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/7 "2020-12-08T16:47:34Z")

</div>

Several options:

- Pass a routing preference in the request to ensure your client consistently visits only one of the replicas
- Force merge the indices down to 1 segment (only useful if you have no more additions/updates).
- Have no replicas (only useful if you don't like resiliency)
- Run a preliminary query to discover the terms you want to use in a follow-up query rather than leaving a replica's selections to chance.

---

<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:** [January 5, 2021, 4:47pm UTC](https://discuss.elastic.co/t/inconsistency-in-no-of-total-hits-in-results/258023/8 "2021-01-05T16:47:35Z")

</div>

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