Search in sources :

Example 31 with QueryParseContext

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);
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) ParsingException(org.elasticsearch.common.ParsingException)

Example 32 with QueryParseContext

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());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 33 with QueryParseContext

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
    }
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 34 with QueryParseContext

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());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 35 with QueryParseContext

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());
}
Also used : QueryParseContext(org.elasticsearch.index.query.QueryParseContext) XContentParser(org.elasticsearch.common.xcontent.XContentParser) IOException(java.io.IOException)

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