OpenSearch/docs/java-api/docs/multi-get.asciidoc

31 lines
1.0 KiB
Plaintext

[[java-docs-multi-get]]
=== Multi Get API
The multi get API allows to get a list of documents based on their `index` and `id`:
[source,java]
--------------------------------------------------
MultiGetResponse multiGetItemResponses = client.prepareMultiGet()
.add("twitter", "_doc", "1") <1>
.add("twitter", "_doc", "2", "3", "4") <2>
.add("another", "_doc", "foo") <3>
.get();
for (MultiGetItemResponse itemResponse : multiGetItemResponses) { <4>
GetResponse response = itemResponse.getResponse();
if (response.isExists()) { <5>
String json = response.getSourceAsString(); <6>
}
}
--------------------------------------------------
<1> get by a single id
<2> or by a list of ids for the same index
<3> you can also get from another index
<4> iterate over the result set
<5> you can check if the document exists
<6> access to the `_source` field
For more information on the multi get operation, check out the REST
{ref}/docs-multi-get.html[multi get] docs.