Search in sources :

Example 1 with ValidateQueryResponse

use of org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse in project elasticsearch by elastic.

the class SimpleValidateQueryIT method assertExplanation.

private static void assertExplanation(QueryBuilder queryBuilder, Matcher<String> matcher, boolean withRewrite) {
    ValidateQueryResponse response = client().admin().indices().prepareValidateQuery("test").setTypes("type1").setQuery(queryBuilder).setExplain(true).setRewrite(withRewrite).execute().actionGet();
    assertThat(response.getQueryExplanation().size(), equalTo(1));
    assertThat(response.getQueryExplanation().get(0).getError(), nullValue());
    assertThat(response.getQueryExplanation().get(0).getExplanation(), matcher);
    assertThat(response.isValid(), equalTo(true));
}
Also used : ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse)

Example 2 with ValidateQueryResponse

use of org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse in project elasticsearch by elastic.

the class SimpleValidateQueryIT method testExplainNoQuery.

public void testExplainNoQuery() {
    createIndex("test");
    ensureGreen();
    ValidateQueryResponse validateQueryResponse = client().admin().indices().prepareValidateQuery().setExplain(true).get();
    assertThat(validateQueryResponse.isValid(), equalTo(true));
    assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getIndex(), equalTo("test"));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), equalTo("*:*"));
}
Also used : ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse)

Example 3 with ValidateQueryResponse

use of org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse in project elasticsearch by elastic.

the class SimpleValidateQueryIT method testExplainDateRangeInQueryString.

// Issue #3629
public void testExplainDateRangeInQueryString() {
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.number_of_shards", 1)));
    String aMonthAgo = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).minusMonths(1));
    String aMonthFromNow = ISODateTimeFormat.yearMonthDay().print(new DateTime(DateTimeZone.UTC).plusMonths(1));
    client().prepareIndex("test", "type", "1").setSource("past", aMonthAgo, "future", aMonthFromNow).get();
    refresh();
    ValidateQueryResponse response = client().admin().indices().prepareValidateQuery().setQuery(queryStringQuery("past:[now-2M/d TO now/d]")).setRewrite(true).get();
    assertNoFailures(response);
    assertThat(response.getQueryExplanation().size(), equalTo(1));
    assertThat(response.getQueryExplanation().get(0).getError(), nullValue());
    DateTime twoMonthsAgo = new DateTime(DateTimeZone.UTC).minusMonths(2).withTimeAtStartOfDay();
    DateTime now = new DateTime(DateTimeZone.UTC).plusDays(1).withTimeAtStartOfDay().minusMillis(1);
    assertThat(response.getQueryExplanation().get(0).getExplanation(), equalTo("past:[" + twoMonthsAgo.getMillis() + " TO " + now.getMillis() + "]"));
    assertThat(response.isValid(), equalTo(true));
}
Also used : ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) Matchers.containsString(org.hamcrest.Matchers.containsString) DateTime(org.joda.time.DateTime)

Example 4 with ValidateQueryResponse

use of org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse in project elasticsearch by elastic.

the class SimpleValidateQueryIT method testExplainMatchPhrasePrefix.

public void testExplainMatchPhrasePrefix() {
    assertAcked(prepareCreate("test").setSettings(Settings.builder().put(indexSettings()).put("index.analysis.filter.syns.type", "synonym").putArray("index.analysis.filter.syns.synonyms", "one,two").put("index.analysis.analyzer.syns.tokenizer", "standard").putArray("index.analysis.analyzer.syns.filter", "syns")).addMapping("test", "field", "type=text,analyzer=syns"));
    ensureGreen();
    ValidateQueryResponse validateQueryResponse = client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.matchPhrasePrefixQuery("field", "foo")).setExplain(true).get();
    assertThat(validateQueryResponse.isValid(), equalTo(true));
    assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), containsString("field:\"foo*\""));
    validateQueryResponse = client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.matchPhrasePrefixQuery("field", "foo bar")).setExplain(true).get();
    assertThat(validateQueryResponse.isValid(), equalTo(true));
    assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), containsString("field:\"foo bar*\""));
    // Stacked tokens
    validateQueryResponse = client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.matchPhrasePrefixQuery("field", "one bar")).setExplain(true).get();
    assertThat(validateQueryResponse.isValid(), equalTo(true));
    assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), containsString("field:\"(one two) bar*\""));
    validateQueryResponse = client().admin().indices().prepareValidateQuery("test").setQuery(QueryBuilders.matchPhrasePrefixQuery("field", "foo one")).setExplain(true).get();
    assertThat(validateQueryResponse.isValid(), equalTo(true));
    assertThat(validateQueryResponse.getQueryExplanation().size(), equalTo(1));
    assertThat(validateQueryResponse.getQueryExplanation().get(0).getExplanation(), containsString("field:\"foo (one* two*)\""));
}
Also used : ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse)

Example 5 with ValidateQueryResponse

