Search in sources :

Example 96 with ParsingException

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

the class GeoDistanceSortBuilder method fromXContent.

/**
 * Creates a new {@link GeoDistanceSortBuilder} from the query held by the {@link XContentParser} in
 * {@link org.opensearch.common.xcontent.XContent} format.
 *
 * @param parser the input parser. The state on the parser contained in this context will be changed as a
 *                side effect of this method call
 * @param elementName in some sort syntax variations the field name precedes the xContent object that specifies
 *                    further parameters, e.g. in '{ "foo": { "order" : "asc"} }'. When parsing the inner object,
 *                    the field name can be passed in via this argument
 */
public static GeoDistanceSortBuilder fromXContent(XContentParser parser, String elementName) throws IOException {
    String fieldName = null;
    List<GeoPoint> geoPoints = new ArrayList<>();
    DistanceUnit unit = DistanceUnit.DEFAULT;
    GeoDistance geoDistance = GeoDistance.ARC;
    SortOrder order = SortOrder.ASC;
    SortMode sortMode = null;
    QueryBuilder nestedFilter = null;
    String nestedPath = null;
    NestedSortBuilder nestedSort = null;
    GeoValidationMethod validation = null;
    boolean ignoreUnmapped = false;
    XContentParser.Token token;
    String currentName = parser.currentName();
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentName = parser.currentName();
        } else if (token == XContentParser.Token.START_ARRAY) {
            parseGeoPoints(parser, geoPoints);
            fieldName = currentName;
        } else if (token == XContentParser.Token.START_OBJECT) {
            if (NESTED_FILTER_FIELD.match(currentName, parser.getDeprecationHandler())) {
                deprecationLogger.deprecate("geo_distance_nested_filter", "[nested_filter] has been deprecated in favour of the [nested] parameter");
                nestedFilter = parseInnerQueryBuilder(parser);
            } else if (NESTED_FIELD.match(currentName, parser.getDeprecationHandler())) {
                nestedSort = NestedSortBuilder.fromXContent(parser);
            } else {
                // the json in the format of -> field : { lat : 30, lon : 12 }
                if (fieldName != null && fieldName.equals(currentName) == false) {
                    throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
                }
                fieldName = currentName;
                GeoPoint point = new GeoPoint();
                GeoUtils.parseGeoPoint(parser, point);
                geoPoints.add(point);
            }
        } else if (token.isValue()) {
            if (ORDER_FIELD.match(currentName, parser.getDeprecationHandler())) {
                order = SortOrder.fromString(parser.text());
            } else if (UNIT_FIELD.match(currentName, parser.getDeprecationHandler())) {
                unit = DistanceUnit.fromString(parser.text());
            } else if (DISTANCE_TYPE_FIELD.match(currentName, parser.getDeprecationHandler())) {
                geoDistance = GeoDistance.fromString(parser.text());
            } else if (VALIDATION_METHOD_FIELD.match(currentName, parser.getDeprecationHandler())) {
                validation = GeoValidationMethod.fromString(parser.text());
            } else if (SORTMODE_FIELD.match(currentName, parser.getDeprecationHandler())) {
                sortMode = SortMode.fromString(parser.text());
            } else if (NESTED_PATH_FIELD.match(currentName, parser.getDeprecationHandler())) {
                deprecationLogger.deprecate("geo_distance_nested_path", "[nested_path] has been deprecated in favour of the [nested] parameter");
                nestedPath = parser.text();
            } else if (IGNORE_UNMAPPED.match(currentName, parser.getDeprecationHandler())) {
                ignoreUnmapped = parser.booleanValue();
            } else if (token == Token.VALUE_STRING) {
                if (fieldName != null && fieldName.equals(currentName) == false) {
                    throw new ParsingException(parser.getTokenLocation(), "Trying to reset fieldName to [{}], already set to [{}].", currentName, fieldName);
                }
                GeoPoint point = new GeoPoint();
                point.resetFromString(parser.text());
                geoPoints.add(point);
                fieldName = currentName;
            } else if (fieldName.equals(currentName)) {
                throw new ParsingException(parser.getTokenLocation(), "Only geohashes of type string supported for field [{}]", currentName);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[{}] does not support [{}]", NAME, currentName);
            }
        }
    }
    GeoDistanceSortBuilder result = new GeoDistanceSortBuilder(fieldName, geoPoints.toArray(new GeoPoint[geoPoints.size()]));
    result.geoDistance(geoDistance);
    result.unit(unit);
    result.order(order);
    if (sortMode != null) {
        result.sortMode(sortMode);
    }
    if (nestedFilter != null) {
        result.setNestedFilter(nestedFilter);
    }
    result.setNestedPath(nestedPath);
    if (nestedSort != null) {
        result.setNestedSort(nestedSort);
    }
    if (validation != null) {
        result.validation(validation);
    }
    result.ignoreUnmapped(ignoreUnmapped);
    return result;
}
Also used : GeoValidationMethod(org.opensearch.index.query.GeoValidationMethod) ArrayList(java.util.ArrayList) AbstractQueryBuilder.parseInnerQueryBuilder(org.opensearch.index.query.AbstractQueryBuilder.parseInnerQueryBuilder) QueryBuilder(org.opensearch.index.query.QueryBuilder) GeoPoint(org.opensearch.common.geo.GeoPoint) Token(org.opensearch.common.xcontent.XContentParser.Token) ParsingException(org.opensearch.common.ParsingException) GeoDistance(org.opensearch.common.geo.GeoDistance) DistanceUnit(org.opensearch.common.unit.DistanceUnit) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 97 with ParsingException

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

