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);
}
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();
}
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;
}
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));
}
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));
}
}
Aggregations