use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class QueryRescoreBuilderTests method testUnknownFieldsExpection.
/**
* test parsing exceptions for incorrect rescorer syntax
*/
public void testUnknownFieldsExpection() throws IOException {
String rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"bad_rescorer_name\" : { }\n" + "}\n";
QueryParseContext context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (ParsingException e) {
assertEquals("rescore doesn't support rescorer with name [bad_rescorer_name]", e.getMessage());
}
rescoreElement = "{\n" + " \"bad_fieldName\" : 20\n" + "}\n";
context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (ParsingException e) {
assertEquals("rescore doesn't support [bad_fieldName]", e.getMessage());
}
rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : [ ]\n" + "}\n";
context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (ParsingException e) {
assertEquals("unexpected token [START_ARRAY] after [query]", e.getMessage());
}
rescoreElement = "{ }";
context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (ParsingException e) {
assertEquals("missing rescore type", e.getMessage());
}
rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : { \"bad_fieldname\" : 1.0 } \n" + "}\n";
context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (IllegalArgumentException e) {
assertEquals("[query] unknown field [bad_fieldname], parser not found", e.getMessage());
}
rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : { \"rescore_query\" : { \"unknown_queryname\" : { } } } \n" + "}\n";
context = createContext(rescoreElement);
try {
RescoreBuilder.parseFromXContent(context);
fail("expected a parsing exception");
} catch (ParsingException e) {
assertEquals("[query] failed to parse field [rescore_query]", e.getMessage());
}
rescoreElement = "{\n" + " \"window_size\" : 20,\n" + " \"query\" : { \"rescore_query\" : { \"match_all\" : { } } } \n" + "}\n";
context = createContext(rescoreElement);
RescoreBuilder.parseFromXContent(context);
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class ScoreSortBuilderTests method testParseOrder.
/**
* test parsing order parameter if specified as `order` field in the json
* instead of the `reverse` field that we render in toXContent
*/
public void testParseOrder() throws IOException {
SortOrder order = randomBoolean() ? SortOrder.ASC : SortOrder.DESC;
String scoreSortString = "{ \"_score\": { \"order\": \"" + order.toString() + "\" }}";
XContentParser parser = createParser(JsonXContent.jsonXContent, scoreSortString);
// need to skip until parser is located on second START_OBJECT
parser.nextToken();
parser.nextToken();
parser.nextToken();
QueryParseContext context = new QueryParseContext(parser);
ScoreSortBuilder scoreSort = ScoreSortBuilder.fromXContent(context, "_score");
assertEquals(order, scoreSort.order());
}
use of org.elasticsearch.index.query.QueryParseContext in project elasticsearch by elastic.
the class ScoreSortBuilderTests method testReverseOptionFails.
public void testReverseOptionFails() throws IOException {
String json = "{ \"_score\": { \"reverse\": true }}";
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 {
ScoreSortBuilder.fromXContent(context, "_score");
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 ScriptSortBuilderTests method testParseJson.
public void testParseJson() throws IOException {
String scriptSort = "{\n" + "\"_script\" : {\n" + "\"type\" : \"number\",\n" + "\"script\" : {\n" + "\"inline\": \"doc['field_name'].value * factor\",\n" + "\"params\" : {\n" + "\"factor\" : 1.1\n" + "}\n" + "},\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 * factor", builder.script().getIdOrCode());
assertEquals(Script.DEFAULT_SCRIPT_LANG, builder.script().getLang());
assertEquals(1.1, builder.script().getParams().get("factor"));
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 ScriptSortBuilderTests method testParseUnexpectedToken.
public void testParseUnexpectedToken() throws IOException {
String scriptSort = "{\"_script\" : {" + "\"script\" : [ \"order\" : \"asc\" ] } }";
XContentParser parser = createParser(JsonXContent.jsonXContent, scriptSort);
parser.nextToken();
parser.nextToken();
parser.nextToken();
QueryParseContext context = new QueryParseContext(parser);
Exception e = expectThrows(IllegalArgumentException.class, () -> ScriptSortBuilder.fromXContent(context, null));
assertEquals("[_script] script doesn't support values of type: START_ARRAY", e.getMessage());
}
Aggregations