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

The following examples show how to use org.elasticsearch.action.admin.indices.validate.query.ValidateQueryResponse. 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: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * This is a new search that accepts additionalSearchInformation. We need it for our complex question searches.
 * We have duplicated the methods that need this parameter, like prepareSearchRequest
 */
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInformation) {
    final Pair<SearchRequestBuilder,QueryBuilder> searchBuilders = prepareSearchRequest(searchTerms, references, siteIds, start, end, additionalSearchInformation);
    final SearchRequestBuilder searchRequestBuilder = searchBuilders.getLeft();
    final QueryBuilder queryBuilder = searchBuilders.getRight();

    getLog().debug("Search request from index builder [" + getName() + "]: " + searchRequestBuilder.toString());

    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(indexName);
    QuerySourceBuilder querySourceBuilder = new QuerySourceBuilder().setQuery(queryBuilder);
    validateQueryRequest.source(querySourceBuilder);
    validateQueryRequest.explain(true);


    try {
        ActionFuture<ValidateQueryResponse> future = client.admin().indices().validateQuery(validateQueryRequest); // the client is org.elasticsearch.client.Client
        ValidateQueryResponse responseV = future.get(); // typical java future as response

        if (responseV.isValid()) {
            SearchResponse response = searchRequestBuilder.execute().actionGet();
            getLog().debug("Search request from index builder [" + getName() + "] took: " + response.getTook().format());
            eventTrackingService.post(eventTrackingService.newEvent(SearchService.EVENT_SEARCH,
                    SearchService.EVENT_SEARCH_REF + queryBuilder.toString(), true,
                    NotificationService.PREF_IMMEDIATE));

            return response;
        }else{
            return null;
        }
    }catch(Exception ex){
        return null;
    }

}
 
Example #3
Source File: QuestionElasticSearchIndexBuilder.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
/**
 * This is a new search that accepts additionalSearchInformation. We need it for our complex question searches.
 * We have duplicated the methods that need this parameter, like prepareSearchRequest
 */
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInformation) {
    final Pair<SearchRequestBuilder,QueryBuilder> searchBuilders = prepareSearchRequest(searchTerms, references, siteIds, start, end, additionalSearchInformation);
    final SearchRequestBuilder searchRequestBuilder = searchBuilders.getLeft();
    final QueryBuilder queryBuilder = searchBuilders.getRight();

    getLog().debug("Search request from index builder [" + getName() + "]: " + searchRequestBuilder.toString());

    ValidateQueryRequest validateQueryRequest = new ValidateQueryRequest(indexName);
    QuerySourceBuilder querySourceBuilder = new QuerySourceBuilder().setQuery(queryBuilder);
    validateQueryRequest.source(querySourceBuilder);
    validateQueryRequest.explain(true);


    try {
        ActionFuture<ValidateQueryResponse> future = client.admin().indices().validateQuery(validateQueryRequest); // the client is org.elasticsearch.client.Client
        ValidateQueryResponse responseV = future.get(); // typical java future as response

        if (responseV.isValid()) {
            SearchResponse response = searchRequestBuilder.execute().actionGet();
            getLog().debug("Search request from index builder [" + getName() + "] took: " + response.getTook().format());
            eventTrackingService.post(eventTrackingService.newEvent(SearchService.EVENT_SEARCH,
                    SearchService.EVENT_SEARCH_REF + queryBuilder.toString(), true,
                    NotificationService.PREF_IMMEDIATE));

            return response;
        }else{
            return null;
        }
    }catch(Exception ex){
        return null;
    }

}
 
Example #4
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 #5
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public ActionFuture<ValidateQueryResponse> validateQuery(final ValidateQueryRequest request) {
    return execute(ValidateQueryAction.INSTANCE, request);
}
 
Example #6
Source File: AbstractClient.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void validateQuery(final ValidateQueryRequest request, final ActionListener<ValidateQueryResponse> listener) {
    execute(ValidateQueryAction.INSTANCE, request, listener);
}
 
Example #7
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);
        }
    });
}
 
Example #8
Source File: ValidateQueryRequestBuilder.java    From elasticshell with Apache License 2.0 4 votes vote down vote up
@Override
protected ActionFuture<ValidateQueryResponse> doExecute(ValidateQueryRequest request) {
    return client.admin().indices().validateQuery(request);
}
 
Example #9
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Validate a query for correctness.
 *
 * @param request The count request
 * @return The result future
 * @see Requests#countRequest(String...)
 */
ActionFuture<ValidateQueryResponse> validateQuery(ValidateQueryRequest request);
 
Example #10
Source File: IndicesAdminClient.java    From Elasticsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Validate a query for correctness.
 *
 * @param request  The count request
 * @param listener A listener to be notified of the result
 * @see Requests#countRequest(String...)
 */
void validateQuery(ValidateQueryRequest request, ActionListener<ValidateQueryResponse> listener);