the class RescorerBuilder method parseFromXContent.

public static RescorerBuilder<?> parseFromXContent(XContentParser parser) throws IOException {
    String fieldName = null;
    RescorerBuilder<?> rescorer = null;
    Integer windowSize = null;
    XContentParser.Token token;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token.isValue()) {
            if (WINDOW_SIZE_FIELD.match(fieldName, parser.getDeprecationHandler())) {
                windowSize = parser.intValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "rescore doesn't support [" + fieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            rescorer = parser.namedObject(RescorerBuilder.class, fieldName, null);
        } else {
            throw new ParsingException(parser.getTokenLocation(), "unexpected token [" + token + "] after [" + fieldName + "]");
        }
    }
    if (rescorer == null) {
        throw new ParsingException(parser.getTokenLocation(), "missing rescore type");
    }
    if (windowSize != null) {
        rescorer.windowSize(windowSize.intValue());
    }
    return rescorer;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 98 with ParsingException

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

the class SuggestionBuilder method fromXContent.

static SuggestionBuilder<?> fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String currentFieldName = null;
    String suggestText = null;
    String prefix = null;
    String regex = null;
    SuggestionBuilder<?> suggestionBuilder = null;
    while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TEXT_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                suggestText = parser.text();
            } else if (PREFIX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                prefix = parser.text();
            } else if (REGEX_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                regex = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "suggestion does not support [" + currentFieldName + "]");
            }
        } else if (token == XContentParser.Token.START_OBJECT) {
            suggestionBuilder = parser.namedObject(SuggestionBuilder.class, currentFieldName, null);
        }
    }
    if (suggestionBuilder == null) {
        throw new OpenSearchParseException("missing suggestion object");
    }
    if (suggestText != null) {
        suggestionBuilder.text(suggestText);
    }
    if (prefix != null) {
        suggestionBuilder.prefix(prefix);
    }
    if (regex != null) {
        suggestionBuilder.regex(regex);
    }
    return suggestionBuilder;
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 99 with ParsingException

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

the class LinearInterpolation method fromXContent.

