Search in sources :

Example 26 with QueryParseContext

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

the class AbstractHighlighterBuilder method setupParser.

static <HB extends AbstractHighlighterBuilder<HB>> BiFunction<QueryParseContext, HB, HB> setupParser(ObjectParser<HB, QueryParseContext> parser) {
    parser.declareStringArray(fromList(String.class, HB::preTags), PRE_TAGS_FIELD);
    parser.declareStringArray(fromList(String.class, HB::postTags), POST_TAGS_FIELD);
    parser.declareString(HB::order, ORDER_FIELD);
    parser.declareBoolean(HB::highlightFilter, HIGHLIGHT_FILTER_FIELD);
    parser.declareInt(HB::fragmentSize, FRAGMENT_SIZE_FIELD);
    parser.declareInt(HB::numOfFragments, NUMBER_OF_FRAGMENTS_FIELD);
    parser.declareBoolean(HB::requireFieldMatch, REQUIRE_FIELD_MATCH_FIELD);
    parser.declareString(HB::boundaryScannerType, BOUNDARY_SCANNER_FIELD);
    parser.declareInt(HB::boundaryMaxScan, BOUNDARY_MAX_SCAN_FIELD);
    parser.declareString((HB hb, String bc) -> hb.boundaryChars(bc.toCharArray()), BOUNDARY_CHARS_FIELD);
    parser.declareString(HB::boundaryScannerLocale, BOUNDARY_SCANNER_LOCALE_FIELD);
    parser.declareString(HB::highlighterType, TYPE_FIELD);
    parser.declareString(HB::fragmenter, FRAGMENTER_FIELD);
    parser.declareInt(HB::noMatchSize, NO_MATCH_SIZE_FIELD);
    parser.declareBoolean(HB::forceSource, FORCE_SOURCE_FIELD);
    parser.declareInt(HB::phraseLimit, PHRASE_LIMIT_FIELD);
    parser.declareObject(HB::options, (XContentParser p, QueryParseContext c) -> {
        try {
            return p.map();
        } catch (IOException e) {
            throw new RuntimeException("Error parsing options", e);
        }
    }, OPTIONS_FIELD);
    parser.declareObject(HB::highlightQuery, (XContentParser p, QueryParseContext c) -> {
        try {
            return c.parseInnerQueryBuilder();
        } catch (IOException e) {
            throw new RuntimeException("Error parsing query", e);
        }
    }, HIGHLIGHT_QUERY_FIELD);
    return (QueryParseContext c, HB hb) -> {
        try {
            parser.parse(c.parser(), hb, c);
            if (hb.preTags() != null && hb.postTags() == null) {
                throw new ParsingException(c.parser().getTokenLocation(), "pre_tags are set but post_tags are not set");
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return hb;
    };
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ParsingException(org.elasticsearch.common.ParsingException) IOException(java.io.IOException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 27 with QueryParseContext

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

the class SliceBuilderTests method testFromXContent.

public void testFromXContent() throws Exception {
    SliceBuilder sliceBuilder = randomSliceBuilder();
    XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder.startObject();
    sliceBuilder.innerToXContent(builder);
    builder.endObject();
    XContentParser parser = createParser(shuffleXContent(builder));
    QueryParseContext context = new QueryParseContext(parser);
    SliceBuilder secondSliceBuilder = SliceBuilder.fromXContent(context);
    assertNotSame(sliceBuilder, secondSliceBuilder);
    assertEquals(sliceBuilder, secondSliceBuilder);
    assertEquals(sliceBuilder.hashCode(), secondSliceBuilder.hashCode());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 28 with QueryParseContext

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

the class FieldSortBuilderTests method testReverseOptionFails.

public void testReverseOptionFails() throws IOException {
    String json = "{ \"post_date\" : {\"reverse\" : true} },\n";
    XContentParser parser = createParser(JsonXContent.jsonXContent, json);
    // need to skip until parser is located on second START_OBJECT
    parser.nextToken();
    parser.nextToken();
    parser.nextToken();
    QueryParseContext context = new QueryParseContext(parser);
    try {
        FieldSortBuilder.fromXContent(context, "");
        fail("adding reverse sorting option should fail with an exception");
    } catch (IllegalArgumentException e) {
    // all good
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 29 with QueryParseContext

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

the class QueryRescoreBuilderTests method testFromXContent.

/**
     *  creates random rescorer, 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++) {
        RescoreBuilder<?> rescoreBuilder = randomRescoreBuilder();
        XContentBuilder builder = XContentFactory.contentBuilder(randomFrom(XContentType.values()));
        if (randomBoolean()) {
            builder.prettyPrint();
        }
        rescoreBuilder.toXContent(builder, ToXContent.EMPTY_PARAMS);
        XContentBuilder shuffled = shuffleXContent(builder);
        XContentParser parser = createParser(shuffled);
        QueryParseContext context = new QueryParseContext(parser);
        parser.nextToken();
        RescoreBuilder<?> secondRescoreBuilder = RescoreBuilder.parseFromXContent(context);
        assertNotSame(rescoreBuilder, secondRescoreBuilder);
        assertEquals(rescoreBuilder, secondRescoreBuilder);
        assertEquals(rescoreBuilder.hashCode(), secondRescoreBuilder.hashCode());
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 30 with QueryParseContext

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

the class QueryRescoreBuilderTests method createContext.

/**
     * create a new parser from the rescorer string representation and reset context with it
     */
private QueryParseContext createContext(String rescoreElement) throws IOException {
    XContentParser parser = createParser(JsonXContent.jsonXContent, rescoreElement);
    QueryParseContext context = new QueryParseContext(parser);
    // move to first token, this is where the internal fromXContent
    assertTrue(parser.nextToken() == XContentParser.Token.START_OBJECT);
    return context;
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) 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