# Elasticquery

**URL:** https://discuss.elastic.co/t/elasticquery/168939
**Category:** Elasticsearch
**Created:** [February 19, 2019, 6:37am UTC](https://discuss.elastic.co/t/elasticquery/168939 "2019-02-19T06:37:04Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 6:37am UTC](https://discuss.elastic.co/t/elasticquery/168939/1 "2019-02-19T06:37:04Z")

</div>

I have few duplicate documents under a particular index . Is there any way to skip the duplicate entry through elastic query ?  
I have following documents under a index. Now I want a single document through elastic query based on status field. Means if we have 10 documents having "status: done" , it should return only single document (latestone)

```
{
_id: dxsafaf
status: done
pid: 123
}

{
_id: dadadfe
status: done
pid: 123
}

{
_id: qewqert
status:done
pid: 123
}
```

---

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 6:40am UTC](https://discuss.elastic.co/t/elasticquery/168939/2 "2019-02-19T06:40:55Z")

</div>

i am using the following query through python but it returns all the doucument

res = es.search(index='data-ver2-\*', size=5000,

body={"sort": [{"@timestamp": {"order": "asc"}}],"query": { "bool": {"must":[{"match\_all" :{}},{"match\_phrase": {"pid": { "query": 123}}}]}}},request\_timeout=60)

---

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 6:58am UTC](https://discuss.elastic.co/t/elasticquery/168939/3 "2019-02-19T06:58:03Z")

</div>

Could you pls let me know what extra function I need to add with this query and how ?

Thanks  
Niraj

---

<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: [February 19, 2019, 7:56am UTC](https://discuss.elastic.co/t/elasticquery/168939/5 "2019-02-19T07:56:11Z")

</div>

What would help to make the decision that `qewqert` is the "latest" one?  
There is nothing else to sort on.

---

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 7:58am UTC](https://discuss.elastic.co/t/elasticquery/168939/6 "2019-02-19T07:58:47Z")

</div>

@timestamp

```
{
'@timestamp': '2019-02-12T11:05:36.124Z'
_id: dxsafaf
status: done
pid: 123
}

{
'@timestamp': '2019-02-12T11:05:45.124Z'
_id: dadadfe
status: done
pid: 123
}

{
'@timestamp': '2019-02-12T11:05:52.124Z'
_id: qewqert
status:done
pid: 123
}
```

---

<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: [February 19, 2019, 8:37am UTC](https://discuss.elastic.co/t/elasticquery/168939/7 "2019-02-19T08:37:18Z")

</div>

Why about using `size: 1`?

---

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 9:06am UTC](https://discuss.elastic.co/t/elasticquery/168939/8 "2019-02-19T09:06:41Z")

</div>

Size =1 would be fine if there is only single status (done). But if we have some other status as well then this should not work , right ?  
Let me give the complete example here:

{  
'@timestamp': '2019-02-12T11:03:52.124Z'  
\_id: qewqert  
status:run  
pid: 123  
}  
{  
'@timestamp': '2019-02-12T11:04:36.124Z'  
\_id: dxsafah  
status: run  
pid: 123  
}  
{  
'@timestamp': '2019-02-12T11:05:52.124Z'  
\_id: qewwert  
status:done  
pid: 123  
}  
{  
'@timestamp': '2019-02-12T11:05:45.124Z'  
\_id: dadadfe  
status: done  
pid: 123  
}  
{  
'@timestamp': '2019-02-12T11:05:52.124Z'  
\_id: qewqert  
status:done  
pid: 123  
}

And I want only these two documents

```
 {
    '@timestamp': '2019-02-12T11:04:36.124Z'
    _id: dxsafah
    status: run
    pid: 123
    }
{
    '@timestamp': '2019-02-12T11:05:52.124Z'
    _id: qewqert
    status:done
    pid: 123
    }
```

---

<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: [February 19, 2019, 9:20am UTC](https://discuss.elastic.co/t/elasticquery/168939/9 "2019-02-19T09:20:01Z")

</div>

> [@niraj\_pandey](#):
>
> Let me give the complete example here:

That would help not spending time with wrong answers. Please do that next time.

It's always better to provide a full recreation script as described in [About the Elasticsearch category](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It helps to better understand what you are doing.

A full reproduction script will help readers to understand, reproduce and if needed fix your problem. It will also most likely help to get a faster answer.

Anyway, you can run a `terms` aggregation and then a `top_hits` agg with `size:1` in it.

---

<div class="post-metadata">

### Author: ![niraj\_pandey](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/niraj_pandey/32/44034_2.png) [@niraj\_pandey](https://discuss.elastic.co/u/niraj_pandey)
#### Post date: [February 19, 2019, 10:13am UTC](https://discuss.elastic.co/t/elasticquery/168939/10 "2019-02-19T10:13:54Z")

</div>

Thanks for you support. It works now.

---

<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: [March 19, 2019, 10:13am UTC](https://discuss.elastic.co/t/elasticquery/168939/11 "2019-03-19T10:13:58Z")

</div>

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