ElasticSearch Jest client, how to return document id from hit?
Just fyi, was looking for same so took a look at Jest source code and saw that Jest does populate hit.source.es_metadata_id with the "_id" if it exists (see io.searchbox.core.SearchResult, line 115)
So could do something like following as alternative to annotation:
List<Hit<Map,Void>> hits = client.execute(search).getHits(Map.class)Hit hit = hits.get(0)Map source = (Map)hit.sourceString id = (String)source.get(JestResult.ES_METADATA_ID)
It seems no way to get _id
via Jest
API, have to get _id
from _source
object.
As @juliendangers mentioned, add @JestId
annotation can achieve:
public class MyClass { @JestId private String documentId; private String foo;}
And must explicitly set document id:myClass.setDocumentId("some_id");
Each Hit
object looks like:{"_index":"some_index","_type":"some_type","_id":"some_id","_score":2.609438,"_source":{"foo":"bar","documentId":"some_id"}}
(If not explicitly set document id, the "documentId":"some_id"
pair will be missing from Hit
)
While according to my test, if use inheritance, have to explicitly set parent class's document id:
public class ParentClass { @JestId private String documentId;}public class MyClass extends ParentClass { private String foo; public MyClass() { super.setDocumentId("some_id"); }}