Search in sources :

Example 81 with ParsingException

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

the class HasChildQueryBuilder method fromXContent.

public static HasChildQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String childType = null;
    ScoreMode scoreMode = ScoreMode.None;
    int minChildren = HasChildQueryBuilder.DEFAULT_MIN_CHILDREN;
    int maxChildren = HasChildQueryBuilder.DEFAULT_MAX_CHILDREN;
    boolean ignoreUnmapped = DEFAULT_IGNORE_UNMAPPED;
    String queryName = null;
    InnerHitBuilder innerHitBuilder = null;
    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())) {
                innerHitBuilder = InnerHitBuilder.fromXContent(parser);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        } else if (token.isValue()) {
            if (TYPE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                childType = parser.text();
            } else if (SCORE_MODE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                scoreMode = NestedQueryBuilder.parseScoreMode(parser.text());
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (MIN_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                minChildren = parser.intValue(true);
            } else if (MAX_CHILDREN_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                maxChildren = parser.intValue(true);
            } else if (IGNORE_UNMAPPED_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[has_child] query does not support [" + currentFieldName + "]");
            }
        }
    }
    HasChildQueryBuilder hasChildQueryBuilder = new HasChildQueryBuilder(childType, iqb, scoreMode);
    hasChildQueryBuilder.minMaxChildren(minChildren, maxChildren);
    hasChildQueryBuilder.queryName(queryName);
    hasChildQueryBuilder.boost(boost);
    hasChildQueryBuilder.ignoreUnmapped(ignoreUnmapped);
    if (innerHitBuilder != null) {
        hasChildQueryBuilder.innerHit(innerHitBuilder);
    }
    return hasChildQueryBuilder;
}
Also used : ScoreMode(org.apache.lucene.search.join.ScoreMode) ParsingException(org.opensearch.common.ParsingException) InnerHitBuilder(org.opensearch.index.query.InnerHitBuilder) NestedQueryBuilder(org.opensearch.index.query.NestedQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) AbstractQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 82 with ParsingException

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

the class ParentAggregationBuilder method parse.

public static ParentAggregationBuilder 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 parent aggregation [" + aggregationName + "]");
    }
    return new ParentAggregationBuilder(aggregationName, childType);
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 83 with ParsingException

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

the class RemoteScrollableHitSourceTests method testParseRequestFailure.

