Search in sources :

Example 1 with FieldCapabilitiesRequest

use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.

the class RequestConverters method fieldCaps.

static Request fieldCaps(FieldCapabilitiesRequest fieldCapabilitiesRequest) throws IOException {
    String methodName = fieldCapabilitiesRequest.indexFilter() != null ? HttpPost.METHOD_NAME : HttpGet.METHOD_NAME;
    Request request = new Request(methodName, endpoint(fieldCapabilitiesRequest.indices(), "_field_caps"));
    Params params = new Params();
    params.withFields(fieldCapabilitiesRequest.fields());
    params.withIndicesOptions(fieldCapabilitiesRequest.indicesOptions());
    request.addParameters(params.asMap());
    if (fieldCapabilitiesRequest.indexFilter() != null) {
        request.setEntity(createEntity(fieldCapabilitiesRequest, REQUEST_BODY_CONTENT_TYPE));
    }
    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 FieldCapabilitiesRequest

use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.

the class RequestConvertersTests method testFieldCaps.

public void testFieldCaps() throws IOException {
    // Create a random request.
    String[] indices = randomIndicesNames(0, 5);
    String[] fields = generateRandomStringArray(5, 10, false, false);
    FieldCapabilitiesRequest fieldCapabilitiesRequest = new FieldCapabilitiesRequest().indices(indices).fields(fields);
    Map<String, String> indicesOptionsParams = new HashMap<>();
    setRandomIndicesOptions(fieldCapabilitiesRequest::indicesOptions, fieldCapabilitiesRequest::indicesOptions, indicesOptionsParams);
    Request request = RequestConverters.fieldCaps(fieldCapabilitiesRequest);
    // Verify that the resulting REST request looks as expected.
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    String joinedIndices = String.join(",", indices);
    if (!joinedIndices.isEmpty()) {
        endpoint.add(joinedIndices);
    }
    endpoint.add("_field_caps");
    assertEquals(endpoint.toString(), request.getEndpoint());
    assertEquals(5, request.getParameters().size());
    // Note that we don't check the field param value explicitly, as field names are
    // passed through
    // a hash set before being added to the request, and can appear in a
    // non-deterministic order.
    assertThat(request.getParameters(), hasKey("fields"));
    String[] requestFields = Strings.splitStringByCommaToArray(request.getParameters().get("fields"));
    assertEquals(new HashSet<>(Arrays.asList(fields)), new HashSet<>(Arrays.asList(requestFields)));
    for (Map.Entry<String, String> param : indicesOptionsParams.entrySet()) {
        assertThat(request.getParameters(), hasEntry(param.getKey(), param.getValue()));
    }
    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) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest) Map(java.util.Map) HashMap(java.util.HashMap) Collections.singletonMap(java.util.Collections.singletonMap) Collections.emptyMap(java.util.Collections.emptyMap) StringJoiner(java.util.StringJoiner)

Example 3 with FieldCapabilitiesRequest

use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.

the class SearchIT method testFieldCapsWithNonExistentFields.

public void testFieldCapsWithNonExistentFields() throws IOException {
    FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("index2").fields("nonexistent");
    FieldCapabilitiesResponse response = execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync);
    assertTrue(response.get().isEmpty());
}
Also used : FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)

Example 4 with FieldCapabilitiesRequest

use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.

the class SearchIT method testFieldCapsWithNonExistentIndices.

public void testFieldCapsWithNonExistentIndices() {
    FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().indices("non-existent").fields("rating");
    OpenSearchException exception = expectThrows(OpenSearchException.class, () -> execute(request, highLevelClient()::fieldCaps, highLevelClient()::fieldCapsAsync));
    assertEquals(RestStatus.NOT_FOUND, exception.status());
}
Also used : OpenSearchException(org.opensearch.OpenSearchException) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)

Example 5 with FieldCapabilitiesRequest

