Search in sources :

Example 1 with ValidateQueryRequest

use of org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConverters method validateQuery.

static Request validateQuery(ValidateQueryRequest validateQueryRequest) throws IOException {
    String[] indices = validateQueryRequest.indices() == null ? Strings.EMPTY_ARRAY : validateQueryRequest.indices();
    String endpoint = RequestConverters.endpoint(indices, "_validate/query");
    Request request = new Request(HttpGet.METHOD_NAME, endpoint);
    RequestConverters.Params params = new RequestConverters.Params();
    params.withIndicesOptions(validateQueryRequest.indicesOptions());
    params.putParam("explain", Boolean.toString(validateQueryRequest.explain()));
    params.putParam("all_shards", Boolean.toString(validateQueryRequest.allShards()));
    params.putParam("rewrite", Boolean.toString(validateQueryRequest.rewrite()));
    request.addParameters(params.asMap());
    request.setEntity(RequestConverters.createEntity(validateQueryRequest, RequestConverters.REQUEST_BODY_CONTENT_TYPE));
    return request;
}
Also used : UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest)

Example 2 with ValidateQueryRequest

use of org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testInvalidValidateQuery.

public void testInvalidValidateQuery() throws IOException {
    String index = "shakespeare";
    createIndex(index, Settings.EMPTY);
    Request postDoc = new Request(HttpPost.METHOD_NAME, "/" + index + "/_doc");
    postDoc.setJsonEntity("{" + "  \"type\": \"act\"," + "  \"line_id\": 1," + "  \"play_name\": \"Henry IV\"," + "  \"speech_number\": \"\"," + "  \"line_number\": \"\"," + "  \"speaker\": \"\"," + "  \"text_entry\": \"ACT I\"" + "}");
    assertOK(client().performRequest(postDoc));
    QueryBuilder builder = QueryBuilders.queryStringQuery("line_id:foo").lenient(false);
    ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder);
    request.explain(true);
    ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, highLevelClient().indices()::validateQueryAsync);
    assertFalse(response.isValid());
}
Also used : ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) WriteRequest(org.opensearch.action.support.WriteRequest) ComposableIndexTemplateExistRequest(org.opensearch.client.indices.ComposableIndexTemplateExistRequest) GetComposableIndexTemplateRequest(org.opensearch.client.indices.GetComposableIndexTemplateRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) SimulateIndexTemplateRequest(org.opensearch.client.indices.SimulateIndexTemplateRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) DataStreamsStatsRequest(org.opensearch.client.indices.DataStreamsStatsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) DeleteComposableIndexTemplateRequest(org.opensearch.client.indices.DeleteComposableIndexTemplateRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) PutComposableIndexTemplateRequest(org.opensearch.client.indices.PutComposableIndexTemplateRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) ResizeRequest(org.opensearch.action.admin.indices.shrink.ResizeRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) IndexRequest(org.opensearch.action.index.IndexRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) QueryBuilder(org.opensearch.index.query.QueryBuilder)

Example 3 with ValidateQueryRequest

use of org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest in project OpenSearch by opensearch-project.

the class IndicesClientIT method testValidateQuery.

public void testValidateQuery() throws IOException {
    String index = "some_index";
    createIndex(index, Settings.EMPTY);
    QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
    ValidateQueryRequest request = new ValidateQueryRequest(index).query(builder);
    request.explain(randomBoolean());
    ValidateQueryResponse response = execute(request, highLevelClient().indices()::validateQuery, highLevelClient().indices()::validateQueryAsync);
    assertTrue(response.isValid());
}
Also used : ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) Matchers.containsString(org.hamcrest.Matchers.containsString) QueryBuilder(org.opensearch.index.query.QueryBuilder)

Example 4 with ValidateQueryRequest

use of org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest in project OpenSearch by opensearch-project.

the class IndicesRequestConvertersTests method testValidateQuery.