public void testParseRequestFailure() throws Exception {
    AtomicBoolean called = new AtomicBoolean();
    Consumer<Response> checkResponse = r -> {
        assertFalse(r.isTimedOut());
        assertNull(r.getScrollId());
        assertEquals(0, r.getTotalHits());
        assertThat(r.getFailures(), hasSize(1));
        assertThat(r.getFailures().get(0).getReason(), instanceOf(ParsingException.class));
        ParsingException failure = (ParsingException) r.getFailures().get(0).getReason();
        assertEquals("Unknown key for a VALUE_STRING in [invalid].", failure.getMessage());
        assertEquals(2, failure.getLineNumber());
        assertEquals(14, failure.getColumnNumber());
        called.set(true);
    };
    sourceWithMockedRemoteCall("request_failure.json").doStart(wrapAsListener(checkResponse));
    assertTrue(called.get());
    called.set(false);
    sourceWithMockedRemoteCall("request_failure.json").doStartNextScroll("scroll", timeValueMillis(0), wrapAsListener(checkResponse));
    assertTrue(called.get());
}
Also used : Response(org.opensearch.index.reindex.ScrollableHitSource.Response) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpResponse(org.apache.http.HttpResponse) OpenSearchRejectedExecutionException(org.opensearch.common.util.concurrent.OpenSearchRejectedExecutionException) TimeValue.timeValueMinutes(org.opensearch.common.unit.TimeValue.timeValueMinutes) URL(java.net.URL) HttpClientContext(org.apache.http.client.protocol.HttpClientContext) OpenSearchStatusException(org.opensearch.OpenSearchStatusException) TestThreadPool(org.opensearch.threadpool.TestThreadPool) ByteSizeUnit(org.opensearch.common.unit.ByteSizeUnit) Version(org.opensearch.Version) FileSystemUtils(org.opensearch.common.io.FileSystemUtils) StatusLine(org.apache.http.StatusLine) Mockito.doThrow(org.mockito.Mockito.doThrow) Future(java.util.concurrent.Future) BackoffPolicy(org.opensearch.action.bulk.BackoffPolicy) After(org.junit.After) RestClient(org.opensearch.client.RestClient) HttpAsyncRequestProducer(org.apache.http.nio.protocol.HttpAsyncRequestProducer) ParsingException(org.opensearch.common.ParsingException) TimeValue(org.opensearch.common.unit.TimeValue) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) HttpEntity(org.apache.http.HttpEntity) ContentType(org.apache.http.entity.ContentType) Response(org.opensearch.index.reindex.ScrollableHitSource.Response) StringEntity(org.apache.http.entity.StringEntity) CloseableHttpAsyncClient(org.apache.http.impl.nio.client.CloseableHttpAsyncClient) RestStatus(org.opensearch.rest.RestStatus) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) StandardCharsets(java.nio.charset.StandardCharsets) ScrollableHitSource(org.opensearch.index.reindex.ScrollableHitSource) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) Stream(java.util.stream.Stream) BytesArray(org.opensearch.common.bytes.BytesArray) SearchSourceBuilder(org.opensearch.search.builder.SearchSourceBuilder) RejectAwareActionListener(org.opensearch.index.reindex.RejectAwareActionListener) Queue(java.util.Queue) Mockito.any(org.mockito.Mockito.any) Mockito.mock(org.mockito.Mockito.mock) TimeValue.timeValueMillis(org.opensearch.common.unit.TimeValue.timeValueMillis) ContentTooLongException(org.apache.http.ContentTooLongException) BasicStatusLine(org.apache.http.message.BasicStatusLine) ThreadPool(org.opensearch.threadpool.ThreadPool) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteSizeValue(org.opensearch.common.unit.ByteSizeValue) OpenSearchExecutors(org.opensearch.common.util.concurrent.OpenSearchExecutors) AtomicReference(java.util.concurrent.atomic.AtomicReference) HttpAsyncResponseConsumer(org.apache.http.nio.protocol.HttpAsyncResponseConsumer) Answer(org.mockito.stubbing.Answer) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LegacyESVersion(org.opensearch.LegacyESVersion) Streams(org.opensearch.common.io.Streams) SearchRequest(org.opensearch.action.search.SearchRequest) Matchers.hasSize(org.hamcrest.Matchers.hasSize) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) HttpEntityEnclosingRequest(org.apache.http.HttpEntityEnclosingRequest) Matchers.empty(org.hamcrest.Matchers.empty) FutureCallback(org.apache.http.concurrent.FutureCallback) IOException(java.io.IOException) HeapBufferedAsyncResponseConsumer(org.opensearch.client.HeapBufferedAsyncResponseConsumer) Mockito.when(org.mockito.Mockito.when) InputStreamReader(java.io.InputStreamReader) Mockito.verify(org.mockito.Mockito.verify) Consumer(java.util.function.Consumer) ProtocolVersion(org.apache.http.ProtocolVersion) HttpResponse(org.apache.http.HttpResponse) InputStreamEntity(org.apache.http.entity.InputStreamEntity) HttpHost(org.apache.http.HttpHost) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ParsingException(org.opensearch.common.ParsingException)

Example 84 with ParsingException

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

the class MatchBoolPrefixQueryBuilder method fromXContent.

