use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SearchHitTests method testFromXContent.
public void testFromXContent() throws IOException {
SearchHit searchHit = createTestItem(true);
boolean humanReadable = randomBoolean();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference originalBytes = toXContent(searchHit, xContentType, humanReadable);
SearchHit parsed;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
// jump to first START_OBJECT
parser.nextToken();
parsed = SearchHit.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SearchHitsTests method testFromXContent.
public void testFromXContent() throws IOException {
SearchHits searchHits = createTestItem();
XContentType xcontentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(searchHits, xcontentType, humanReadable);
SearchHits parsed;
try (XContentParser parser = createParser(xcontentType.xContent(), originalBytes)) {
parsed = SearchHits.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xcontentType, humanReadable), xcontentType);
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class FiltersAggsRewriteIT method testWrapperQueryIsRewritten.
public void testWrapperQueryIsRewritten() throws IOException {
createIndex("test", Settings.EMPTY, "test", "title", "type=text");
client().prepareIndex("test", "test", "1").setSource("title", "foo bar baz").get();
client().prepareIndex("test", "test", "2").setSource("title", "foo foo foo").get();
client().prepareIndex("test", "test", "3").setSource("title", "bar baz bax").get();
client().admin().indices().prepareRefresh("test").get();
XContentType xContentType = randomFrom(XContentType.values());
BytesReference bytesReference;
try (XContentBuilder builder = XContentFactory.contentBuilder(xContentType)) {
builder.startObject();
{
builder.startObject("terms");
{
builder.array("title", "foo");
}
builder.endObject();
}
builder.endObject();
bytesReference = builder.bytes();
}
FiltersAggregationBuilder builder = new FiltersAggregationBuilder("titles", new FiltersAggregator.KeyedFilter("titleterms", new WrapperQueryBuilder(bytesReference)));
SearchResponse searchResponse = client().prepareSearch("test").setSize(0).addAggregation(builder).get();
assertEquals(3, searchResponse.getHits().getTotalHits());
InternalFilters filters = searchResponse.getAggregations().get("titles");
assertEquals(1, filters.getBuckets().size());
assertEquals(2, filters.getBuckets().get(0).getDocCount());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class HighlightFieldTests method testFromXContent.
public void testFromXContent() throws IOException {
HighlightField highlightField = createTestItem();
XContentType xcontentType = randomFrom(XContentType.values());
XContentBuilder builder = XContentFactory.contentBuilder(xcontentType);
if (randomBoolean()) {
builder.prettyPrint();
}
// we need to wrap xContent output in proper object to create a parser for it
builder.startObject();
builder = highlightField.toXContent(builder, ToXContent.EMPTY_PARAMS);
builder.endObject();
XContentParser parser = createParser(builder);
// skip to the opening object token, fromXContent advances from here and starts with the field name
parser.nextToken();
parser.nextToken();
HighlightField parsedField = HighlightField.fromXContent(parser);
assertEquals(highlightField, parsedField);
if (highlightField.fragments() != null) {
assertEquals(XContentParser.Token.END_ARRAY, parser.currentToken());
}
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.
the class SearchProfileShardResultsTests method testFromXContent.
public void testFromXContent() throws IOException {
SearchProfileShardResults shardResult = createTestItem();
XContentType xContentType = randomFrom(XContentType.values());
boolean humanReadable = randomBoolean();
BytesReference originalBytes = toXContent(shardResult, xContentType, humanReadable);
SearchProfileShardResults parsed = null;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
ensureFieldName(parser, parser.nextToken(), SearchProfileShardResults.PROFILE_FIELD);
ensureExpectedToken(parser.nextToken(), XContentParser.Token.START_OBJECT, parser::getTokenLocation);
parsed = SearchProfileShardResults.fromXContent(parser);
assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
assertNull(parser.nextToken());
}
assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Aggregations