use of org.opensearch.action.get.MultiGetItemResponse in project OpenSearch by opensearch-project.
the class BulkProcessorIT method assertMultiGetResponse.
private static void assertMultiGetResponse(MultiGetResponse multiGetResponse, int numDocs) {
assertThat(multiGetResponse.getResponses().length, equalTo(numDocs));
int i = 1;
for (MultiGetItemResponse multiGetItemResponse : multiGetResponse) {
assertThat(multiGetItemResponse.getIndex(), equalTo("test"));
assertThat(multiGetItemResponse.getId(), equalTo(Integer.toString(i++)));
}
}
use of org.opensearch.action.get.MultiGetItemResponse in project OpenSearch by opensearch-project.
the class BulkProcessorIT method assertMultiGetResponse.
private static void assertMultiGetResponse(MultiGetResponse multiGetResponse, int numDocs) {
assertThat(multiGetResponse.getResponses().length, equalTo(numDocs));
int i = 1;
for (MultiGetItemResponse multiGetItemResponse : multiGetResponse) {
assertThat(multiGetItemResponse.getIndex(), equalTo("test"));
assertThat(multiGetItemResponse.getId(), equalTo(Integer.toString(i++)));
}
}
use of org.opensearch.action.get.MultiGetItemResponse in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method unwrapAndAssertExample.
private MultiGetItemResponse unwrapAndAssertExample(MultiGetResponse response) {
assertThat(response.getResponses(), arrayWithSize(1));
MultiGetItemResponse item = response.getResponses()[0];
assertEquals("index", item.getIndex());
assertEquals("example_id", item.getId());
return item;
}
use of org.opensearch.action.get.MultiGetItemResponse in project OpenSearch by opensearch-project.
the class CRUDDocumentationIT method testMultiGet.
@SuppressWarnings("unused")
public void testMultiGet() throws Exception {
RestHighLevelClient client = highLevelClient();
{
Request createIndex = new Request("PUT", "/index");
createIndex.setJsonEntity("{\n" + " \"mappings\" : {\n" + " \"properties\" : {\n" + " \"foo\" : {\n" + " \"type\": \"text\",\n" + " \"store\": true\n" + " }\n" + " }\n" + " }\n" + "}");
Response response = client().performRequest(createIndex);
assertEquals(200, response.getStatusLine().getStatusCode());
}
Map<String, Object> source = new HashMap<>();
source.put("foo", "val1");
source.put("bar", "val2");
source.put("baz", "val3");
client.index(new IndexRequest("index").id("example_id").source(source).setRefreshPolicy(RefreshPolicy.IMMEDIATE), RequestOptions.DEFAULT);
{
// tag::multi-get-request
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item(// <1>
"index", // <2>
"example_id"));
// <3>
request.add(new MultiGetRequest.Item("index", "another_id"));
// end::multi-get-request
// Add a missing index so we can test it.
request.add(new MultiGetRequest.Item("missing_index", "id"));
// tag::multi-get-request-item-extras
request.add(new MultiGetRequest.Item("index", "with_routing").routing(// <1>
"some_routing"));
request.add(new MultiGetRequest.Item("index", "with_version").versionType(// <2>
VersionType.EXTERNAL).version(// <3>
10123L));
// end::multi-get-request-item-extras
// tag::multi-get-request-top-level-extras
// <1>
request.preference("some_preference");
// <2>
request.realtime(false);
// <3>
request.refresh(true);
// end::multi-get-request-top-level-extras
// tag::multi-get-execute
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
// end::multi-get-execute
// tag::multi-get-response
MultiGetItemResponse firstItem = response.getResponses()[0];
// <1>
assertNull(firstItem.getFailure());
// <2>
GetResponse firstGet = firstItem.getResponse();
String index = firstItem.getIndex();
String id = firstItem.getId();
if (firstGet.isExists()) {
long version = firstGet.getVersion();
// <3>
String sourceAsString = firstGet.getSourceAsString();
// <4>
Map<String, Object> sourceAsMap = firstGet.getSourceAsMap();
// <5>
byte[] sourceAsBytes = firstGet.getSourceAsBytes();
} else {
// <6>
}
// end::multi-get-response
assertTrue(firstGet.isExists());
assertEquals(source, firstGet.getSource());
MultiGetItemResponse missingIndexItem = response.getResponses()[2];
// tag::multi-get-indexnotfound
// <1>
assertNull(missingIndexItem.getResponse());
// <2>
Exception e = missingIndexItem.getFailure().getFailure();
// <3>
OpenSearchException ee = (OpenSearchException) e;
// TODO status is broken! fix in a followup
// assertEquals(RestStatus.NOT_FOUND, ee.status()); // <4>
assertThat(e.getMessage(), // <5>
containsString("reason=no such index [missing_index]"));
// end::multi-get-indexnotfound
ActionListener<MultiGetResponse> listener;
// tag::multi-get-execute-listener
listener = new ActionListener<MultiGetResponse>() {
@Override
public void onResponse(MultiGetResponse response) {
// <1>
}
@Override
public void onFailure(Exception e) {
// <2>
}
};
// end::multi-get-execute-listener
// Replace the empty listener by a blocking listener in test
final CountDownLatch latch = new CountDownLatch(1);
listener = new LatchedActionListener<>(listener, latch);
// tag::multi-get-execute-async
// <1>
client.mgetAsync(request, RequestOptions.DEFAULT, listener);
// end::multi-get-execute-async
assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-no-source
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
FetchSourceContext.DO_NOT_FETCH_SOURCE));
// end::multi-get-request-no-source
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertNull(item.getResponse().getSource());
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-source-include
String[] includes = new String[] { "foo", "*r" };
String[] excludes = Strings.EMPTY_ARRAY;
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
fetchSourceContext));
// end::multi-get-request-source-include
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertThat(item.getResponse().getSource(), hasEntry("foo", "val1"));
assertThat(item.getResponse().getSource(), hasEntry("bar", "val2"));
assertThat(item.getResponse().getSource(), not(hasKey("baz")));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-source-exclude
String[] includes = Strings.EMPTY_ARRAY;
String[] excludes = new String[] { "foo", "*r" };
FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes);
request.add(new MultiGetRequest.Item("index", "example_id").fetchSourceContext(// <1>
fetchSourceContext));
// end::multi-get-request-source-exclude
MultiGetItemResponse item = unwrapAndAssertExample(client.mget(request, RequestOptions.DEFAULT));
assertThat(item.getResponse().getSource(), not(hasKey("foo")));
assertThat(item.getResponse().getSource(), not(hasKey("bar")));
assertThat(item.getResponse().getSource(), hasEntry("baz", "val3"));
}
{
MultiGetRequest request = new MultiGetRequest();
// tag::multi-get-request-stored
request.add(new MultiGetRequest.Item("index", "example_id").storedFields(// <1>
"foo"));
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
MultiGetItemResponse item = response.getResponses()[0];
// <2>
String value = item.getResponse().getField("foo").getValue();
// end::multi-get-request-stored
assertNull(item.getResponse().getSource());
assertEquals("val1", value);
}
{
// tag::multi-get-conflict
MultiGetRequest request = new MultiGetRequest();
request.add(new MultiGetRequest.Item("index", "example_id").version(1000L));
MultiGetResponse response = client.mget(request, RequestOptions.DEFAULT);
MultiGetItemResponse item = response.getResponses()[0];
// <1>
assertNull(item.getResponse());
// <2>
Exception e = item.getFailure().getFailure();
// <3>
OpenSearchException ee = (OpenSearchException) e;
// TODO status is broken! fix in a followup
// assertEquals(RestStatus.CONFLICT, ee.status()); // <4>
assertThat(e.getMessage(), containsString("version conflict, current version [1] is " + // <5>
"different than the one provided [1000]"));
// end::multi-get-conflict
}
}
use of org.opensearch.action.get.MultiGetItemResponse in project OpenSearch by opensearch-project.
the class SimpleMgetIT method testThatSourceFilteringIsSupported.
@SuppressWarnings("unchecked")
public void testThatSourceFilteringIsSupported() throws Exception {
assertAcked(prepareCreate("test").addAlias(new Alias("alias")));
BytesReference sourceBytesRef = BytesReference.bytes(jsonBuilder().startObject().array("field", "1", "2").startObject("included").field("field", "should be seen").field("hidden_field", "should not be seen").endObject().field("excluded", "should not be seen").endObject());
for (int i = 0; i < 100; i++) {
client().prepareIndex("test").setId(Integer.toString(i)).setSource(sourceBytesRef, XContentType.JSON).get();
}
MultiGetRequestBuilder request = client().prepareMultiGet();
for (int i = 0; i < 100; i++) {
if (i % 2 == 0) {
request.add(new MultiGetRequest.Item(indexOrAlias(), Integer.toString(i)).fetchSourceContext(new FetchSourceContext(true, new String[] { "included" }, new String[] { "*.hidden_field" })));
} else {
request.add(new MultiGetRequest.Item(indexOrAlias(), Integer.toString(i)).fetchSourceContext(new FetchSourceContext(false)));
}
}
MultiGetResponse response = request.get();
assertThat(response.getResponses().length, equalTo(100));
for (int i = 0; i < 100; i++) {
MultiGetItemResponse responseItem = response.getResponses()[i];
assertThat(responseItem.getIndex(), equalTo("test"));
if (i % 2 == 0) {
Map<String, Object> source = responseItem.getResponse().getSourceAsMap();
assertThat(source.size(), equalTo(1));
assertThat(source, hasKey("included"));
assertThat(((Map<String, Object>) source.get("included")).size(), equalTo(1));
assertThat(((Map<String, Object>) source.get("included")), hasKey("field"));
} else {
assertThat(responseItem.getResponse().getSourceAsBytes(), nullValue());
}
}
}
Aggregations