public void testValidateQuery() throws Exception {
    String[] indices = OpenSearchTestCase.randomBoolean() ? null : RequestConvertersTests.randomIndicesNames(0, 5);
    ValidateQueryRequest validateQueryRequest;
    if (OpenSearchTestCase.randomBoolean()) {
        validateQueryRequest = new ValidateQueryRequest(indices);
    } else {
        validateQueryRequest = new ValidateQueryRequest();
        validateQueryRequest.indices(indices);
    }
    Map<String, String> expectedParams = new HashMap<>();
    RequestConvertersTests.setRandomIndicesOptions(validateQueryRequest::indicesOptions, validateQueryRequest::indicesOptions, expectedParams);
    validateQueryRequest.explain(OpenSearchTestCase.randomBoolean());
    validateQueryRequest.rewrite(OpenSearchTestCase.randomBoolean());
    validateQueryRequest.allShards(OpenSearchTestCase.randomBoolean());
    expectedParams.put("explain", Boolean.toString(validateQueryRequest.explain()));
    expectedParams.put("rewrite", Boolean.toString(validateQueryRequest.rewrite()));
    expectedParams.put("all_shards", Boolean.toString(validateQueryRequest.allShards()));
    Request request = IndicesRequestConverters.validateQuery(validateQueryRequest);
    StringJoiner endpoint = new StringJoiner("/", "/", "");
    if (indices != null && indices.length > 0) {
        endpoint.add(String.join(",", indices));
    }
    endpoint.add("_validate/query");
    Assert.assertThat(request.getEndpoint(), equalTo(endpoint.toString()));
    Assert.assertThat(request.getParameters(), equalTo(expectedParams));
    RequestConvertersTests.assertToXContentBody(validateQueryRequest, request.getEntity());
    Assert.assertThat(request.getMethod(), equalTo(HttpGet.METHOD_NAME));
}
Also used : HashMap(java.util.HashMap) UpdateSettingsRequest(org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest) RefreshRequest(org.opensearch.action.admin.indices.refresh.RefreshRequest) OpenIndexRequest(org.opensearch.action.admin.indices.open.OpenIndexRequest) ForceMergeRequest(org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest) GetDataStreamRequest(org.opensearch.client.indices.GetDataStreamRequest) AnalyzeRequest(org.opensearch.client.indices.AnalyzeRequest) DeleteIndexTemplateRequest(org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest) AcknowledgedRequest(org.opensearch.action.support.master.AcknowledgedRequest) PutMappingRequest(org.opensearch.client.indices.PutMappingRequest) PutIndexTemplateRequest(org.opensearch.client.indices.PutIndexTemplateRequest) DeleteDataStreamRequest(org.opensearch.client.indices.DeleteDataStreamRequest) ClearIndicesCacheRequest(org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest) RolloverRequest(org.opensearch.client.indices.rollover.RolloverRequest) CreateIndexRequest(org.opensearch.client.indices.CreateIndexRequest) FlushRequest(org.opensearch.action.admin.indices.flush.FlushRequest) GetIndexRequest(org.opensearch.client.indices.GetIndexRequest) DeleteAliasRequest(org.opensearch.client.indices.DeleteAliasRequest) GetFieldMappingsRequest(org.opensearch.client.indices.GetFieldMappingsRequest) GetSettingsRequest(org.opensearch.action.admin.indices.settings.get.GetSettingsRequest) DeleteIndexRequest(org.opensearch.action.admin.indices.delete.DeleteIndexRequest) ResizeRequest(org.opensearch.client.indices.ResizeRequest) CloseIndexRequest(org.opensearch.client.indices.CloseIndexRequest) GetIndexTemplatesRequest(org.opensearch.client.indices.GetIndexTemplatesRequest) GetMappingsRequest(org.opensearch.client.indices.GetMappingsRequest) GetAliasesRequest(org.opensearch.action.admin.indices.alias.get.GetAliasesRequest) CreateDataStreamRequest(org.opensearch.client.indices.CreateDataStreamRequest) IndicesAliasesRequest(org.opensearch.action.admin.indices.alias.IndicesAliasesRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) IndexTemplatesExistRequest(org.opensearch.client.indices.IndexTemplatesExistRequest) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) StringJoiner(java.util.StringJoiner)

Example 5 with ValidateQueryRequest

use of org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest in project OpenSearch by opensearch-project.

the class IndicesClientDocumentationIT method testValidateQuery.

