Search in sources :

Example 6 with MultiGetRequest

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");
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 7 with MultiGetRequest

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());
    }
}
Also used : MultiGetResponse(org.opensearch.action.get.MultiGetResponse) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 8 with MultiGetRequest

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

Example 9 with MultiGetRequest

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
    }
}
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 10 with MultiGetRequest

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());
}
Also used : RandomSearchRequestGenerator.randomSearchRequest(org.opensearch.search.RandomSearchRequestGenerator.randomSearchRequest) MasterNodeRequest(org.opensearch.action.support.master.MasterNodeRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) RatedRequest(org.opensearch.index.rankeval.RatedRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) MultiSearchRequest(org.opensearch.action.search.MultiSearchRequest) DocWriteRequest(org.opensearch.action.DocWriteRequest) SearchScrollRequest(org.opensearch.action.search.SearchScrollRequest) ExplainRequest(org.opensearch.action.explain.ExplainRequest) SearchRequest(org.opensearch.action.search.SearchRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest) BulkRequest(org.opensearch.action.bulk.BulkRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) ReplicationRequest(org.opensearch.action.support.replication.ReplicationRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) CountRequest(org.opensearch.client.core.CountRequest) BulkShardRequest(org.opensearch.action.bulk.BulkShardRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Aggregations

MultiGetRequest (org.opensearch.action.get.MultiGetRequest)17 IndexRequest (org.opensearch.action.index.IndexRequest)9 BulkRequest (org.opensearch.action.bulk.BulkRequest)8 CountDownLatch (java.util.concurrent.CountDownLatch)7 BulkProcessor (org.opensearch.action.bulk.BulkProcessor)6 GetRequest (org.opensearch.action.get.GetRequest)5 ByteSizeValue (org.opensearch.common.unit.ByteSizeValue)5 DocWriteRequest (org.opensearch.action.DocWriteRequest)4 BulkItemResponse (org.opensearch.action.bulk.BulkItemResponse)4 DeleteRequest (org.opensearch.action.delete.DeleteRequest)4 SearchRequest (org.opensearch.action.search.SearchRequest)4 WriteRequest (org.opensearch.action.support.WriteRequest)4 UpdateRequest (org.opensearch.action.update.UpdateRequest)4 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)4 MultiTermVectorsRequest (org.opensearch.client.core.MultiTermVectorsRequest)4 TermVectorsRequest (org.opensearch.client.core.TermVectorsRequest)4 DeleteByQueryRequest (org.opensearch.index.reindex.DeleteByQueryRequest)4 ReindexRequest (org.opensearch.index.reindex.ReindexRequest)4 UpdateByQueryRequest (org.opensearch.index.reindex.UpdateByQueryRequest)4 DeleteStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest)3