Search in sources :

Example 1 with GetSourceResponse

use of org.opensearch.client.core.GetSourceResponse in project OpenSearch by opensearch-project.

the class CRUDDocumentationIT method testGetSource.

public void testGetSource() throws Exception {
    RestHighLevelClient client = highLevelClient();
    {
        Request createIndex = new Request("PUT", "/posts");
        createIndex.setJsonEntity("{\n" + "    \"mappings\" : {\n" + "        \"properties\" : {\n" + "            \"message\" : {\n" + "                \"type\": \"text\",\n" + "                \"store\": true\n" + "            }\n" + "        }\n" + "    }\n" + "}");
        Response response = client().performRequest(createIndex);
        assertEquals(200, response.getStatusLine().getStatusCode());
        IndexRequest indexRequest = new IndexRequest("posts").id("1").source("user", "foobar", "postDate", new Date(), "message", "trying out OpenSearch");
        IndexResponse indexResponse = client.index(indexRequest, RequestOptions.DEFAULT);
        assertEquals(DocWriteResponse.Result.CREATED, indexResponse.getResult());
    }
    // tag::get-source-request
    GetSourceRequest getSourceRequest = new GetSourceRequest(// <1>
    "posts", // <2>
    "1");
    // end::get-source-request
    // tag::get-source-request-optional
    // <2>
    String[] includes = Strings.EMPTY_ARRAY;
    String[] excludes = new String[] { "postDate" };
    getSourceRequest.fetchSourceContext(// <1>
    new FetchSourceContext(true, includes, excludes));
    // end::get-source-request-optional
    // tag::get-source-request-routing
    // <1>
    getSourceRequest.routing("routing");
    // end::get-source-request-routing
    // tag::get-source-request-preference
    // <1>
    getSourceRequest.preference("preference");
    // end::get-source-request-preference
    // tag::get-source-request-realtime
    // <1>
    getSourceRequest.realtime(false);
    // end::get-source-request-realtime
    // tag::get-source-request-refresh
    // <1>
    getSourceRequest.refresh(true);
    // end::get-source-request-refresh
    {
        // tag::get-source-execute
        GetSourceResponse response = client.getSource(getSourceRequest, RequestOptions.DEFAULT);
        // end::get-source-execute
        // tag::get-source-response
        Map<String, Object> source = response.getSource();
        // end::get-source-response
        Map<String, Object> expectSource = new HashMap<>();
        expectSource.put("user", "foobar");
        expectSource.put("message", "trying out OpenSearch");
        assertEquals(expectSource, source);
    }
    {
        GetSourceRequest request = new GetSourceRequest("posts", "1");
        // tag::get-source-execute-listener
        ActionListener<GetSourceResponse> listener = new ActionListener<GetSourceResponse>() {

            @Override
            public void onResponse(GetSourceResponse getResponse) {
            // <1>
            }

            @Override
            public void onFailure(Exception e) {
            // <2>
            }
        };
        // end::get-source-execute-listener
        // Replace the empty listener by a blocking listener in test
        final CountDownLatch latch = new CountDownLatch(1);
        listener = new LatchedActionListener<>(listener, latch);
        // tag::get-source-execute-async
        // <1>
        client.getSourceAsync(request, RequestOptions.DEFAULT, listener);
        // end::get-source-execute-async
        assertTrue(latch.await(30L, TimeUnit.SECONDS));
    }
}
Also used : 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) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) CountDownLatch(java.util.concurrent.CountDownLatch) Date(java.util.Date) OpenSearchException(org.opensearch.OpenSearchException) 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) LatchedActionListener(org.opensearch.action.LatchedActionListener) FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) IndexResponse(org.opensearch.action.index.IndexResponse) CreateIndexResponse(org.opensearch.client.indices.CreateIndexResponse) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) GetSourceResponse(org.opensearch.client.core.GetSourceResponse)

Example 2 with GetSourceResponse

use of org.opensearch.client.core.GetSourceResponse in project OpenSearch by opensearch-project.

the class CrudIT method testGetSource.

public void testGetSource() throws IOException {
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("OpenSearch exception [type=index_not_found_exception, reason=no such index [index]]", exception.getMessage());
        assertEquals("index", exception.getMetadata("opensearch.index").get(0));
    }
    IndexRequest index = new IndexRequest("index").id("id");
    String document = "{\"field1\":\"value1\",\"field2\":\"value2\"}";
    index.source(document, XContentType.JSON);
    index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    highLevelClient().index(index, RequestOptions.DEFAULT);
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
        Map<String, Object> expectedResponse = new HashMap<>();
        expectedResponse.put("field1", "value1");
        expectedResponse.put("field2", "value2");
        assertEquals(expectedResponse, response.getSource());
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
        OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
        assertEquals(RestStatus.NOT_FOUND, exception.status());
        assertEquals("OpenSearch exception [type=resource_not_found_exception, " + "reason=Document not found [index]/[does_not_exist]]", exception.getMessage());
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, Strings.EMPTY_ARRAY));
        GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
        Map<String, Object> expectedResponse = new HashMap<>();
        expectedResponse.put("field1", "value1");
        expectedResponse.put("field2", "value2");
        assertEquals(expectedResponse, response.getSource());
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(true, new String[] { "field1" }, Strings.EMPTY_ARRAY));
        GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
        Map<String, Object> expectedResponse = new HashMap<>();
        expectedResponse.put("field1", "value1");
        assertEquals(expectedResponse, response.getSource());
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(true, Strings.EMPTY_ARRAY, new String[] { "field1" }));
        GetSourceResponse response = execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync);
        Map<String, Object> expectedResponse = new HashMap<>();
        expectedResponse.put("field2", "value2");
        assertEquals(expectedResponse, response.getSource());
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        getRequest.fetchSourceContext(new FetchSourceContext(false));
        OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(getRequest, highLevelClient()::getSource, highLevelClient()::getSourceAsync));
        assertEquals("OpenSearch exception [type=action_request_validation_exception, " + "reason=Validation Failed: 1: fetching source can not be disabled;]", exception.getMessage());
    }
}
Also used : FetchSourceContext(org.opensearch.search.fetch.subphase.FetchSourceContext) OpenSearchException(org.opensearch.OpenSearchException) GetSourceRequest(org.opensearch.client.core.GetSourceRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) GetSourceResponse(org.opensearch.client.core.GetSourceResponse)

Aggregations

Collections.singletonMap (java.util.Collections.singletonMap)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 OpenSearchException (org.opensearch.OpenSearchException)2 IndexRequest (org.opensearch.action.index.IndexRequest)2 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)2 GetSourceResponse (org.opensearch.client.core.GetSourceResponse)2 FetchSourceContext (org.opensearch.search.fetch.subphase.FetchSourceContext)2 Date (java.util.Date)1 CountDownLatch (java.util.concurrent.CountDownLatch)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 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