Search in sources :

Example 1 with ToXContent

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

the class EvalQueryQualityTests method testXContentParsing.

public void testXContentParsing() throws IOException {
    EvalQueryQuality testItem = randomEvalQueryQuality();
    boolean humanReadable = randomBoolean();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
    // skip inserting random fields for:
    // - the root object, since we expect a particular queryId there in this test
    // - the `metric_details` section, which can potentially contain different namedXContent names
    // - everything under `hits` (we test lenient SearchHit parsing elsewhere)
    Predicate<String> pathsToExclude = path -> path.isEmpty() || path.endsWith("metric_details") || path.contains("hits");
    BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, pathsToExclude, random());
    EvalQueryQuality parsedItem;
    try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
        ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser);
        String queryId = parser.currentName();
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
        parsedItem = EvalQueryQuality.fromXContent(parser, queryId);
        ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.currentToken(), parser);
        ensureExpectedToken(XContentParser.Token.END_OBJECT, parser.nextToken(), parser);
        assertNull(parser.nextToken());
    }
    assertNotSame(testItem, parsedItem);
    // we cannot check equality of object here because some information (e.g. SearchHit#shard) cannot fully be
    // parsed back after going through the rest layer. That's why we only check that the original and the parsed item
    // have the same xContent representation
    assertToXContentEquivalent(originalBytes, toXContent(parsedItem, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) OpenSearchAssertions.assertToXContentEquivalent(org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent) EqualsHashCodeTestUtils.checkEqualsAndHashCode(org.opensearch.test.EqualsHashCodeTestUtils.checkEqualsAndHashCode) BytesReference(org.opensearch.common.bytes.BytesReference) XContentTestUtils.insertRandomFields(org.opensearch.test.XContentTestUtils.insertRandomFields) Predicate(java.util.function.Predicate) ToXContent(org.opensearch.common.xcontent.ToXContent) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) IOException(java.io.IOException) XContentParserUtils.ensureExpectedToken(org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken) OriginalIndices(org.opensearch.action.OriginalIndices) NamedWriteableRegistry(org.opensearch.common.io.stream.NamedWriteableRegistry) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) ShardId(org.opensearch.index.shard.ShardId) List(java.util.List) XContentHelper.toXContent(org.opensearch.common.xcontent.XContentHelper.toXContent) SearchShardTarget(org.opensearch.search.SearchShardTarget) NamedXContentRegistry(org.opensearch.common.xcontent.NamedXContentRegistry) XContentType(org.opensearch.common.xcontent.XContentType) XContentType(org.opensearch.common.xcontent.XContentType) XContentParser(org.opensearch.common.xcontent.XContentParser)

Example 2 with ToXContent

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

the class AnalysisStats method toXContentCollection.

private void toXContentCollection(XContentBuilder builder, Params params, String name, Collection<? extends ToXContent> coll) throws IOException {
    builder.startArray(name);
    for (ToXContent toXContent : coll) {
        toXContent.toXContent(builder, params);
    }
    builder.endArray();
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent)

Example 3 with ToXContent

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

the class GetIndexTemplatesResponse method toXContent.

@Override
public XContentBuilder toXContent(XContentBuilder builder, Params params) throws IOException {
    params = new ToXContent.DelegatingMapParams(singletonMap("reduce_mappings", "true"), params);
    builder.startObject();
    for (IndexTemplateMetadata indexTemplateMetadata : getIndexTemplates()) {
        IndexTemplateMetadata.Builder.toXContent(indexTemplateMetadata, builder, params);
    }
    builder.endObject();
    return builder;
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) IndexTemplateMetadata(org.opensearch.cluster.metadata.IndexTemplateMetadata)

Example 4 with ToXContent

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

the class LegacyGeoShapeIntegrationIT method testLegacyCircle.

/**
 * Test that the circle is still supported for the legacy shapes
 */
public void testLegacyCircle() throws Exception {
    // create index
    assertAcked(client().admin().indices().prepareCreate("test").addMapping("geometry", "shape", "type=geo_shape,strategy=recursive,tree=geohash").get());
    ensureGreen();
    indexRandom(true, client().prepareIndex("test").setId("0").setSource("shape", (ToXContent) (builder, params) -> {
        builder.startObject().field("type", "circle").startArray("coordinates").value(30).value(50).endArray().field("radius", "77km").endObject();
        return builder;
    }));
    // test self crossing of circles
    SearchResponse searchResponse = client().prepareSearch("test").setQuery(geoShapeQuery("shape", new Circle(30, 50, 77000))).get();
    assertThat(searchResponse.getHits().getTotalHits().value, equalTo(1L));
}
Also used : ToXContent(org.opensearch.common.xcontent.ToXContent) Circle(org.opensearch.geometry.Circle) SearchResponse(org.opensearch.action.search.SearchResponse)

Example 5 with ToXContent

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

the class RankEvalResponseTests method testXContentParsing.

