Retrieving a document by its ID, JAVA API


(Harsh) #1

Hello,
I am a newbie to elasticsearch. I wanted to know how, in JAVA, can we
retrieve a document if we know its ID? Any help would be appreciated.


(David Pilato) #2

http://www.elasticsearch.org/guide/reference/java-api/get.html

HTH
David :wink:
Twitter : @dadoonet / @elasticsearchfr

Le 20 juin 2012 à 11:39, Harsh harsh.chhatwani@gmail.com a écrit :

Hello,
I am a newbie to elasticsearch. I wanted to know how, in JAVA, can we retrieve a document if we know its ID? Any help would be appreciated.


(rpsandiford) #3

To help out a little more, check the IdsQueryBuilder. So, something like this:

SearchRequestBuilder req = xxxx; // however you usually get your request builder...
IdsQueryBuilder qb = QueryBuilders.idsQuery(type);
qb.addIds(id1);
req.setSearchType(SearchType.QUERY_AND_FETCH)
.setQuery(qb)
.addField(fieldName);
SearchResponse resp = req.execute().actionGet();

Bob.


(system) #4