Search in sources :

Example 91 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class FileInfoTests method testInvalidFieldsInFromXContent.

public void testInvalidFieldsInFromXContent() throws IOException {
    final int iters = scaledRandomIntBetween(1, 10);
    for (int iter = 0; iter < iters; iter++) {
        final BytesRef hash = new BytesRef(scaledRandomIntBetween(0, 1024 * 1024));
        hash.length = hash.bytes.length;
        for (int i = 0; i < hash.length; i++) {
            hash.bytes[i] = randomByte();
        }
        String name = "foobar";
        String physicalName = "_foobar";
        String failure = null;
        long length = Math.max(0, Math.abs(randomLong()));
        // random corruption
        switch(randomIntBetween(0, 3)) {
            case 0:
                name = "foo,bar";
                failure = "missing or invalid file name";
                break;
            case 1:
                physicalName = "_foo,bar";
                failure = "missing or invalid physical file name";
                break;
            case 2:
                length = -Math.abs(randomLong());
                failure = "missing or invalid file length";
                break;
            case 3:
                break;
            default:
                fail("shouldn't be here");
        }
        XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
        builder.startObject();
        builder.field(FileInfo.NAME, name);
        builder.field(FileInfo.PHYSICAL_NAME, physicalName);
        builder.field(FileInfo.LENGTH, length);
        builder.field(FileInfo.WRITTEN_BY, Version.LATEST.toString());
        builder.field(FileInfo.CHECKSUM, "666");
        builder.endObject();
        byte[] xContent = BytesReference.toBytes(BytesReference.bytes(builder));
        if (failure == null) {
            // No failures should read as usual
            final BlobStoreIndexShardSnapshot.FileInfo parsedInfo;
            try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
                parser.nextToken();
                parsedInfo = BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
            }
            assertThat(name, equalTo(parsedInfo.name()));
            assertThat(physicalName, equalTo(parsedInfo.physicalName()));
            assertThat(length, equalTo(parsedInfo.length()));
            assertEquals("666", parsedInfo.checksum());
            assertEquals("666", parsedInfo.metadata().checksum());
            assertEquals(Version.LATEST, parsedInfo.metadata().writtenBy());
        } else {
            try (XContentParser parser = createParser(JsonXContent.jsonXContent, xContent)) {
                parser.nextToken();
                BlobStoreIndexShardSnapshot.FileInfo.fromXContent(parser);
                fail("Should have failed with [" + failure + "]");
            } catch (OpenSearchParseException ex) {
                assertThat(ex.getMessage(), containsString(failure));
            }
        }
    }
}
Also used : OpenSearchParseException(org.opensearch.OpenSearchParseException) Matchers.containsString(org.hamcrest.Matchers.containsString) FileInfo(org.opensearch.index.snapshots.blobstore.BlobStoreIndexShardSnapshot.FileInfo) BytesRef(org.apache.lucene.util.BytesRef) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 92 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class SpanWithinQueryBuilder method fromXContent.

public static SpanWithinQueryBuilder fromXContent(XContentParser parser) throws IOException {
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String queryName = null;
    SpanQueryBuilder big = null;
    SpanQueryBuilder little = null;
    String currentFieldName = null;
    XContentParser.Token token;
    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 (BIG_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                QueryBuilder query = parseInnerQueryBuilder(parser);
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_within [big] must be of type span query");
                }
                big = (SpanQueryBuilder) query;
                checkNoBoost(NAME, currentFieldName, parser, big);
            } else if (LITTLE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                QueryBuilder query = parseInnerQueryBuilder(parser);
                if (query instanceof SpanQueryBuilder == false) {
                    throw new ParsingException(parser.getTokenLocation(), "span_within [little] must be of type span query");
                }
                little = (SpanQueryBuilder) query;
                checkNoBoost(NAME, currentFieldName, parser, little);
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[span_within] query does not support [" + currentFieldName + "]");
            }
        } 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(), "[span_within] query does not support [" + currentFieldName + "]");
        }
    }
    if (big == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_within must include [big]");
    }
    if (little == null) {
        throw new ParsingException(parser.getTokenLocation(), "span_within must include [little]");
    }
    SpanWithinQueryBuilder query = new SpanWithinQueryBuilder(big, little);
    query.boost(boost).queryName(queryName);
    return query;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 93 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class TypeQueryBuilder method fromXContent.

