# Return the query terms when total hits = 0

**URL:** <https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931>\
**Category:** Elasticsearch\
**Created:** [December 3, 2016, 8:06pm UTC](https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931 "2016-12-03T20:06:39Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![dcwdeveloper](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@dcwdeveloper](https://discuss.elastic.co/u/dcwdeveloper)\
**Post date:** [December 3, 2016, 8:06pm UTC](https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931/1 "2016-12-03T20:06:39Z")

</div>

I have the following document structure:

> ```
> {
> "date" : 1400500,
> "idc" : 1001,
> "name": "somebody",
> }
> 
> ```

I am performing an \_msearch query on a bunch of documents based on different values (the "idc" and a "date" range)  
When ES could not find any documents for the given date range it returns:

> ```
> "hits":{  
> "total":0,
> "max_score":null,
> "hits":[  
> ]
> }
> 
> ```

But, since there are N results, I cannot tell what "idc" and what "date" range was this result for.  
I would like the response to have the "searched" date range and "idc" when there are no results for the given query, for example, if I am searching documents for IDC = 1001 and date between 1400100 and 1400200, and there are no results found, the response could have the date-range somewhere, something like this:

> ```
> "hits":{  
> "total":0,
> "max_score":null,
> "query": {
> "date": {
> "gt": 1400100,
> "lte": 1400200,
> }
> "idc": 1001,
> }
> }
> 
> ```

That way I can tell what date range along with what IDC's has no results.

---

<div class="post-metadata">

**Author:** ![xavierfacq](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/xavierfacq/32/8744_2.png) [@xavierfacq](https://discuss.elastic.co/u/xavierfacq)\
**Post date:** [December 4, 2016, 5:50pm UTC](https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931/2 "2016-12-04T17:50:01Z")

</div>

Hi, Did you try with Aggregation like date histogramme or date range ? Maybe you can do something with date histogramme having a subaggregation with 0 as sum...

[https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html](https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-datehistogram-aggregation.html)

---

<div class="post-metadata">

**Author:** ![dcwdeveloper](https://avatars.discourse-cdn.com/v4/letter/d/eb8c5e/32.png) [@dcwdeveloper](https://discuss.elastic.co/u/dcwdeveloper)\
**Post date:** [December 4, 2016, 11:08pm UTC](https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931/3 "2016-12-04T23:08:11Z")

</div>

Thank you, however the real world test case is more complicated, I posted that minified example to make the point, I think I will work with the "response order" based on the "request order" of \_msearch. --\> in PHP with loops.

---

<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 1, 2017, 11:08pm UTC](https://discuss.elastic.co/t/return-the-query-terms-when-total-hits-0/67931/4 "2017-01-01T23:08:16Z")

</div>

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