Search in sources :

Example 1 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class TransportValidateQueryAction method shardOperation.

@Override
protected ShardValidateQueryResponse shardOperation(ShardValidateQueryRequest request, Task task) throws IOException {
    boolean valid;
    String explanation = null;
    String error = null;
    ShardSearchRequest shardSearchLocalRequest = new ShardSearchRequest(request.shardId(), request.nowInMillis(), request.filteringAliases());
    SearchContext searchContext = searchService.createSearchContext(shardSearchLocalRequest, SearchService.NO_TIMEOUT);
    try {
        ParsedQuery parsedQuery = searchContext.getQueryShardContext().toQuery(request.query());
        searchContext.parsedQuery(parsedQuery);
        searchContext.preProcess(request.rewrite());
        valid = true;
        explanation = explain(searchContext, request.rewrite());
    } catch (QueryShardException | ParsingException e) {
        valid = false;
        error = e.getDetailedMessage();
    } catch (AssertionError e) {
        valid = false;
        error = e.getMessage();
    } finally {
        Releasables.close(searchContext);
    }
    return new ShardValidateQueryResponse(request.shardId(), valid, explanation, error);
}
Also used : ParsedQuery(org.opensearch.index.query.ParsedQuery) ParsingException(org.opensearch.common.ParsingException) ShardSearchRequest(org.opensearch.search.internal.ShardSearchRequest) SearchContext(org.opensearch.search.internal.SearchContext) QueryShardException(org.opensearch.index.query.QueryShardException)

Example 2 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class HasParentQueryBuilder method fromXContent.

public static HasParentQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String parentType = null;
    boolean score = false;
    String queryName = null;
    InnerHitBuilder innerHits = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String currentFieldName = null;
    XContentParser.Token token;
    QueryBuilder iqb = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                iqb = parseInnerQueryBuilder(parser);
            } else if (INNER_HITS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                innerHits = InnerHitBuilder.fromXContent(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (PARENT_TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                parentType = parser.text();
            } else if (SCORE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                score = parser.booleanValue();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_parent] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasParentQueryBuilder queryBuilder = new HasParentQueryBuilder(parentType, iqb, score).ignoreUnmapped(ignoreUnmapped).queryName(queryName).boost(boost);
    if (innerHits != null) {
        queryBuilder.innerHit(innerHits);
    }
    return queryBuilder;
}
Also used : ParsingException(org.opensearch.common.ParsingException) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 3 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class ParentIdQueryBuilder method fromXContent.

public static ParentIdQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String type = null;
    String id = null;
    String queryName = null;
    String currentFieldName = null;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                type = parser.text();
            } else if (ID_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                id = parser.text();
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[parent_id] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[parent_id] query does not support [" + currentFieldName + "]");
        }
    }
    ParentIdQueryBuilder queryBuilder = new ParentIdQueryBuilder(type, id);
    queryBuilder.queryName(queryName);
    queryBuilder.boost(boost);
    queryBuilder.ignoreUnmapped(ignoreUnmapped);
    return queryBuilder;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 4 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class ChildrenAggregationBuilder method parse.

public static ChildrenAggregationBuilder parse(String aggregationName, XContentParser parser) throws IOException {
    String childType = null;
    XContentParser.Token token;
    String currentFieldName = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.VALUE_STRING) {
            if ("type".equals(currentFieldName)) {
                childType = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "Unknown key for a " + token + " in [" + aggregationName + "]: [" + currentFieldName + "].");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "Unexpected token " + token + " in [" + aggregationName + "].");
        }
    }
    if (childType == null) {
        throw new ParsingException(parser.getTokenLocation(), "Missing [child_type] field for children aggregation [" + aggregationName + "]");
    }
    return new ChildrenAggregationBuilder(aggregationName, childType);
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 5 with ParsingException

use of org.opensearch.common.ParsingException in project OpenSearch by opensearch-project.

the class RankEvalResponseTests method testToXContent.

public void testToXContent() throws IOException {
    EvalQueryQuality coffeeQueryQuality = new EvalQueryQuality("coffee_query", 0.1);
    coffeeQueryQuality.addHitsAndRatings(Arrays.asList(searchHit("index", 123, 5), searchHit("index", 456, null)));
    RankEvalResponse response = new RankEvalResponse(0.123, Collections.singletonMap("coffee_query", coffeeQueryQuality), Collections.singletonMap("beer_query", new ParsingException(new XContentLocation(0, 0), "someMsg")));
    XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
    String xContent = BytesReference.bytes(response.toXContent(builder, ToXContent.EMPTY_PARAMS)).utf8ToString();
    assertEquals(("{" + "    \"metric_score\": 0.123," + "    \"details\": {" + "        \"coffee_query\": {" + "            \"metric_score\": 0.1," + "            \"unrated_docs\": [{\"_index\":\"index\",\"_id\":\"456\"}]," + "            \"hits\":[{\"hit\":{\"_index\":\"index\",\"_id\":\"123\",\"_score\":1.0}," + "                       \"rating\":5}," + "                      {\"hit\":{\"_index\":\"index\",\"_id\":\"456\",\"_score\":1.0}," + "                       \"rating\":null}" + "                     ]" + "        }" + "    }," + "    \"failures\": {" + "        \"beer_query\": {" + "          \"error\" : {\"root_cause\": [{\"type\":\"parsing_exception\", \"reason\":\"someMsg\",\"line\":0,\"col\":0}]," + "                       \"type\":\"parsing_exception\"," + "                       \"reason\":\"someMsg\"," + "                       \"line\":0,\"col\":0" + "                      }" + "        }" + "    }" + "}").replaceAll("\\s+", ""), xContent);
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentLocation(org.opensearch.common.xcontent.XContentLocation) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder)

Aggregations

ParsingException (org.opensearch.common.ParsingException)157 XContentParser (org.opensearch.common.xcontent.XContentParser)92 ArrayList (java.util.ArrayList)27 Matchers.containsString (org.hamcrest.Matchers.containsString)21 IOException (java.io.IOException)14 Token (org.opensearch.common.xcontent.XContentParser.Token)11 List (java.util.List)10 ShardId (org.opensearch.index.shard.ShardId)10 SearchShardTarget (org.opensearch.search.SearchShardTarget)10 BytesReference (org.opensearch.common.bytes.BytesReference)9 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)9 HashMap (java.util.HashMap)7 QueryBuilder (org.opensearch.index.query.QueryBuilder)7 ShardSearchFailure (org.opensearch.action.search.ShardSearchFailure)6 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)5 OpenSearchParseException (org.opensearch.OpenSearchParseException)5 TimestampParsingException (org.opensearch.action.TimestampParsingException)5 XContentLocation (org.opensearch.common.xcontent.XContentLocation)5 Script (org.opensearch.script.Script)5 GapPolicy (org.opensearch.search.aggregations.pipeline.BucketHelpers.GapPolicy)5