public static TypeQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String type = null;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    String currentFieldName = null;
    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 (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else if (VALUE_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                type = parser.text();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter doesn't support [" + currentFieldName + "]");
        }
    }
    if (type == null) {
        throw new ParsingException(parser.getTokenLocation(), "[" + TypeQueryBuilder.NAME + "] filter needs to be provided with a value for the type");
    }
    return new TypeQueryBuilder(type).boost(boost).queryName(queryName);
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 94 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class MatchNoneQueryBuilder method fromXContent.

public static MatchNoneQueryBuilder fromXContent(XContentParser parser) throws IOException {
    String currentFieldName = null;
    XContentParser.Token token;
    String queryName = null;
    float boost = AbstractQueryBuilder.DEFAULT_BOOST;
    while (((token = parser.nextToken()) != XContentParser.Token.END_OBJECT && token != XContentParser.Token.END_ARRAY)) {
        if (token == XContentParser.Token.FIELD_NAME) {
            currentFieldName = parser.currentName();
        } else if (token.isValue()) {
            if (AbstractQueryBuilder.NAME_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                queryName = parser.text();
            } else if (AbstractQueryBuilder.BOOST_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                boost = parser.floatValue();
            } else {
                throw new ParsingException(parser.getTokenLocation(), "[" + MatchNoneQueryBuilder.NAME + "] query does not support [" + currentFieldName + "]");
            }
        } else {
            throw new ParsingException(parser.getTokenLocation(), "[" + MatchNoneQueryBuilder.NAME + "] unknown token [" + token + "] after [" + currentFieldName + "]");
        }
    }
    MatchNoneQueryBuilder matchNoneQueryBuilder = new MatchNoneQueryBuilder();
    matchNoneQueryBuilder.boost(boost);
    matchNoneQueryBuilder.queryName(queryName);
    return matchNoneQueryBuilder;
}
Also used : ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 95 with XContentParser

use of org.opensearch.common.xcontent.XContentParser in project OpenSearch by opensearch-project.

the class MatchPhraseQueryBuilder method fromXContent.

public static MatchPhraseQueryBuilder 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;
    ZeroTermsQuery zeroTermsQuery = MatchQuery.DEFAULT_ZERO_TERMS_QUERY;
    String queryName = null;
    String currentFieldName = null;
    XContentParser.Token token;
    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 (SLOP_FIELD.match(currentFieldName, parser.getDeprecationHandler())) {
                        slop = 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();
        }
    }
    MatchPhraseQueryBuilder matchQuery = new MatchPhraseQueryBuilder(fieldName, value);
    matchQuery.analyzer(analyzer);
    matchQuery.slop(slop);
    matchQuery.zeroTermsQuery(zeroTermsQuery);
    matchQuery.queryName(queryName);
    matchQuery.boost(boost);
    return matchQuery;
}
Also used : ZeroTermsQuery(org.opensearch.index.search.MatchQuery.ZeroTermsQuery) ParsingException(org.opensearch.common.ParsingException) XContentParser(org.opensearch.common.xcontent.XContentParser)

Aggregations

XContentParser (org.opensearch.common.xcontent.XContentParser)602 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)222 ParsingException (org.opensearch.common.ParsingException)105 BytesReference (org.opensearch.common.bytes.BytesReference)104 IOException (java.io.IOException)97 XContentType (org.opensearch.common.xcontent.XContentType)88 ArrayList (java.util.ArrayList)73 OpenSearchParseException (org.opensearch.OpenSearchParseException)55 Matchers.containsString (org.hamcrest.Matchers.containsString)51 List (java.util.List)43 XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 HashMap (java.util.HashMap)38 ToXContent (org.opensearch.common.xcontent.ToXContent)38 Map (java.util.Map)32 BytesArray (org.opensearch.common.bytes.BytesArray)26 ShardId (org.opensearch.index.shard.ShardId)25 XContent (org.opensearch.common.xcontent.XContent)19 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)16 CategoryContextMapping (org.opensearch.search.suggest.completion.context.CategoryContextMapping)16 Collections (java.util.Collections)15