# How to return the most recent distinct doc by the query

**URL:** <https://discuss.elastic.co/t/how-to-return-the-most-recent-distinct-doc-by-the-query/32887>\
**Category:** Elasticsearch\
**Created:** [October 23, 2015, 8:42pm UTC](https://discuss.elastic.co/t/how-to-return-the-most-recent-distinct-doc-by-the-query/32887 "2015-10-23T20:42:24Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Tiannan\_Wang](https://avatars.discourse-cdn.com/v4/letter/t/45deac/32.png) [@Tiannan\_Wang](https://discuss.elastic.co/u/Tiannan_Wang)\
**Post date:** [October 23, 2015, 8:42pm UTC](https://discuss.elastic.co/t/how-to-return-the-most-recent-distinct-doc-by-the-query/32887/1 "2015-10-23T20:42:24Z")

</div>

I have a list of build info records under jenkins/build which contains jobname may be duplicated by different build time. I need to return the most recent distinct doc by the query.

here is my query right now:  
GET jenkins/build/\_search  
{  
"query": {  
"bool": {  
"should": [  
{ "match": { "pluginName": "cobertura" }},  
{ "match": {"pluginName": "clover" }},  
{ "match": { "pluginName": "jenkins-elasticsearch-zaphyr" }}  
]  
}  
},  
"sort":{ "BUILD\_ID": "desc" },

"size": 1,  
"aggs" : {  
"distinct\_rec" : {  
"terms" : { "field" : "jobName" }  
}  
}  
}

it returns :  
{  
"took": 592,  
"timed\_out": false,  
"\_shards": {  
"total": 5,  
"successful": 5,  
"failed": 0  
},  
"hits": {  
"total": 643,  
"max\_score": null,  
"hits": [  
{  
"\_index": "jenkins",  
"\_type": "build",  
"\_id": "AU-Fk10Pt-Hfa4CI2JWt",  
"\_score": null,  
"_source": {  
"number": 2527,  
"jobName": "FLDR\_DM/TRUNK/DM/DM\_CI",  
"result": "SUCCESS",  
"startTime": 1441053492048,  
"duration": 1572975,  
"environment": {  
"_": "_21693_/apps/jdk/jdk1.7.0/bin/java",  
"\_AST\_FEATURES": "UNIVERSE - ucb",  
"A\_\_z": ""\*SHLVL",  
"APP\_UTIL\_DATADIR": "/apps/SFCMData",  
"APP\_UTIL\_DIR": "/apps/util",  
"applicationName": "DATAMGT",  
"AWS\_ACCESS\_KEY\_ID": "AKIAJDLTPWWVTBCPFILQ",  
"AWS\_SECRET\_KEY": "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",  
"BUILD\_CAUSE": "SCMTRIGGER",  
"BUILD\_CAUSE\_SCMTRIGGER": "true",  
"BUILD\_DISPLAY\_NAME": "#2527",  
"BUILD\_ID": "20150831163812",  
.....  
"sort": [  
null  
]  
}  
]  
},  
"aggregations": {  
"distinct\_rec": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 1,  
"buckets": [  
{  
"key": "DIVER/DIVER\_Services/DIVER\_Services-ci",  
"doc\_count": 360  
},  
{  
"key": "FLDR\_DM/TRUNK/DM/DM\_CI",  
"doc\_count": 180  
},  
{  
"key": "FLDR\_OLA/FOLA\_APP/ola\_app\_ci",  
"doc\_count": 54  
},  
{  
"key": "SQD\_1",  
"doc\_count": 22  
},  
{  
"key": "SQD\_2",  
"doc\_count": 7  
},  
{  
"key": "FLDR\_DM/TRUNK/DM/Herd\_CI",  
"doc\_count": 6  
},  
{  
"key": "Ver1",  
"doc\_count": 6  
},  
{  
"key": "SQD\_1/default",  
"doc\_count": 3  
},  
{  
"key": "DIVER/DIVER\_Services/DIVER\_Services-daily",  
"doc\_count": 2  
},  
{  
"key": "FLDR\_DM/test\_dm\_carbom",  
"doc\_count": 2  
}  
]  
}  
}  
}

it aggregates the jobname but I also need to return with the property of the most recent one, I hope to return the most recent distinct records in hits part, can you help me with this? Thanks.

---

<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:** [July 5, 2017, 11:42pm UTC](https://discuss.elastic.co/t/how-to-return-the-most-recent-distinct-doc-by-the-query/32887/2 "2017-07-05T23:42:52Z")

</div>