use of org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse in project elasticsearch by elastic.

the class RestValidateQueryAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(final RestRequest request, final NodeClient client) throws IOException {
    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
    validateQueryRequest.explain(request.paramAsBoolean("explain", false));
    validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    validateQueryRequest.rewrite(request.paramAsBoolean("rewrite", false));
    Exception bodyParsingException = null;
    try {
        request.withContentOrSourceParamParserOrNull(parser -> {
            if (parser != null) {
                validateQueryRequest.query(RestActions.getQueryContent(parser));
            } else if (request.hasParam("q")) {
                validateQueryRequest.query(RestActions.urlParamsToQueryBuilder(request));
            }
        });
    } catch (Exception e) {
        bodyParsingException = e;
    }
    final Exception finalBodyParsingException = bodyParsingException;
    return channel -> {
        if (finalBodyParsingException != null) {
            if (finalBodyParsingException instanceof ParsingException) {
                handleException(validateQueryRequest, ((ParsingException) finalBodyParsingException).getDetailedMessage(), channel);
            } else {
                handleException(validateQueryRequest, finalBodyParsingException.getMessage(), channel);
            }
        } else {
            client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {

                @Override
                public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
                    builder.startObject();
                    builder.field(VALID_FIELD, response.isValid());
                    buildBroadcastShardsHeader(builder, request, response);
                    if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
                        builder.startArray(EXPLANATIONS_FIELD);
                        for (QueryExplanation explanation : response.getQueryExplanation()) {
                            builder.startObject();
                            if (explanation.getIndex() != null) {
                                builder.field(INDEX_FIELD, explanation.getIndex());
                            }
                            builder.field(VALID_FIELD, explanation.isValid());
                            if (explanation.getError() != null) {
                                builder.field(ERROR_FIELD, explanation.getError());
                            }
                            if (explanation.getExplanation() != null) {
                                builder.field(EXPLANATION_FIELD, explanation.getExplanation());
                            }
                            builder.endObject();
                        }
                        builder.endArray();
                    }
                    builder.endObject();
                    return new BytesRestResponse(OK, builder);
                }
            });
        }
    };
}
Also used : BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) GET(org.elasticsearch.rest.RestRequest.Method.GET) ParsingException(org.elasticsearch.common.ParsingException) RestResponse(org.elasticsearch.rest.RestResponse) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) RestController(org.elasticsearch.rest.RestController) Strings(org.elasticsearch.common.Strings) RestActions(org.elasticsearch.rest.action.RestActions) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) RestChannel(org.elasticsearch.rest.RestChannel) POST(org.elasticsearch.rest.RestRequest.Method.POST) Settings(org.elasticsearch.common.settings.Settings) RestActions.buildBroadcastShardsHeader(org.elasticsearch.rest.action.RestActions.buildBroadcastShardsHeader) IndicesOptions(org.elasticsearch.action.support.IndicesOptions) RestRequest(org.elasticsearch.rest.RestRequest) OK(org.elasticsearch.rest.RestStatus.OK) NodeClient(org.elasticsearch.client.node.NodeClient) QueryExplanation(org.elasticsearch.action.admin.indices.validate.query.QueryExplanation) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ValidateQueryResponse(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse) ParsingException(org.elasticsearch.common.ParsingException) BytesRestResponse(org.elasticsearch.rest.BytesRestResponse) ValidateQueryRequest(org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest) RestBuilderListener(org.elasticsearch.rest.action.RestBuilderListener) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) QueryExplanation(org.elasticsearch.action.admin.indices.validate.query.QueryExplanation)

Aggregations

ValidateQueryResponse (org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse)7 IOException (java.io.IOException)1 Alias (org.elasticsearch.action.admin.indices.alias.Alias)1 QueryExplanation (org.elasticsearch.action.admin.indices.validate.query.QueryExplanation)1 ValidateQueryRequest (org.elasticsearch.action.admin.indices.validate.query.ValidateQueryRequest)1 IndicesOptions (org.elasticsearch.action.support.IndicesOptions)1 Client (org.elasticsearch.client.Client)1 NodeClient (org.elasticsearch.client.node.NodeClient)1 ParsingException (org.elasticsearch.common.ParsingException)1 Strings (org.elasticsearch.common.Strings)1 Settings (org.elasticsearch.common.settings.Settings)1 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)1 BaseRestHandler (org.elasticsearch.rest.BaseRestHandler)1 BytesRestResponse (org.elasticsearch.rest.BytesRestResponse)1 RestChannel (org.elasticsearch.rest.RestChannel)1 RestController (org.elasticsearch.rest.RestController)1 RestRequest (org.elasticsearch.rest.RestRequest)1 GET (org.elasticsearch.rest.RestRequest.Method.GET)1 POST (org.elasticsearch.rest.RestRequest.Method.POST)1 RestResponse (org.elasticsearch.rest.RestResponse)1