Search in sources :

Example 41 with QueryParseContext

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

the class ScriptSortBuilderTests method testParseJson_simple.

public void testParseJson_simple() throws IOException {
    String scriptSort = "{\n" + "\"_script\" : {\n" + "\"type\" : \"number\",\n" + "\"script\" : \"doc['field_name'].value\",\n" + "\"mode\" : \"max\",\n" + "\"order\" : \"asc\"\n" + "} }\n";
    XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort);
    parser.nextToken();
    parser.nextToken();
    parser.nextToken();
    QueryParseContext context = new QueryParseContext(parser);
    ScriptSortBuilder builder = ScriptSortBuilder.fromXContent(context, null);
    assertEquals("doc['field_name'].value", builder.script().getIdOrCode());
    assertEquals(Script.DEFAULT_SCRIPT_LANG, builder.script().getLang());
    assertEquals(builder.script().getParams(), Collections.emptyMap());
    assertEquals(ScriptType.INLINE, builder.script().getType());
    assertEquals(ScriptSortType.NUMBER, builder.type());
    assertEquals(SortOrder.ASC, builder.order());
    assertEquals(SortMode.MAX, builder.sortMode());
    assertNull(builder.getNestedFilter());
    assertNull(builder.getNestedPath());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 42 with QueryParseContext

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

the class SortBuilderTests method parseSort.

private List<SortBuilder<?>> parseSort(String jsonString) throws IOException {
    XContentParser itemParser = createParser(JsonXContent.jsonXContent, jsonString);
    QueryParseContext context = new QueryParseContext(itemParser);
    assertEquals(XContentParser.Token.START_OBJECT, itemParser.nextToken());
    assertEquals(XContentParser.Token.FIELD_NAME, itemParser.nextToken());
    assertEquals("sort", itemParser.currentName());
    itemParser.nextToken();
    return SortBuilder.fromXContent(context);
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 43 with QueryParseContext

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

the class PercolatorFieldMapperTests method assertQueryBuilder.

private void assertQueryBuilder(BytesRef actual, QueryBuilder expected) throws IOException {
    XContentParser sourceParser = createParser(PercolatorFieldMapper.QUERY_BUILDER_CONTENT_TYPE.xContent(), new BytesArray(actual));
    QueryParseContext qsc = indexService.newQueryShardContext(randomInt(20), null, () -> {
        throw new UnsupportedOperationException();
    }).newParseContext(sourceParser);
    assertThat(qsc.parseInnerQueryBuilder(), equalTo(expected));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 44 with QueryParseContext

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

the class HighlightBuilderTests method testParsingEmptyStructure.

/**
     * test parsing empty highlight or empty fields blocks
     */
public void testParsingEmptyStructure() throws IOException {
    String highlightElement = "{ }";
    XContentParser parser = createParser(JsonXContent.jsonXContent, highlightElement);
    QueryParseContext context = new QueryParseContext(parser);
    HighlightBuilder highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("expected plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
    highlightElement = "{ \"fields\" : { } }";
    parser = createParser(JsonXContent.jsonXContent, highlightElement);
    context = new QueryParseContext(parser);
    highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("defining no field should return plain HighlightBuilder", new HighlightBuilder(), highlightBuilder);
    highlightElement = "{ \"fields\" : { \"foo\" : { } } }";
    parser = createParser(JsonXContent.jsonXContent, highlightElement);
    context = new QueryParseContext(parser);
    highlightBuilder = HighlightBuilder.fromXContent(context);
    assertEquals("expected HighlightBuilder with field", new HighlightBuilder().field(new Field("foo")), highlightBuilder);
}
Also used : Field(org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder.Field) QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 45 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)

Aggregations

QueryParseContext (org.elasticsearch.index.query.QueryParseContext)55 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)14 ParsingException (org.elasticsearch.common.ParsingException)12 IOException (java.io.IOException)7 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 NamedXContentRegistry (org.elasticsearch.common.xcontent.NamedXContentRegistry)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 SearchSourceBuilder (org.elasticsearch.search.builder.SearchSourceBuilder)2 SearchContext (org.elasticsearch.search.internal.SearchContext)2