# Get the last entry from each of multiple types

**URL:** <https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890>\
**Category:** Elasticsearch\
**Created:** [September 19, 2014, 8:29pm UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890 "2014-09-19T20:29:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jingzhao\_Ou](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@Jingzhao\_Ou](https://discuss.elastic.co/u/Jingzhao_Ou)\
**Post date:** [September 19, 2014, 8:29pm UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890/1 "2014-09-19T20:29:03Z")

</div>

Hi, all,

To get the last entry from two different types, I am doing

GET localhost:9200/index/type1  
{  
size: 1,  
sort: { id: 'desc' },  
}

GET localhost:9200/index/type2  
{  
size: 1,  
sort: { id: 'desc' },  
}

For more efficient queries over multiple types, I want to combine the two  
queries into one by doing

GET localhost:9200/index/type1,type2  
{  
size: 2,  
sort: { id: 'desc' },  
}

However, this only gives me the last two samples from either type1 or type  
2. If the last two samples both belong to type1, the combined query only  
returns two type1 samples, which is different from the two separate GET  
queries.

Can any one give me some hints how to get this done? I appreciate your  
help!

Best regards,  
Jingzhao

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/64c5323f-9a5c-4c79-9c4e-8e8cad50607b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/64c5323f-9a5c-4c79-9c4e-8e8cad50607b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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:** [September 19, 2014, 8:37pm UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890/2 "2014-09-19T20:37:14Z")

</div>

May be multi search could help in that case?  
[http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html#search-multi-search](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-multi-search.html#search-multi-search)

Or may be a terms aggregation on \_type field (you'll need to index it) and a top hits sub aggregation: [http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#search-aggregations-metrics-top-hits-aggregation](http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html#search-aggregations-metrics-top-hits-aggregation)

--  
David Pilato | Technical Advocate | [elasticsearch.com](http://elasticsearch.com)  
[david.pilato@elasticsearch.com](mailto:david.pilato@elasticsearch.com)  
@dadoonet | @elasticsearchfr | @scrutmydocs

Le 19 septembre 2014 à 22:29:07, Jingzhao Ou ([jingzhao.ou@gmail.com](mailto:jingzhao.ou@gmail.com)) a écrit:

Hi, all,

To get the last entry from two different types, I am doing

GET localhost:9200/index/type1  
{  
size: 1,  
sort: { id: 'desc' },  
}

GET localhost:9200/index/type2  
{  
size: 1,  
sort: { id: 'desc' },  
}

For more efficient queries over multiple types, I want to combine the two queries into one by doing

GET localhost:9200/index/type1,type2  
{  
size: 2,  
sort: { id: 'desc' },  
}

However, this only gives me the last two samples from either type1 or type 2. If the last two samples both belong to type1, the combined query only returns two type1 samples, which is different from the two separate GET queries.

Can any one give me some hints how to get this done? I appreciate your help!

Best regards,  
Jingzhao

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/64c5323f-9a5c-4c79-9c4e-8e8cad50607b%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/64c5323f-9a5c-4c79-9c4e-8e8cad50607b%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/etPan.541c93fa.41b71efb.23d3%40MacBook-Air-de-David.local](https://groups.google.com/d/msgid/elasticsearch/etPan.541c93fa.41b71efb.23d3%40MacBook-Air-de-David.local).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jingzhao\_Ou](https://avatars.discourse-cdn.com/v4/letter/j/eada6e/32.png) [@Jingzhao\_Ou](https://discuss.elastic.co/u/Jingzhao_Ou)\
**Post date:** [September 19, 2014, 11:51pm UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890/3 "2014-09-19T23:51:17Z")

</div>

Hi, David,

Thanks a lot for your prompt help. I got both approaches working, which is  
very exciting. I prefer the top\_hits aggregation approach. The msearch  
approach is not accepting normal JSON payloads, which makes things a bit  
harder for processing in Javascript.

My query payload is shown below. Works like a champ!

Best regards,  
Jingzhao

{  
"size": 0,  
"query": {  
"bool": {  
"should": [  
{ "term" : { "\_type": "0000000000000001" } },  
{ "term" : { "\_type": "0000000000000002" } }  
]  
}  
},  
"sort": {  
"id": "desc"  
},  
"aggs": {  
"top-tags": {  
"terms": {  
"field": "\_type"  
},  
"aggs": {  
"top\_tag\_hits": {  
"top\_hits": { "size" : 1 }  
}  
}  
}  
}  
}

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a9ab08a2-cfda-4a3a-a3e9-013527505c09%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a9ab08a2-cfda-4a3a-a3e9-013527505c09%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Roman\_Parkhunovsky](https://avatars.discourse-cdn.com/v4/letter/r/a6a055/32.png) [@Roman\_Parkhunovsky](https://discuss.elastic.co/u/Roman_Parkhunovsky)\
**Post date:** [March 23, 2015, 7:54am UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890/4 "2015-03-23T07:54:29Z")

</div>

Jingzhao Ou, do you state that you successfully combined msearch with the  
query containing aggregations (like in your example)? How should the query  
be constructed then to prevent "msearch approach is not accepting normal  
JSON payloads"? Could you please post a working msearch API call example?

Thanks,  
Roman

субота, 20 вересня 2014 р. 02:51:17 UTC+3 користувач Jingzhao Ou написав:

> Hi, David,
> 
> Thanks a lot for your prompt help. I got both approaches working, which is  
> very exciting. I prefer the top\_hits aggregation approach. The msearch  
> approach is not accepting normal JSON payloads, which makes things a bit  
> harder for processing in Javascript.
> 
> My query payload is shown below. Works like a champ!
> 
> Best regards,  
> Jingzhao
> 
> {  
> "size": 0,  
> "query": {  
> "bool": {  
> "should": [  
> { "term" : { "\_type": "0000000000000001" } },  
> { "term" : { "\_type": "0000000000000002" } }  
> ]  
> }  
> },  
> "sort": {  
> "id": "desc"  
> },  
> "aggs": {  
> "top-tags": {  
> "terms": {  
> "field": "\_type"  
> },  
> "aggs": {  
> "top\_tag\_hits": {  
> "top\_hits": { "size" : 1 }  
> }  
> }  
> }  
> }  
> }

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/a16ca0fc-d6cb-4b6f-8adb-06bb683c82f8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/a16ca0fc-d6cb-4b6f-8adb-06bb683c82f8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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 6, 2017, 12:24am UTC](https://discuss.elastic.co/t/get-the-last-entry-from-each-of-multiple-types/19890/5 "2017-07-06T00:24:58Z")

</div>


