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;
};
}
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());
}
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
}
}
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());
}
}
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;
}
Aggregations