# How to retrieve the maximum id for one type index effectively?

**URL:** <https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092>\
**Category:** Elasticsearch\
**Created:** [December 17, 2012, 2:29pm UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092 "2012-12-17T14:29:50Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![SuoNayi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suonayi/32/2569_2.png) [@SuoNayi](https://discuss.elastic.co/u/SuoNayi)\
**Post date:** [December 17, 2012, 2:29pm UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092/1 "2012-12-17T14:29:50Z")

</div>

Hi all,how to retrieve the maximum id for one type index effectively?  
What I only know is to search with sort on the id field but in production  
there are millions of documents in the index so this approach may suffer  
from the penalty on performance.  
Could someone share your best practice for this issue?

Thanks,  
SuoNayi

--

---

<div class="post-metadata">

**Author:** ![radu\_gheorghe](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/radu_gheorghe/32/556_2.png) [@radu\_gheorghe](https://discuss.elastic.co/u/radu_gheorghe)\
**Post date:** [December 17, 2012, 5:39pm UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092/2 "2012-12-17T17:39:42Z")

</div>

Hello,

If your \_id is a numeric value, you can use the Custom Score query, which  
should be faster:

> **[Elastic — The Search AI Company](https://www.elastic.co)**
>
> Power insights and outcomes with The Elastic Search AI Platform. See into your data and find answers that matter with enterprise solutions designed to help you accelerate time to insight. Try Elastic ...

Otherwise, I think you're stuck with sorting. Unless you specify the ID, ES  
assigns a random one for you. Therefore, sorting IDs is pretty much like  
sorting any other field.

## Best regards, Radu

[http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene

On Mon, Dec 17, 2012 at 4:29 PM, SuoNayi [suonayi@gmail.com](mailto:suonayi@gmail.com) wrote:

> Hi all,how to retrieve the maximum id for one type index effectively?  
> What I only know is to search with sort on the id field but in production  
> there are millions of documents in the index so this approach may suffer  
> from the penalty on performance.  
> Could someone share your best practice for this issue?
> 
> Thanks,  
> SuoNayi
> 
> --

--

---

<div class="post-metadata">

**Author:** ![otisg](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/otisg/32/492_2.png) [@otisg](https://discuss.elastic.co/u/otisg)\
**Post date:** [December 17, 2012, 9:33pm UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092/3 "2012-12-17T21:33:04Z")

</div>

Hi,

Or  
maybe [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/statistical-facet.html)?

Looks like SuoNayi is exploring Solr vs. ES?  
If so, here is how they compare when it comes to faceting:

> **[Solr vs Elasticsearch: Performance Differences & More - Sematext](https://sematext.com/blog/solr-vs-elasticsearch-differences/)**
>
> How to decide between Solr and Elasticsearch: Which one performs best, is faster, scales better & is easier to manage?

## Otis

ELASTICSEARCH Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)  
Search Analytics - [Cloud Monitoring Tools & Services | Sematext](http://sematext.com/search-analytics/index.html)

On Monday, December 17, 2012 12:39:42 PM UTC-5, Radu Gheorghe wrote:

> Hello,
> 
> If your \_id is a numeric value, you can use the Custom Score query, which  
> should be faster:
> 
> [Elastic — The Search AI Company | Elastic](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)
> 
> Otherwise, I think you're stuck with sorting. Unless you specify the ID,  
> ES assigns a random one for you. Therefore, sorting IDs is pretty much like  
> sorting any other field.
> 
> ## Best regards, Radu
> 
> [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> 
> On Mon, Dec 17, 2012 at 4:29 PM, SuoNayi \<[suo...@gmail.com](mailto:suo...@gmail.com) \<javascript:\>\>wrote:
> 
> > Hi all,how to retrieve the maximum id for one type index effectively?  
> > What I only know is to search with sort on the id field but in production  
> > there are millions of documents in the index so this approach may suffer  
> > from the penalty on performance.  
> > Could someone share your best practice for this issue?
> > 
> > Thanks,  
> > SuoNayi
> > 
> > --

--

---

<div class="post-metadata">

**Author:** ![SuoNayi](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/suonayi/32/2569_2.png) [@SuoNayi](https://discuss.elastic.co/u/SuoNayi)\
**Post date:** [December 18, 2012, 1:51am UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092/4 "2012-12-18T01:51:46Z")

</div>

thanks guys, I am doing a performance test on them and if there already  
exist some performance tests on them please tell me.  
I'm only measuring them by writing index performance and it's hard to do  
search index compare because there are many search approaches.  
What I'm really to do is a choice decision between solrcloud and es.

2012/12/18 Otis Gospodnetic [otis.gospodnetic@gmail.com](mailto:otis.gospodnetic@gmail.com)

> Hi,
> 
> Or maybe  
> [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/guide/reference/api/search/facets/statistical-facet.html)  
> ?
> 
> Looks like SuoNayi is exploring Solr vs. ES?  
> If so, here is how they compare when it comes to faceting:  
> [Solr vs Elasticsearch: Performance Differences & More - Sematext](http://blog.sematext.com/2012/10/30/solr-vs-elasticsearch-part-4-faceting/)
> 
> ## Otis
> 
> ELASTICSEARCH Performance Monitoring - [Sematext Monitoring | Infrastructure Monitoring Service](http://sematext.com/spm/index.html)  
> Search Analytics - [Cloud Monitoring Tools & Services | Sematext](http://sematext.com/search-analytics/index.html)
> 
> On Monday, December 17, 2012 12:39:42 PM UTC-5, Radu Gheorghe wrote:
> 
> > Hello,
> > 
> > If your \_id is a numeric value, you can use the Custom Score query, which  
> > should be faster:  
> > [Elasticsearch Platform — Find real-time answers at scale | Elastic](http://www.elasticsearch.org/**guide/reference/query-dsl/)\*\*  
> > custom-score-query.html[http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html](http://www.elasticsearch.org/guide/reference/query-dsl/custom-score-query.html)
> > 
> > Otherwise, I think you're stuck with sorting. Unless you specify the ID,  
> > ES assigns a random one for you. Therefore, sorting IDs is pretty much like  
> > sorting any other field.
> > 
> > ## Best regards, Radu
> > 
> > [http://sematext.com/](http://sematext.com/) -- Elasticsearch -- Solr -- Lucene
> > 
> > On Mon, Dec 17, 2012 at 4:29 PM, SuoNayi [suo...@gmail.com](mailto:suo...@gmail.com) wrote:
> > 
> > > Hi all,how to retrieve the maximum id for one type index effectively?  
> > > What I only know is to search with sort on the id field but in  
> > > production there are millions of documents in the index so this approach  
> > > may suffer from the penalty on performance.  
> > > Could someone share your best practice for this issue?
> > > 
> > > Thanks,  
> > > SuoNayi
> > > 
> > > --
> > 
> > --

--

---

<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, 2:59am UTC](https://discuss.elastic.co/t/how-to-retrieve-the-maximum-id-for-one-type-index-effectively/10092/5 "2017-07-06T02:59:31Z")

</div>


