# How to limit data in search hits based on condition

**URL:** https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450
**Category:** Elasticsearch
**Created:** [April 25, 2019, 12:04pm UTC](https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450 "2019-04-25T12:04:48Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)
#### Post date: [April 25, 2019, 12:04pm UTC](https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450/1 "2019-04-25T12:04:49Z")

</div>

Hi All,

I am working in winlogbeat and I am started receiving the data to elasticsearch but now I want to limit the search hits based on some condition. Don't know whether this can be achieved using es queries or not

below is the sample data I am getting in winlogbeat index,

> "hits": {  
> "total": 38234,  
> "max\_score": 2.5101082,  
> "hits": [  
> {  
> "\_index": "winlogbeat-2019.04",  
> "\_type": "doc",  
> "\_id": "wWOIIGoBRROkT6ZhE4N5",  
> "\_score": 2.5101082,  
> "\_source": {  
> "level": "Error"  
> }  
> },  
> {  
> "\_index": "winlogbeat-2019.04",  
> "\_type": "doc",  
> "\_id": "ymOPIGoBRROkT6Zhh4xK",  
> "\_score": 2.5101082,  
> "\_source": {  
> "level": "Error"  
> }  
> },  
> {  
> "\_index": "winlogbeat-2019.04",  
> "\_type": "doc",  
> "\_id": "GWOYIGoBRROkT6Zh8ZgE",  
> "\_score": 2.5101082,  
> "\_source": {  
> "level": "Error",  
> "message": """The Open Procedure for service "BITS" in DLL "D:\Windows\System32\bitsperf.dll" failed. Performance data for this service will not be available. The first four bytes (DWORD) of the Data section contains the error code."""  
> }  
> },

If you see the above documents there are 2 docs which doesn't have message field but now I want to limit hit results based on some condition like , If both level & message field has any values then it should show all the docs in the hits. If either one of them was not available in the doc then the document should not show in the result set.

Kindly guide us how this can be achieved.

Regards,  
Ganeshbabu R

---

<div class="post-metadata">

### Author: ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)
#### Post date: [April 25, 2019, 3:48pm UTC](https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450/2 "2019-04-25T15:48:02Z")

</div>

You can use the [Exists query](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html) to only include documents that have a specific field. Multiple Exists queries can be combined with Boolean queries, and you can use the `must_not` clause of a boolean to include documents that _don't_ have a field too.

---

<div class="post-metadata">

### Author: ![r.ganeshbabu](https://avatars.discourse-cdn.com/v4/letter/r/4da419/32.png) [@r.ganeshbabu](https://discuss.elastic.co/u/r.ganeshbabu)
#### Post date: [April 26, 2019, 6:25am UTC](https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450/3 "2019-04-26T06:25:20Z")

</div>

Thanks @polyfractal for sharing this and its worked..

---

<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: [May 24, 2019, 6:25am UTC](https://discuss.elastic.co/t/how-to-limit-data-in-search-hits-based-on-condition/178450/4 "2019-05-24T06:25:28Z")

</div>

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