public void testXContentParsing() throws IOException {
    RankEvalResponse testItem = createRandomResponse();
    boolean humanReadable = randomBoolean();
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toShuffledXContent(testItem, xContentType, ToXContent.EMPTY_PARAMS, humanReadable);
    // skip inserting random fields for:
    // - the `details` section, which can contain arbitrary queryIds
    // - everything under `failures` (exceptions parsing is quiet lenient)
    // - everything under `hits` (we test lenient SearchHit parsing elsewhere)
    Predicate<String> pathsToExclude = path -> (path.endsWith("details") || path.contains("failures") || path.contains("hits"));
    BytesReference withRandomFields = insertRandomFields(xContentType, originalBytes, pathsToExclude, random());
    RankEvalResponse parsedItem;
    try (XContentParser parser = createParser(xContentType.xContent(), withRandomFields)) {
        parsedItem = RankEvalResponse.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertNotSame(testItem, parsedItem);
    // We cannot check equality of object here because some information (e.g.
    // SearchHit#shard) cannot fully be parsed back.
    assertEquals(testItem.getMetricScore(), parsedItem.getMetricScore(), 0.0);
    assertEquals(testItem.getPartialResults().keySet(), parsedItem.getPartialResults().keySet());
    for (EvalQueryQuality metricDetail : testItem.getPartialResults().values()) {
        EvalQueryQuality parsedEvalQueryQuality = parsedItem.getPartialResults().get(metricDetail.getId());
        assertToXContentEquivalent(toXContent(metricDetail, xContentType, humanReadable), toXContent(parsedEvalQueryQuality, xContentType, humanReadable), xContentType);
    }
    // Also exceptions that are parsed back will be different since they are re-wrapped during parsing.
    // However, we can check that there is the expected number
    assertEquals(testItem.getFailures().keySet(), parsedItem.getFailures().keySet());
    for (String queryId : testItem.getFailures().keySet()) {
        Exception ex = parsedItem.getFailures().get(queryId);
        assertThat(ex, instanceOf(OpenSearchException.class));
    }
}
Also used : BytesReference(org.opensearch.common.bytes.BytesReference) OpenSearchAssertions.assertToXContentEquivalent(org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent) Arrays(java.util.Arrays) BytesReference(org.opensearch.common.bytes.BytesReference) XContentTestUtils.insertRandomFields(org.opensearch.test.XContentTestUtils.insertRandomFields) ToXContent(org.opensearch.common.xcontent.ToXContent) HashMap(java.util.HashMap) OpenSearchException(org.opensearch.OpenSearchException) CircuitBreaker(org.opensearch.common.breaker.CircuitBreaker) OptionalInt(java.util.OptionalInt) ArrayList(java.util.ArrayList) XContentParser(org.opensearch.common.xcontent.XContentParser) XContentHelper.toXContent(org.opensearch.common.xcontent.XContentHelper.toXContent) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) XContentFactory(org.opensearch.common.xcontent.XContentFactory) NoMasterBlockService(org.opensearch.cluster.coordination.NoMasterBlockService) ParsingException(org.opensearch.common.ParsingException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) StreamInput(org.opensearch.common.io.stream.StreamInput) SearchHit(org.opensearch.search.SearchHit) Predicate(java.util.function.Predicate) OpenSearchTestCase(org.opensearch.test.OpenSearchTestCase) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) SearchParseException(org.opensearch.search.SearchParseException) BytesStreamOutput(org.opensearch.common.io.stream.BytesStreamOutput) OriginalIndices(org.opensearch.action.OriginalIndices) XContentBuilder(org.opensearch.common.xcontent.XContentBuilder) ShardId(org.opensearch.index.shard.ShardId) Matchers.instanceOf(org.hamcrest.Matchers.instanceOf) List(java.util.List) SearchShardTarget(org.opensearch.search.SearchShardTarget) SHARD_TARGET(org.opensearch.test.TestSearchContext.SHARD_TARGET) ShardSearchFailure(org.opensearch.action.search.ShardSearchFailure) XContentLocation(org.opensearch.common.xcontent.XContentLocation) XContentType(org.opensearch.common.xcontent.XContentType) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException) Collections(java.util.Collections) XContentType(org.opensearch.common.xcontent.XContentType) OpenSearchException(org.opensearch.OpenSearchException) XContentParser(org.opensearch.common.xcontent.XContentParser) OpenSearchException(org.opensearch.OpenSearchException) ParsingException(org.opensearch.common.ParsingException) CircuitBreakingException(org.opensearch.common.breaker.CircuitBreakingException) ClusterBlockException(org.opensearch.cluster.block.ClusterBlockException) IOException(java.io.IOException) SearchParseException(org.opensearch.search.SearchParseException) SearchPhaseExecutionException(org.opensearch.action.search.SearchPhaseExecutionException)

Aggregations

ToXContent (org.opensearch.common.xcontent.ToXContent)30 IOException (java.io.IOException)17 XContentBuilder (org.opensearch.common.xcontent.XContentBuilder)15 Collections (java.util.Collections)13 HashMap (java.util.HashMap)13 BytesReference (org.opensearch.common.bytes.BytesReference)13 XContentParser (org.opensearch.common.xcontent.XContentParser)13 XContentType (org.opensearch.common.xcontent.XContentType)13 List (java.util.List)12 Map (java.util.Map)12 OpenSearchTestCase (org.opensearch.test.OpenSearchTestCase)11 Predicate (java.util.function.Predicate)10 XContentHelper.toXContent (org.opensearch.common.xcontent.XContentHelper.toXContent)10 XContentTestUtils.insertRandomFields (org.opensearch.test.XContentTestUtils.insertRandomFields)9 OpenSearchAssertions.assertToXContentEquivalent (org.opensearch.test.hamcrest.OpenSearchAssertions.assertToXContentEquivalent)9 ArrayList (java.util.ArrayList)8 Strings (org.opensearch.common.Strings)7 Matchers.containsString (org.hamcrest.Matchers.containsString)5 Arrays (java.util.Arrays)4 NamedXContentRegistry (org.opensearch.common.xcontent.NamedXContentRegistry)4