Search in sources :

Example 81 with XContentParser

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

the class NestedIdentityTests method testFromXContent.

public void testFromXContent() throws IOException {
    NestedIdentity nestedIdentity = createTestItem(randomInt(3));
    XContentType xcontentType = randomFrom(XContentType.values());
    XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
    if (randomBoolean()) {
        builder.prettyPrint();
    }
    builder = nestedIdentity.innerToXContent(builder, ToXContent.EMPTY_PARAMS);
    try (XContentParser parser = createParser(builder)) {
        NestedIdentity parsedNestedIdentity = NestedIdentity.fromXContent(parser);
        assertEquals(nestedIdentity, parsedNestedIdentity);
        assertNull(parser.nextToken());
    }
}
Also used : XContentType(org.opensearch.common.xcontent.XContentType) NestedIdentity(org.opensearch.search.SearchHit.NestedIdentity) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 82 with XContentParser

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

the class AggregatorFactoriesTests method testTwoTypes.

public void testTwoTypes() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("in_stock").startObject("filter").startObject("range").startObject("stock").field("gt", 0).endObject().endObject().endObject().startObject("terms").field("field", "stock").endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
    assertThat(e.toString(), containsString("Found two aggregation type definitions in [in_stock]: [filter] and [terms]"));
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) ParsingException(org.opensearch.common.ParsingException) IOException(java.io.IOException)

Example 83 with XContentParser

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

the class AggregatorFactoriesTests method testInvalidType.

public void testInvalidType() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("tags").startObject("term").field("field", "tag").endObject().endObject().endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
    assertThat(e.toString(), containsString("Unknown aggregation type [term] did you mean [terms]?"));
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) ParsingException(org.opensearch.common.ParsingException) IOException(java.io.IOException)

Example 84 with XContentParser

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

the class AggregatorFactoriesTests method testMissingType.

public void testMissingType() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("tag_count").field("field", "tag").endObject().endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
    assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [tag_count]"));
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) ParsingException(org.opensearch.common.ParsingException) IOException(java.io.IOException)

Example 85 with XContentParser

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

the class AggregatorFactoriesTests method testMissingName.

public void testMissingName() throws Exception {
    XContentBuilder source = JsonXContent.contentBuilder().startObject().startObject("by_date").startObject("date_histogram").field("field", "timestamp").field("calendar_interval", "month").endObject().startObject("aggs").startObject("cardinality").field("field", "tag").endObject().endObject().endObject().endObject();
    XContentParser parser = createParser(source);
    assertSame(XContentParser.Token.START_OBJECT, parser.nextToken());
    Exception e = expectThrows(ParsingException.class, () -> AggregatorFactories.parseAggregators(parser));
    assertThat(e.toString(), containsString("Expected [START_OBJECT] under [field], but got a [VALUE_STRING] in [cardinality]"));
}
Also used : XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) XContentParser(org.opensearch.common.xcontent.XContentParser) ParsingException(org.opensearch.common.ParsingException) IOException(java.io.IOException)

Aggregations

XContentParser (org.opensearch.common.xcontent.XContentParser)694 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)245 IOException (java.io.IOException)127 BytesReference (org.opensearch.common.bytes.BytesReference)117 ParsingException (org.opensearch.common.ParsingException)111 XContentType (org.opensearch.common.xcontent.XContentType)106 ArrayList (java.util.ArrayList)80 List (java.util.List)56 OpenSearchParseException (org.opensearch.OpenSearchParseException)55 Matchers.containsString (org.hamcrest.Matchers.containsString)51 HashMap (java.util.HashMap)49 Map (java.util.Map)46 ToXContent (org.opensearch.common.xcontent.ToXContent)45 XContentParseException (org.opensearch.common.xcontent.XContentParseException)40 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)35 SearchResponse (org.opensearch.action.search.SearchResponse)28 BytesArray (org.opensearch.common.bytes.BytesArray)26 XContentParserUtils.ensureExpectedToken (org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken)25 GetRequest (org.opensearch.action.get.GetRequest)24 DiscoveryNode (org.opensearch.cluster.node.DiscoveryNode)24