Search in sources :

Example 1 with MultiGetItemResponse

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++)));
    }
}
Also used : MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse)

Example 2 with MultiGetItemResponse

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++)));
    }
}
Also used : MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse)

Example 3 with MultiGetItemResponse

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;
}
Also used : MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse)

Example 4 with MultiGetItemResponse

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
    }
}
Also used : MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) HashMap(java.util.HashMap) BulkRequest(org.opensearch.action.bulk.BulkRequest) Request(org.opensearch.client.Request) WriteRequest(org.opensearch.action.support.WriteRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) RethrottleRequest(org.opensearch.client.RethrottleRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) GetRequest(org.opensearch.action.get.GetRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) GetResponse(org.opensearch.action.get.GetResponse) OpenSearchException(org.opensearch.OpenSearchException) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) IndexResponse(org.opensearch.action.index.IndexResponse) BulkItemResponse(org.opensearch.action.bulk.BulkItemResponse) GetResponse(org.opensearch.action.get.GetResponse) MultiTermVectorsResponse(org.opensearch.client.core.MultiTermVectorsResponse) ReplicationResponse(org.opensearch.action.support.replication.ReplicationResponse) DocWriteResponse(org.opensearch.action.DocWriteResponse) Response(org.opensearch.client.Response) GetSourceResponse(org.opensearch.client.core.GetSourceResponse) UpdateResponse(org.opensearch.action.update.UpdateResponse) ListTasksResponse(org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse) TermVectorsResponse(org.opensearch.client.core.TermVectorsResponse) DeleteResponse(org.opensearch.action.delete.DeleteResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) BulkByScrollResponse(org.opensearch.index.reindex.BulkByScrollResponse) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) BulkResponse(org.opensearch.action.bulk.BulkResponse) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) LatchedActionListener(org.opensearch.action.LatchedActionListener) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) OpenSearchException(org.opensearch.OpenSearchException) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap)

Example 5 with MultiGetItemResponse

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());
        }
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) MultiGetResponse(org.opensearch.action.get.MultiGetResponse) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) MultiGetItemResponse(org.opensearch.action.get.MultiGetItemResponse) Alias(org.opensearch.action.admin.indices.alias.Alias) MultiGetRequestBuilder(org.opensearch.action.get.MultiGetRequestBuilder) Matchers.containsString(org.hamcrest.Matchers.containsString) Map(java.util.Map)

Aggregations

MultiGetItemResponse (org.opensearch.action.get.MultiGetItemResponse)5 Map (java.util.Map)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 MultiGetResponse (org.opensearch.action.get.MultiGetResponse)2 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)2 Collections.singletonMap (java.util.Collections.singletonMap)1 HashMap (java.util.HashMap)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 OpenSearchException (org.opensearch.OpenSearchException)1 ActionListener (org.opensearch.action.ActionListener)1 DocWriteRequest (org.opensearch.action.DocWriteRequest)1 DocWriteResponse (org.opensearch.action.DocWriteResponse)1 LatchedActionListener (org.opensearch.action.LatchedActionListener)1 ListTasksResponse (org.opensearch.action.admin.cluster.node.tasks.list.ListTasksResponse)1 Alias (org.opensearch.action.admin.indices.alias.Alias)1 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)1 BulkRequest (org.opensearch.action.bulk.BulkRequest)1 BulkResponse (org.opensearch.action.bulk.BulkResponse)1 DeleteRequest (org.opensearch.action.delete.DeleteRequest)1 DeleteResponse (org.opensearch.action.delete.DeleteResponse)1