public static MatchBoolPrefixQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    Operator operator = DEFAULT_OPERATOR;
    String minimumShouldMatch = null;
    Fuzziness fuzziness = null;
    int prefixLength = FuzzyQuery.defaultPrefixLength;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    boolean fuzzyTranspositions = FuzzyQuery.defaultTranspositions;
    String fuzzyRewrite = null;
    String queryName = 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.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        value = parser.objectText();
                    } else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        analyzer = parser.text();
                    } else if (OPERATOR_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        operator = Operator.fromString(parser.text());
                    } else if (MatchQueryBuilder.MINIMUM_SHOULD_MATCH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        minimumShouldMatch = parser.textOrNull();
                    } else if (Fuzziness.FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzziness = Fuzziness.parse(parser);
                    } else if (PREFIX_LENGTH_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        prefixLength = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        maxExpansion = parser.intValue();
                    } else if (FUZZY_TRANSPOSITIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzzyTranspositions = parser.booleanValue();
                    } else if (FUZZY_REWRITE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        fuzzyRewrite = parser.textOrNull();
                    } 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(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }
    MatchBoolPrefixQueryBuilder queryBuilder = new MatchBoolPrefixQueryBuilder(fieldName, value);
    queryBuilder.analyzer(analyzer);
    queryBuilder.operator(operator);
    queryBuilder.minimumShouldMatch(minimumShouldMatch);
    queryBuilder.boost(boost);
    queryBuilder.queryName(queryName);
    if (fuzziness != null) {
        queryBuilder.fuzziness(fuzziness);
    }
    queryBuilder.prefixLength(prefixLength);
    queryBuilder.maxExpansions(maxExpansion);
    queryBuilder.fuzzyTranspositions(fuzzyTranspositions);
    queryBuilder.fuzzyRewrite(fuzzyRewrite);
    return queryBuilder;
}
Also used : Fuzziness(org.opensearch.common.unit.Fuzziness) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 85 with ParsingException

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

the class MatchPhrasePrefixQueryBuilder method fromXContent.

public static MatchPhrasePrefixQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    Object value = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String analyzer = null;
    int slop = MatchQuery.DEFAULT_PHRASE_SLOP;
    int maxExpansion = FuzzyQuery.defaultMaxExpansions;
    String queryName = null;
    XContentParser.Token token;
    String currentFieldName = null;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token == XContentParser.Token.START_OBJECT) {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, currentFieldName);
            fieldName = currentFieldName;
            while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
                if (token == XContentParser.Token.FIELD_NAME) {
                    currentFieldName = parser.currentName();
                } else if (token.isValue()) {
                    if (MatchQueryBuilder.QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        value = parser.objectText();
                    } else if (MatchQueryBuilder.ANALYZER_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        analyzer = parser.text();
                    } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        boost = parser.floatValue();
                    } else if (MatchPhraseQueryBuilder.SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        slop = parser.intValue();
                    } else if (MAX_EXPANSIONS_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        maxExpansion = parser.intValue();
                    } else if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        queryName = parser.text();
                    } else if (ZERO_TERMS_QUERY_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        String zeroTermsValue = parser.text();
                        if ("none".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.NONE;
                        } else if ("all".equalsIgnoreCase(zeroTermsValue)) {
                            zeroTermsQuery = ZeroTermsQuery.ALL;
                        } else {
                            throw new ParsingException(parser.getTokenLocation(), "Unsupported zero_terms_query value [" + zeroTermsValue + "]");
                        }
                    } else {
                        throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] query does not support [" + currentFieldName + "]");
                    }
                } else {
                    throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
                }
            }
        } else {
            throwParsingExceptionOnMultipleFields(NAME, parser.getTokenLocation(), fieldName, parser.currentName());
            fieldName = parser.currentName();
            value = parser.objectText();
        }
    }
    MatchPhrasePrefixQueryBuilder matchQuery = new MatchPhrasePrefixQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.maxExpansions(maxExpansion);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    return matchQuery;
}
Also used : ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

ParsingException (org.opensearch.common.ParsingException)160 XContentParser (org.opensearch.common.xcontent.XContentParser)94 ArrayList (java.util.ArrayList)28 Matchers.containsString (org.hamcrest.Matchers.containsString)21 IOException (java.io.IOException)15 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 QueryBuilder (org.opensearch.index.query.QueryBuilder)9 HashMap (java.util.HashMap)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