Search in sources :

Example 46 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class HighlightBuilderTests method testFromXContent.

/**
     *  creates random highlighter, renders it to xContent and back to new instance that should be equal to original
     */
public void testFromXContent() throws IOException {
    for (int runs = 0; runs < NUMBER_OF_TESTBUILDERS; runs++) {
        HighlightBuilder highlightBuilder = randomHighlighterBuilder();
        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            builder.prettyPrint();
        }
        highlightBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
        XContentBuilder shuffled = shuffleXContent(builder);
        XContentParser parser = createParser(shuffled);
        QueryParseContext context = new QueryParseContext(parser);
        parser.nextToken();
        HighlightBuilder secondHighlightBuilder;
        try {
            secondHighlightBuilder = HighlightBuilder.fromXContent(context);
        } catch (RuntimeException e) {
            throw new RuntimeException("Error parsing " + highlightBuilder, e);
        }
        assertNotSame(highlightBuilder, secondHighlightBuilder);
        assertEquals(highlightBuilder, secondHighlightBuilder);
        assertEquals(highlightBuilder.hashCode(), secondHighlightBuilder.hashCode());
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 47 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class SearchSourceBuilderTests method assertParseSearchSource.

private void assertParseSearchSource(SearchSourceBuilder testBuilder, XContentParser parser) throws IOException {
    QueryParseContext parseContext = new QueryParseContext(parser);
    if (randomBoolean()) {
        // sometimes we move it on the START_OBJECT to
        parser.nextToken();
    // test the embedded case
    }
    SearchSourceBuilder newBuilder = SearchSourceBuilder.fromXContent(parseContext);
    assertNull(parser.nextToken());
    assertEquals(testBuilder, newBuilder);
    assertEquals(testBuilder.hashCode(), newBuilder.hashCode());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext)

Example 48 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class IncludeExcludeTests method serializeMixedRegex.

// Serializes/deserializes the IncludeExclude statement with include AND
// exclude clauses
private IncludeExclude serializeMixedRegex(IncludeExclude incExc) throws IOException {
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder.startObject();
    incExc.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.endObject();
    XContentParser parser = createParser(builder);
    QueryParseContext parseContext = new QueryParseContext(parser);
    XContentParser.Token token = parser.nextToken();
    assertEquals(token, XContentParser.Token.START_OBJECT);
    IncludeExclude inc = null;
    IncludeExclude exc = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        assertEquals(XContentParser.Token.FIELD_NAME, token);
        if (IncludeExclude.INCLUDE_FIELD.match(parser.currentName())) {
            token = parser.nextToken();
            inc = IncludeExclude.parseInclude(parser, parseContext);
        } else if (IncludeExclude.EXCLUDE_FIELD.match(parser.currentName())) {
            token = parser.nextToken();
            exc = IncludeExclude.parseExclude(parser, parseContext);
        } else {
            throw new IllegalArgumentException("Unexpected field name serialized in test: " + parser.currentName());
        }
    }
    assertNotNull(inc);
    assertNotNull(exc);
    // Include and Exclude clauses are parsed independently and then merged
    return IncludeExclude.merge(inc, exc);
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) IncludeExclude(org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 49 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class TemplateQueryBuilder method doRewrite.

@Override
protected QueryBuilder doRewrite(QueryRewriteContext queryRewriteContext) throws IOException {
    BytesReference querySource = queryRewriteContext.getTemplateBytes(template);
    try (XContentParser qSourceParser = XContentFactory.xContent(querySource).createParser(queryRewriteContext.getXContentRegistry(), querySource)) {
        final QueryParseContext queryParseContext = queryRewriteContext.newParseContext(qSourceParser);
        final QueryBuilder queryBuilder = queryParseContext.parseInnerQueryBuilder();
        if (boost() != DEFAULT_BOOST || queryName() != null) {
            final BoolQueryBuilder boolQueryBuilder = new BoolQueryBuilder();
            boolQueryBuilder.must(queryBuilder);
            return boolQueryBuilder;
        }
        return queryBuilder;
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) QueryBuilder(org.elasticsearch.index.query.QueryBuilder) AbstractQueryBuilder(org.elasticsearch.index.query.AbstractQueryBuilder) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 50 with QueryParseContext

use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.

the class TransportSearchTemplateAction method doExecute.

@Override
protected void doExecute(SearchTemplateRequest request, ActionListener<SearchTemplateResponse> listener) {
    final SearchTemplateResponse response = new SearchTemplateResponse();
    try {
        Script script = new Script(request.getScriptType(), TEMPLATE_LANG, request.getScript(), request.getScriptParams() == null ? Collections.emptyMap() : request.getScriptParams());
        ExecutableScript executable = scriptService.executable(script, SEARCH);
        BytesReference source = (BytesReference) executable.run();
        response.setSource(source);
        if (request.isSimulate()) {
            listener.onResponse(response);
            return;
        }
        // Executes the search
        SearchRequest searchRequest = request.getRequest();
        //we can assume the template is always json as we convert it before compiling it
        try (XContentParser parser = XContentFactory.xContent(XContentType.JSON).createParser(xContentRegistry, source)) {
            SearchSourceBuilder builder = SearchSourceBuilder.searchSource();
            builder.parseXContent(new QueryParseContext(parser));
            builder.explain(request.isExplain());
            builder.profile(request.isProfile());
            searchRequest.source(builder);
            searchAction.execute(searchRequest, new ActionListener<SearchResponse>() {

                @Override
                public void onResponse(SearchResponse searchResponse) {
                    try {
                        response.setResponse(searchResponse);
                        listener.onResponse(response);
                    } catch (Exception t) {
                        listener.onFailure(t);
                    }
                }

                @Override
                public void onFailure(Exception t) {
                    listener.onFailure(t);
                }
            });
        }
    } catch (Exception t) {
        listener.onFailure(t);
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) Script(org.elasticsearch.script.Script) ExecutableScript(org.elasticsearch.script.ExecutableScript) SearchRequest(org.elasticsearch.action.search.SearchRequest) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ExecutableScript(org.elasticsearch.script.ExecutableScript) XContentParser(org.elasticsearch.common.xcontent.XContentParser) SearchSourceBuilder(org.elasticsearch.search.builder.SearchSourceBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

QueryParseContext (org.elasticsearch.index.query.QueryParseContext)54 XContentParser (org.elasticsearch.common.xcontent.XContentParser)44 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ParsingException (org.elasticsearch.common.ParsingException)12 IOException (java.io.IOException)6 QueryBuilder (org.elasticsearch.index.query.QueryBuilder)5 ArrayList (java.util.ArrayList)2 Collections.unmodifiableMap (java.util.Collections.unmodifiableMap)2 List (java.util.List)2 Map (java.util.Map)2 Consumer (java.util.function.Consumer)2 SearchRequest (org.elasticsearch.action.search.SearchRequest)2 SearchResponse (org.elasticsearch.action.search.SearchResponse)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 BoolQueryBuilder (org.elasticsearch.index.query.BoolQueryBuilder)2 SignificanceHeuristicParser (org.elasticsearch.search.aggregations.bucket.significant.heuristics.SignificanceHeuristicParser)2 IncludeExclude (org.elasticsearch.search.aggregations.bucket.terms.support.IncludeExclude)2 SearchContext (org.elasticsearch.search.internal.SearchContext)2 Matchers.containsString (org.hamcrest.Matchers.containsString)2 Closeable (java.io.Closeable)1