# Buckets.doc\_count \> hits.total

**URL:** <https://discuss.elastic.co/t/buckets-doc-count-hits-total/124015>\
**Category:** Elasticsearch\
**Created:** [March 15, 2018, 2:29am UTC](https://discuss.elastic.co/t/buckets-doc-count-hits-total/124015 "2018-03-15T02:29:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![weejim\_sc](https://avatars.discourse-cdn.com/v4/letter/w/ac8455/32.png) [@weejim\_sc](https://discuss.elastic.co/u/weejim_sc)\
**Post date:** [March 15, 2018, 2:29am UTC](https://discuss.elastic.co/t/buckets-doc-count-hits-total/124015/1 "2018-03-15T02:29:07Z")

</div>

hi,

I am searching by identifier and performing aggregation and am able to get a hits.total of 1.  
However the aggregation agg.buckets[0].doc\_count is giving a value of 2 (more than the number of hits).  
Is there a way to fix this such that the sum of doc\_count in the agg.buckets matches hits.total?

Request:  
{  
"size" : 10,  
"query" : {  
"function\_score" : {  
"query" : {  
"multi\_match" : {  
"query" : "10000",  
"fields" : [  
"\_all^1.0",  
"description^2.5",  
"identifier^3.0"  
],  
"type" : "phrase\_prefix",  
"operator" : "AND",  
"slop" : 0,  
"prefix\_length" : 1,  
"max\_expansions" : 20,  
"lenient" : false,  
"zero\_terms\_query" : "NONE",  
"boost" : 1.0  
}  
},  
"functions" : [  
{  
"filter" : {  
"match\_all" : {  
"boost" : 1.0  
}  
}  
}  
],  
"score\_mode" : "multiply",  
"max\_boost" : 3.4028235E38,  
"boost" : 1.0  
}  
},  
"post\_filter" : {  
"query\_string" : {  
"query" : "entityType:payment AND identifier:("xxx" AND "yyy")",  
"default\_field" : "identifier",  
"fields" : [],  
"use\_dis\_max" : false,  
"tie\_breaker" : 0.0,  
"default\_operator" : "and",  
"auto\_generate\_phrase\_queries" : false,  
"max\_determinized\_states" : 100,  
"enable\_position\_increments" : true,  
"fuzziness" : "0",  
"fuzzy\_prefix\_length" : 0,  
"fuzzy\_max\_expansions" : 0,  
"phrase\_slop" : 0,  
"escape" : false,  
"split\_on\_whitespace" : true,  
"all\_fields" : false,  
"boost" : 1.0  
}  
},  
"min\_score" : 1.0E-5,  
"\_source" : {  
"includes" : [],  
"excludes" : []  
},  
"sort" : [  
{  
"\_score" : {  
"order" : "desc"  
}  
},  
{  
"\_uid" : {  
"order" : "asc"  
}  
}  
],  
"aggregations" : {  
"agg" : {  
"terms" : {  
"field" : "entityType",  
"value\_type" : "string",  
"size" : 10,  
"min\_doc\_count" : 1,  
"shard\_min\_doc\_count" : 0,  
"show\_term\_doc\_count\_error" : true,  
"order" : [  
{  
"\_count" : "desc"  
},  
{  
"\_term" : "asc"  
}  
]  
}  
}  
},  
"highlight" : {  
"require\_field\_match" : false,  
"fields" : {  
"payload.\*" : { },  
"identifier" : { },  
"description" : { }  
}  
}  
}

Response:  
{  
"took": 1700,  
"timed\_out": false,  
"\_shards": {  
"total": 3,  
"successful": 3,  
"failed": 0  
},  
"hits": {  
"total": 1,  
"max\_score": null,  
"hits": [  
{  
"\_index": "xxx-payment\_1520842027004",  
"\_type": "payment",  
"\_id": "...",  
"\_score": 2.4415238,

```
			....
        }
		...
      }
    ]
  },
  "aggregations": {
    "agg": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "payment",
          "doc_count": 2,
          "doc_count_error_upper_bound": 0
        }
      ]
    }
  }
}
```

---

<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 20, 2018, 10:12am UTC](https://discuss.elastic.co/t/buckets-doc-count-hits-total/124015/2 "2018-03-20T10:12:55Z")

</div>

That is quite the request you have there. 🙂

What seems to be going on here is that you have a `post_filter` in this request. Post filters are applied after the aggregation has been executed. As a result, post filters only influence the hits and not the aggregation scope. So, the aggregation is run on a larger set of documents than just the hits. This would explain why you are seeing a higher doc\_count than the number of hits.

If this is not what you want, you could move the query\_string query from the post\_filter to the top-level query. You could for example wrap the function\_score query in a bool query, and add a filter clause to it with the query\_string query.

---

<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 17, 2018, 10:13am UTC](https://discuss.elastic.co/t/buckets-doc-count-hits-total/124015/3 "2018-04-17T10:13:06Z")

</div>

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