use of org.opensearch.action.fieldcaps.FieldCapabilitiesRequest in project OpenSearch by opensearch-project.

the class SearchDocumentationIT method testFieldCaps.

public void testFieldCaps() throws Exception {
    indexSearchTestData();
    RestHighLevelClient client = highLevelClient();
    // tag::field-caps-request
    FieldCapabilitiesRequest request = new FieldCapabilitiesRequest().fields("user").indices("posts", "authors", "contributors");
    // end::field-caps-request
    // tag::field-caps-request-indicesOptions
    // <1>
    request.indicesOptions(IndicesOptions.lenientExpandOpen());
    // end::field-caps-request-indicesOptions
    // tag::field-caps-execute
    FieldCapabilitiesResponse response = client.fieldCaps(request, RequestOptions.DEFAULT);
    // end::field-caps-execute
    // tag::field-caps-response
    // <1>
    Map<String, FieldCapabilities> userResponse = response.getField("user");
    FieldCapabilities textCapabilities = userResponse.get("keyword");
    boolean isSearchable = textCapabilities.isSearchable();
    boolean isAggregatable = textCapabilities.isAggregatable();
    // <2>
    String[] indices = textCapabilities.indices();
    // <3>
    String[] nonSearchableIndices = textCapabilities.nonSearchableIndices();
    // <4>
    String[] nonAggregatableIndices = textCapabilities.nonAggregatableIndices();
    // end::field-caps-response
    assertThat(userResponse.keySet(), containsInAnyOrder("keyword", "text"));
    assertTrue(isSearchable);
    assertFalse(isAggregatable);
    assertArrayEquals(indices, new String[] { "authors", "contributors" });
    assertNull(nonSearchableIndices);
    assertArrayEquals(nonAggregatableIndices, new String[] { "authors" });
    // tag::field-caps-execute-listener
    ActionListener<FieldCapabilitiesResponse> listener = new ActionListener<FieldCapabilitiesResponse>() {

        @Override
        public void onResponse(FieldCapabilitiesResponse response) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::field-caps-execute-listener
    // Replace the empty listener by a blocking listener for tests.
    CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::field-caps-execute-async
    // <1>
    client.fieldCapsAsync(request, RequestOptions.DEFAULT, listener);
    // end::field-caps-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : FieldCapabilities(org.opensearch.action.fieldcaps.FieldCapabilities) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) Matchers.containsString(org.hamcrest.Matchers.containsString) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener) FieldCapabilitiesResponse(org.opensearch.action.fieldcaps.FieldCapabilitiesResponse) FieldCapabilitiesRequest(org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)

Aggregations

FieldCapabilitiesRequest (org.opensearch.action.fieldcaps.FieldCapabilitiesRequest)10 FieldCapabilitiesResponse (org.opensearch.action.fieldcaps.FieldCapabilitiesResponse)4 DocWriteRequest (org.opensearch.action.DocWriteRequest)3 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 BulkRequest (org.opensearch.action.bulk.BulkRequest)3 DeleteRequest (org.opensearch.action.delete.DeleteRequest)3 ExplainRequest (org.opensearch.action.explain.ExplainRequest)3 GetRequest (org.opensearch.action.get.GetRequest)3 MultiGetRequest (org.opensearch.action.get.MultiGetRequest)3 IndexRequest (org.opensearch.action.index.IndexRequest)3 ClearScrollRequest (org.opensearch.action.search.ClearScrollRequest)3 MultiSearchRequest (org.opensearch.action.search.MultiSearchRequest)3 SearchRequest (org.opensearch.action.search.SearchRequest)3 SearchScrollRequest (org.opensearch.action.search.SearchScrollRequest)3 WriteRequest (org.opensearch.action.support.WriteRequest)3 UpdateRequest (org.opensearch.action.update.UpdateRequest)3 CountRequest (org.opensearch.client.core.CountRequest)3 GetSourceRequest (org.opensearch.client.core.GetSourceRequest)3