@SuppressWarnings("unused")
public void testValidateQuery() throws IOException, InterruptedException {
    RestHighLevelClient client = highLevelClient();
    String index = "some_index";
    createIndex(index, Settings.EMPTY);
    // tag::indices-validate-query-request
    // <1>
    ValidateQueryRequest request = new ValidateQueryRequest(index);
    // end::indices-validate-query-request
    // tag::indices-validate-query-request-query
    QueryBuilder builder = QueryBuilders.boolQuery().must(QueryBuilders.queryStringQuery("*:*")).filter(QueryBuilders.termQuery("user", "foobar"));
    // <2>
    request.query(builder);
    // end::indices-validate-query-request-query
    // tag::indices-validate-query-request-explain
    // <1>
    request.explain(true);
    // end::indices-validate-query-request-explain
    // tag::indices-validate-query-request-allShards
    // <1>
    request.allShards(true);
    // end::indices-validate-query-request-allShards
    // tag::indices-validate-query-request-rewrite
    // <1>
    request.rewrite(true);
    // end::indices-validate-query-request-rewrite
    // tag::indices-validate-query-execute
    // <1>
    ValidateQueryResponse response = client.indices().validateQuery(request, RequestOptions.DEFAULT);
    // end::indices-validate-query-execute
    // tag::indices-validate-query-response
    // <1>
    boolean isValid = response.isValid();
    // <2>
    int totalShards = response.getTotalShards();
    // <3>
    int successfulShards = response.getSuccessfulShards();
    // <4>
    int failedShards = response.getFailedShards();
    if (failedShards > 0) {
        for (DefaultShardOperationFailedException failure : response.getShardFailures()) {
            // <5>
            // <6>
            String failedIndex = failure.index();
            // <7>
            int shardId = failure.shardId();
            // <8>
            String reason = failure.reason();
        }
    }
    for (QueryExplanation explanation : response.getQueryExplanation()) {
        // <9>
        // <10>
        String explanationIndex = explanation.getIndex();
        // <11>
        int shardId = explanation.getShard();
        // <12>
        String explanationString = explanation.getExplanation();
    }
    // end::indices-validate-query-response
    // tag::indices-validate-query-execute-listener
    ActionListener<ValidateQueryResponse> listener = new ActionListener<ValidateQueryResponse>() {

        @Override
        public void onResponse(ValidateQueryResponse validateQueryResponse) {
        // <1>
        }

        @Override
        public void onFailure(Exception e) {
        // <2>
        }
    };
    // end::indices-validate-query-execute-listener
    // Replace the empty listener by a blocking listener in test
    final CountDownLatch latch = new CountDownLatch(1);
    listener = new LatchedActionListener<>(listener, latch);
    // tag::indices-validate-query-execute-async
    // <1>
    client.indices().validateQueryAsync(request, RequestOptions.DEFAULT, listener);
    // end::indices-validate-query-execute-async
    assertTrue(latch.await(30L, TimeUnit.SECONDS));
}
Also used : ValidateQueryResponse(org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse) ValidateQueryRequest(org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest) RestHighLevelClient(org.opensearch.client.RestHighLevelClient) QueryBuilder(org.opensearch.index.query.QueryBuilder) CountDownLatch(java.util.concurrent.CountDownLatch) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) IOException(java.io.IOException) DefaultShardOperationFailedException(org.opensearch.action.support.DefaultShardOperationFailedException) OpenSearchException(org.opensearch.OpenSearchException) QueryExplanation(org.opensearch.action.admin.indices.validate.query.QueryExplanation) LatchedActionListener(org.opensearch.action.LatchedActionListener) ActionListener(org.opensearch.action.ActionListener)

Aggregations

ValidateQueryRequest (org.opensearch.action.admin.indices.validate.query.ValidateQueryRequest)8 ValidateQueryResponse (org.opensearch.action.admin.indices.validate.query.ValidateQueryResponse)4 IndicesAliasesRequest (org.opensearch.action.admin.indices.alias.IndicesAliasesRequest)3 GetAliasesRequest (org.opensearch.action.admin.indices.alias.get.GetAliasesRequest)3 ClearIndicesCacheRequest (org.opensearch.action.admin.indices.cache.clear.ClearIndicesCacheRequest)3 DeleteIndexRequest (org.opensearch.action.admin.indices.delete.DeleteIndexRequest)3 FlushRequest (org.opensearch.action.admin.indices.flush.FlushRequest)3 ForceMergeRequest (org.opensearch.action.admin.indices.forcemerge.ForceMergeRequest)3 OpenIndexRequest (org.opensearch.action.admin.indices.open.OpenIndexRequest)3 RefreshRequest (org.opensearch.action.admin.indices.refresh.RefreshRequest)3 GetSettingsRequest (org.opensearch.action.admin.indices.settings.get.GetSettingsRequest)3 UpdateSettingsRequest (org.opensearch.action.admin.indices.settings.put.UpdateSettingsRequest)3 DeleteIndexTemplateRequest (org.opensearch.action.admin.indices.template.delete.DeleteIndexTemplateRequest)3 AnalyzeRequest (org.opensearch.client.indices.AnalyzeRequest)3 CloseIndexRequest (org.opensearch.client.indices.CloseIndexRequest)3 CreateDataStreamRequest (org.opensearch.client.indices.CreateDataStreamRequest)3 CreateIndexRequest (org.opensearch.client.indices.CreateIndexRequest)3 DeleteAliasRequest (org.opensearch.client.indices.DeleteAliasRequest)3 DeleteDataStreamRequest (org.opensearch.client.indices.DeleteDataStreamRequest)3 GetDataStreamRequest (org.opensearch.client.indices.GetDataStreamRequest)3