org.elasticsearch.action.admin.indices.validate.query.QueryExplanation Java Examples

The following examples show how to use org.elasticsearch.action.admin.indices.validate.query.QueryExplanation. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SearchQueryServiceImpl.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
private boolean validateQuery(final QueryBuilder query) {
  checkNotNull(query);
  try {
    ValidateQueryRequestBuilder validateRequest = indicesAdminClient().prepareValidateQuery().setQuery(query);
    if (log.isDebugEnabled()) {
      validateRequest.setExplain(true);
    }
    ValidateQueryResponse validateQueryResponse = validateRequest.execute().actionGet();
    if (!validateQueryResponse.isValid()) {
      if (log.isDebugEnabled()) {
        Collection<String> explanations = Collections2.transform(validateQueryResponse.getQueryExplanation(),
            new Function<QueryExplanation, String>()
            {
              @Nullable
              @Override
              public String apply(final QueryExplanation input) {
                return input.getExplanation() != null ? input.getExplanation() : input.getError();
              }
            });
        log.debug("Invalid query explanation: {}", explanations);
      }
      throw new IllegalArgumentException("Invalid query");
    }
    return true;
  }
  catch (IndexNotFoundException e) {
    // no repositories were created yet, so there is no point in searching
    return false;
  }
}
 
Example #2
Source File: ValidateQueryRequestBuilder.java    From elasticshell with Apache License 2.0 5 votes vote down vote up
@Override
protected XContentBuilder toXContent(ValidateQueryRequest request, ValidateQueryResponse response, XContentBuilder builder) throws IOException {
    builder.startObject();
    builder.field("valid", response.isValid());
    buildBroadcastShardsHeader(builder, response);

    List<? extends QueryExplanation> queryExplanation = response.getQueryExplanation();

    if (queryExplanation != null && !queryExplanation.isEmpty()) {
        builder.startArray("explanations");
        for (QueryExplanation explanation : queryExplanation) {
            builder.startObject();
            if (explanation.getIndex() != null) {
                builder.field("index", explanation.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
            }
            builder.field("valid", explanation.isValid());
            if (explanation.getError() != null) {
                builder.field("error", explanation.getError());
            }
            if (explanation.getExplanation() != null) {
                builder.field("explanation", explanation.getExplanation());
            }
            builder.endObject();
        }
        builder.endArray();
    }
    builder.endObject();
    return builder;
}
 
Example #3
Source File: RestValidateQueryAction.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void handleRequest(final RestRequest request, final RestChannel channel, final Client client) {
    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(Strings.splitStringByCommaToArray(request.param("index")));
    validateQueryRequest.indicesOptions(IndicesOptions.fromRequest(request, validateQueryRequest.indicesOptions()));
    if (RestActions.hasBodyContent(request)) {
        validateQueryRequest.source(RestActions.getRestContent(request));
    } else {
        QuerySourceBuilder querySourceBuilder = RestActions.parseQuerySource(request);
        if (querySourceBuilder != null) {
            validateQueryRequest.source(querySourceBuilder);
        }
    }
    validateQueryRequest.types(Strings.splitStringByCommaToArray(request.param("type")));
    if (request.paramAsBoolean("explain", false)) {
        validateQueryRequest.explain(true);
    } else {
        validateQueryRequest.explain(false);
    }
    if (request.paramAsBoolean("rewrite", false)) {
        validateQueryRequest.rewrite(true);
    } else {
        validateQueryRequest.rewrite(false);
    }

    client.admin().indices().validateQuery(validateQueryRequest, new RestBuilderListener<ValidateQueryResponse>(channel) {
        @Override
        public RestResponse buildResponse(ValidateQueryResponse response, XContentBuilder builder) throws Exception {
            builder.startObject();
            builder.field("valid", response.isValid());

            buildBroadcastShardsHeader(builder, request, response);

            if (response.getQueryExplanation() != null && !response.getQueryExplanation().isEmpty()) {
                builder.startArray("explanations");
                for (QueryExplanation explanation : response.getQueryExplanation()) {
                    builder.startObject();
                    if (explanation.getIndex() != null) {
                        builder.field("index", explanation.getIndex(), XContentBuilder.FieldCaseConversion.NONE);
                    }
                    builder.field("valid", explanation.isValid());
                    if (explanation.getError() != null) {
                        builder.field("error", explanation.getError());
                    }
                    if (explanation.getExplanation() != null) {
                        builder.field("explanation", explanation.getExplanation());
                    }
                    builder.endObject();
                }
                builder.endArray();
            }
            builder.endObject();
            return new BytesRestResponse(OK, builder);
        }
    });
}