public static LinearInterpolation fromXContent(XContentParser parser) throws IOException {
    XContentParser.Token token;
    String fieldName = null;
    double trigramLambda = 0.0;
    double bigramLambda = 0.0;
    double unigramLambda = 0.0;
    while ((token = parser.nextToken()) != Token.END_OBJECT) {
        if (token == XContentParser.Token.FIELD_NAME) {
            fieldName = parser.currentName();
        } else if (token.isValue()) {
            if (TRIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
                trigramLambda = parser.doubleValue();
                if (trigramLambda < 0) {
                    throw new IllegalArgumentException("trigram_lambda must be positive");
                }
            } else if (BIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
                bigramLambda = parser.doubleValue();
                if (bigramLambda < 0) {
                    throw new IllegalArgumentException("bigram_lambda must be positive");
                }
            } else if (UNIGRAM_FIELD.match(fieldName, parser.getDeprecationHandler())) {
                unigramLambda = parser.doubleValue();
                if (unigramLambda < 0) {
                    throw new IllegalArgumentException("unigram_lambda must be positive");
                }
            } else {
                throw new IllegalArgumentException("suggester[phrase][smoothing][linear] doesn't support field [" + fieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + NAME + "] unknown token [" + token + "] after [" + fieldName + "]");
        }
    }
    return new LinearInterpolation(trigramLambda, bigramLambda, unigramLambda);
}
Also used : Token(org.opensearch.common.xcontent.XContentParser.Token) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 100 with ParsingException

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

the class ExceptionSerializationTests method testUnknownException.

public void testUnknownException() throws IOException {
    ParsingException parsingException = new ParsingException(1, 2, "foobar", null);
    final Exception ex = new UnknownException("eggplant", parsingException);
    Exception exception = serialize(ex);
    assertEquals("unknown_exception: eggplant", exception.getMessage());
    assertTrue(exception instanceof OpenSearchException);
    ParsingException e = (ParsingException) exception.getCause();
    assertEquals(parsingException.getIndex(), e.getIndex());
    assertEquals(parsingException.getMessage(), e.getMessage());
    assertEquals(parsingException.getLineNumber(), e.getLineNumber());
    assertEquals(parsingException.getColumnNumber(), e.getColumnNumber());
}
Also used : TimestampParsingException(org.opensearch.action.TimestampParsingException) ParsingException(org.opensearch.common.ParsingException) IndexFormatTooNewException(org.apache.lucene.index.IndexFormatTooNewException) AlreadyClosedException(org.apache.lucene.store.AlreadyClosedException) RecoveryEngineException(org.opensearch.index.engine.RecoveryEngineException) RetentionLeaseAlreadyExistsException(org.opensearch.index.seqno.RetentionLeaseAlreadyExistsException) RetentionLeaseInvalidRetainingSeqNoException(org.opensearch.index.seqno.RetentionLeaseInvalidRetainingSeqNoException) IngestProcessorException(org.opensearch.ingest.IngestProcessorException) AccessDeniedException(java.nio.file.AccessDeniedException) LockObtainFailedException(org.apache.lucene.store.LockObtainFailedException) SnapshotException(org.opensearch.snapshots.SnapshotException) RecoverFilesRecoveryException(org.opensearch.indices.recovery.RecoverFilesRecoveryException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) NotDirectoryException(java.nio.file.NotDirectoryException) DirectoryNotEmptyException(java.nio.file.DirectoryNotEmptyException) IOException(java.io.IOException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) ActionTransportException(org.opensearch.transport.ActionTransportException) NoSuchFileException(java.nio.file.NoSuchFileException) URISyntaxException(java.net.URISyntaxException) NoSuchRemoteClusterException(org.opensearch.transport.NoSuchRemoteClusterException) CorruptIndexException(org.apache.lucene.index.CorruptIndexException) NodeHealthCheckFailureException(org.opensearch.cluster.coordination.NodeHealthCheckFailureException) AliasesNotFoundException(org.opensearch.rest.action.admin.indices.AliasesNotFoundException) TimestampParsingException(org.opensearch.action.TimestampParsingException) ParsingException(org.opensearch.common.ParsingException) RepositoryException(org.opensearch.repositories.RepositoryException) ActionNotFoundTransportException(org.opensearch.transport.ActionNotFoundTransportException) SnapshotInProgressException(org.opensearch.snapshots.SnapshotInProgressException) FileSystemException(java.nio.file.FileSystemException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) QueryShardException(org.opensearch.index.query.QueryShardException) SearchParseException(org.opensearch.search.SearchParseException) SearchContextMissingException(org.opensearch.search.SearchContextMissingException) EOFException(java.io.EOFException) FileNotFoundException(java.io.FileNotFoundException) CoordinationStateRejectedException(org.opensearch.cluster.coordination.CoordinationStateRejectedException) ConnectTransportException(org.opensearch.transport.ConnectTransportException) NoSeedNodeLeftException(org.opensearch.transport.NoSeedNodeLeftException) IndexTemplateMissingException(org.opensearch.indices.IndexTemplateMissingException) IllegalShardRoutingStateException(org.opensearch.cluster.routing.IllegalShardRoutingStateException) RoutingMissingException(org.opensearch.action.RoutingMissingException) ShardNotInPrimaryModeException(org.opensearch.index.shard.ShardNotInPrimaryModeException) SearchException(org.opensearch.search.SearchException) FileSystemLoopException(java.nio.file.FileSystemLoopException) IllegalIndexShardStateException(org.opensearch.index.shard.IllegalIndexShardStateException) FailedNodeException(org.opensearch.action.FailedNodeException) ShardLockObtainFailedException(org.opensearch.env.ShardLockObtainFailedException) IndexFormatTooOldException(org.apache.lucene.index.IndexFormatTooOldException) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) AtomicMoveNotSupportedException(java.nio.file.AtomicMoveNotSupportedException) InvalidIndexTemplateException(org.opensearch.indices.InvalidIndexTemplateException) RetentionLeaseNotFoundException(org.opensearch.index.seqno.RetentionLeaseNotFoundException)

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