Search in sources :

Example 1 with GetSourceRequest

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

the class RequestConverters method sourceRequest.

private static Request sourceRequest(GetSourceRequest getSourceRequest, String httpMethodName) {
    Params parameters = new Params();
    parameters.withPreference(getSourceRequest.preference());
    parameters.withRouting(getSourceRequest.routing());
    parameters.withRefresh(getSourceRequest.refresh());
    parameters.withRealtime(getSourceRequest.realtime());
    parameters.withFetchSourceContext(getSourceRequest.fetchSourceContext());
    String endpoint = endpoint(getSourceRequest.index(), "_source", getSourceRequest.id());
    Request request = new Request(httpMethodName, endpoint);
    request.addParameters(parameters.asMap());
    return request;
}
Also used : BulkRequest(org.opensearch.action.bulk.BulkRequest) WriteRequest(org.opensearch.action.support.WriteRequest) AbstractBulkByScrollRequest(org.opensearch.index.reindex.AbstractBulkByScrollRequest) GetStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteRequest(org.opensearch.action.delete.DeleteRequest) CountRequest(org.opensearch.client.core.CountRequest) TermVectorsRequest(org.opensearch.client.core.TermVectorsRequest) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) UpdateRequest(org.opensearch.action.update.UpdateRequest) SearchTemplateRequest(org.opensearch.script.mustache.SearchTemplateRequest) RankEvalRequest(org.opensearch.index.rankeval.RankEvalRequest) 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) DeleteStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.DeleteStoredScriptRequest) GetRequest(org.opensearch.action.get.GetRequest) MultiSearchTemplateRequest(org.opensearch.script.mustache.MultiSearchTemplateRequest) PutStoredScriptRequest(org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest) UpdateByQueryRequest(org.opensearch.index.reindex.UpdateByQueryRequest) MultiTermVectorsRequest(org.opensearch.client.core.MultiTermVectorsRequest) ClearScrollRequest(org.opensearch.action.search.ClearScrollRequest) DeleteByQueryRequest(org.opensearch.index.reindex.DeleteByQueryRequest) IndexRequest(org.opensearch.action.index.IndexRequest) ClusterHealthRequest(org.opensearch.action.admin.cluster.health.ClusterHealthRequest) ReindexRequest(org.opensearch.index.reindex.ReindexRequest) MultiGetRequest(org.opensearch.action.get.MultiGetRequest)

Example 2 with GetSourceRequest

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

the class RequestConvertersTests method doTestSourceExists.

private static void doTestSourceExists(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
    String index = randomAlphaOfLengthBetween(3, 10);
    String id = randomAlphaOfLengthBetween(3, 10);
    final GetSourceRequest getRequest = requestFunction.apply(index, id);
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        String preference = randomAlphaOfLengthBetween(3, 10);
        getRequest.preference(preference);
        expectedParams.put("preference", preference);
    }
    if (randomBoolean()) {
        String routing = randomAlphaOfLengthBetween(3, 10);
        getRequest.routing(routing);
        expectedParams.put("routing", routing);
    }
    if (randomBoolean()) {
        boolean realtime = randomBoolean();
        getRequest.realtime(realtime);
        if (realtime == false) {
            expectedParams.put("realtime", "false");
        }
    }
    if (randomBoolean()) {
        boolean refresh = randomBoolean();
        getRequest.refresh(refresh);
        if (refresh) {
            expectedParams.put("refresh", "true");
        }
    }
    Request request = RequestConverters.sourceExists(getRequest);
    assertEquals(HttpHead.METHOD_NAME, request.getMethod());
    assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertNull(request.getEntity());
}
Also used : HashMap(java.util.HashMap) 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) GetSourceRequest(org.opensearch.client.core.GetSourceRequest)

Example 3 with GetSourceRequest

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

the class CrudIT method testSourceExists.

public void testSourceExists() throws IOException {
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
    }
    IndexRequest index = new IndexRequest("index").id("id");
    index.source("{\"field1\":\"value1\",\"field2\":\"value2\"}", XContentType.JSON);
    index.setRefreshPolicy(RefreshPolicy.IMMEDIATE);
    highLevelClient().index(index, RequestOptions.DEFAULT);
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "id");
        assertTrue(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
    }
    {
        GetSourceRequest getRequest = new GetSourceRequest("index", "does_not_exist");
        assertFalse(execute(getRequest, highLevelClient()::existsSource, highLevelClient()::existsSourceAsync));
    }
}
Also used : GetSourceRequest(org.opensearch.client.core.GetSourceRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) IndexRequest(org.opensearch.action.index.IndexRequest)

Example 4 with GetSourceRequest

use of org.opensearch.client.core.GetSourceRequest 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 5 with GetSourceRequest

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

the class RequestConvertersTests method doTestGetSource.

private static void doTestGetSource(BiFunction<String, String, GetSourceRequest> requestFunction) throws IOException {
    String index = randomAlphaOfLengthBetween(3, 10);
    String id = randomAlphaOfLengthBetween(3, 10);
    final GetSourceRequest getRequest = requestFunction.apply(index, id);
    Map<String, String> expectedParams = new HashMap<>();
    if (randomBoolean()) {
        String preference = randomAlphaOfLengthBetween(3, 10);
        getRequest.preference(preference);
        expectedParams.put("preference", preference);
    }
    if (randomBoolean()) {
        String routing = randomAlphaOfLengthBetween(3, 10);
        getRequest.routing(routing);
        expectedParams.put("routing", routing);
    }
    if (randomBoolean()) {
        boolean realtime = randomBoolean();
        getRequest.realtime(realtime);
        if (realtime == false) {
            expectedParams.put("realtime", "false");
        }
    }
    if (randomBoolean()) {
        boolean refresh = randomBoolean();
        getRequest.refresh(refresh);
        if (refresh) {
            expectedParams.put("refresh", "true");
        }
    }
    Request request = RequestConverters.getSource(getRequest);
    assertEquals(HttpGet.METHOD_NAME, request.getMethod());
    assertEquals("/" + index + "/_source/" + id, request.getEndpoint());
    assertEquals(expectedParams, request.getParameters());
    assertNull(request.getEntity());
}
Also used : HashMap(java.util.HashMap) 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) GetSourceRequest(org.opensearch.client.core.GetSourceRequest)

Aggregations

IndexRequest (org.opensearch.action.index.IndexRequest)6 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)6 HashMap (java.util.HashMap)4 DocWriteRequest (org.opensearch.action.DocWriteRequest)4 BulkRequest (org.opensearch.action.bulk.BulkRequest)4 DeleteRequest (org.opensearch.action.delete.DeleteRequest)4 GetRequest (org.opensearch.action.get.GetRequest)4 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)4 WriteRequest (org.opensearch.action.support.WriteRequest)4 UpdateRequest (org.opensearch.action.update.UpdateRequest)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 GetStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.GetStoredScriptRequest)3 PutStoredScriptRequest (org.opensearch.action.admin.cluster.storedscripts.PutStoredScriptRequest)3 ExplainRequest (org.opensearch.action.explain.ExplainRequest)3 FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)3