use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.
the class CrudIT method testMultiGetWithIds.
public void testMultiGetWithIds() throws IOException {
BulkRequest bulk = new BulkRequest();
bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
bulk.add(new IndexRequest("index").id("id1").source("{\"field\":\"value1\"}", XContentType.JSON));
bulk.add(new IndexRequest("index").id("id2").source("{\"field\":\"value2\"}", XContentType.JSON));
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("index", "id1");
multiGetRequest.add("index", "id2");
}
use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.
the class CrudIT method testMultiGet.
public void testMultiGet() throws IOException {
{
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("index", "id1");
multiGetRequest.add("index", "id2");
MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
assertEquals(2, response.getResponses().length);
assertTrue(response.getResponses()[0].isFailed());
assertNull(response.getResponses()[0].getResponse());
assertEquals("id1", response.getResponses()[0].getFailure().getId());
assertEquals("index", response.getResponses()[0].getFailure().getIndex());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[0].getFailure().getFailure().getMessage());
assertTrue(response.getResponses()[1].isFailed());
assertNull(response.getResponses()[1].getResponse());
assertEquals("id2", response.getResponses()[1].getId());
assertEquals("index", response.getResponses()[1].getIndex());
assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", response.getResponses()[1].getFailure().getFailure().getMessage());
}
BulkRequest bulk = new BulkRequest();
bulk.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
IndexRequest index = new IndexRequest("index").id("id1");
index.source("{\"field\":\"value1\"}", XContentType.JSON);
bulk.add(index);
index = new IndexRequest("index").id("id2");
index.source("{\"field\":\"value2\"}", XContentType.JSON);
bulk.add(index);
highLevelClient().bulk(bulk, RequestOptions.DEFAULT);
{
MultiGetRequest multiGetRequest = new MultiGetRequest();
multiGetRequest.add("index", "id1");
multiGetRequest.add("index", "id2");
MultiGetResponse response = execute(multiGetRequest, highLevelClient()::mget, highLevelClient()::mgetAsync);
assertEquals(2, response.getResponses().length);
assertFalse(response.getResponses()[0].isFailed());
assertNull(response.getResponses()[0].getFailure());
assertEquals("id1", response.getResponses()[0].getId());
assertEquals("index", response.getResponses()[0].getIndex());
assertEquals(Collections.singletonMap("field", "value1"), response.getResponses()[0].getResponse().getSource());
assertFalse(response.getResponses()[1].isFailed());
assertNull(response.getResponses()[1].getFailure());
assertEquals("id2", response.getResponses()[1].getId());
assertEquals("index", response.getResponses()[1].getIndex());
assertEquals(Collections.singletonMap("field", "value2"), response.getResponses()[1].getResponse().getSource());
}
}
use of org.opensearch.action.get.MultiGetRequest in project OpenSearch by opensearch-project.
the class IndicesRequestIT method testMultiGet.
public void testMultiGet() {
String multiGetShardAction = MultiGetAction.NAME + "[shard][s]";
interceptTransportActions(multiGetShardAction);
List<String> indices = new ArrayList<>();
MultiGetRequest multiGetRequest = new MultiGetRequest();
int numDocs = iterations(1, 30);
for (int i = 0; i < numDocs; i++) {
String indexOrAlias = randomIndexOrAlias();
multiGetRequest.add(indexOrAlias, Integer.toString(i));
indices.add(indexOrAlias);
}
internalCluster().coordOnlyNodeClient().multiGet(multiGetRequest).actionGet();
clearInterceptedActions();
assertIndicesSubset(indices, multiGetShardAction);
}
use of org.opensearch.action.get.MultiGetRequest 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.MultiGetRequest in project OpenSearch by opensearch-project.
the class RequestConvertersTests method testMultiGetWithType.
public void testMultiGetWithType() throws IOException {
MultiGetRequest multiGetRequest = new MultiGetRequest();
MultiGetRequest.Item item = new MultiGetRequest.Item(randomAlphaOfLength(4), randomAlphaOfLength(4));
multiGetRequest.add(item);
Request request = RequestConverters.multiGet(multiGetRequest);
assertEquals(HttpPost.METHOD_NAME, request.getMethod());
assertEquals("/_mget", request.getEndpoint());
assertToXContentBody(multiGetRequest, request.getEntity());
}
Aggregations