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