Its not 100% clear from your post if you are using autogenerated IDs (i.e. you are not specifying the id when you create the document) though are suspect you are not using auto generated ids. But if you are, the auto generated ids are not numeric so getting the max id would not make sense.
To find the max value of a field you can generally use the max aggregation, however the max aggregation only works on numeric fields and the
_id in Elasticsearch is interpreted as a String and stored as such (actually this is a bit of a lie as its stored in a more specialised way but it is closest to a String or
keyword type for the purposes of this explanation) so you can't do a max aggregation on the
However, if you are setting the
_id yourself you can always add your own
my_id field which is mapped as a long type and use that to find the max version and rely on your client application to always send documents where the
_id matches the
Why are you wanting to do this incrementing of the id before you index the next document? It is to simulate a counting sequential id? If so there are some things to consider:
- Making a request to find the max id requires a call to Elasticsearch and for Elasticsearch to scan through all id values for all documents in your index. Doing this on every index request will impact indexing performance, as well as causing a lot of requests to Elasticsearch which may impact the performance of your other queries too. If your application is the only application indexing documents into this index I would suggest that you keep a counter of the last doc id in you client application and increment it from there.
- If you are wanting to do this because you might have loads of client applications indexing documents independently then as well as the point above you could run into concurrency problems where client A gets the max id and goes to write the next document with the increased id, but before client A writes that new document, client B gets the max Id and will get the old max ID meaning that it will overwrite the document A is about to write. The only way to reliably avoid this is either to use auto-generated IDs (i.e. let Elasticsearch pick the id) or to use a field (or combination of fields) in your document which unique across all the documents you are going to write.
Hope that helps