Search in sources :

Example 36 with XContentType

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 37 with 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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 38 with 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());
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) InternalFilters(org.elasticsearch.search.aggregations.bucket.filters.InternalFilters) XContentType(org.elasticsearch.common.xcontent.XContentType) FiltersAggregator(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregator) FiltersAggregationBuilder(org.elasticsearch.search.aggregations.bucket.filters.FiltersAggregationBuilder) WrapperQueryBuilder(org.elasticsearch.index.query.WrapperQueryBuilder) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 39 with XContentType

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());
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 40 with XContentType

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);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Aggregations

XContentType (org.elasticsearch.common.xcontent.XContentType)86 BytesReference (org.elasticsearch.common.bytes.BytesReference)50 XContentParser (org.elasticsearch.common.xcontent.XContentParser)45 Map (java.util.Map)20 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)20 HashMap (java.util.HashMap)14 IndexRequest (org.elasticsearch.action.index.IndexRequest)11 DocWriteRequest (org.elasticsearch.action.DocWriteRequest)10 IOException (java.io.IOException)9 DeleteRequest (org.elasticsearch.action.delete.DeleteRequest)8 ArrayList (java.util.ArrayList)6 Collections.emptyMap (java.util.Collections.emptyMap)6 List (java.util.List)6 ElasticsearchException (org.elasticsearch.ElasticsearchException)6 BulkRequest (org.elasticsearch.action.bulk.BulkRequest)6 UpdateRequest (org.elasticsearch.action.update.UpdateRequest)6 Collections.singletonMap (java.util.Collections.singletonMap)5 HttpEntity (org.apache.http.HttpEntity)5 ByteArrayEntity (org.apache.http.entity.ByteArrayEntity)5 GetRequest (org.elasticsearch.action.get.GetRequest)5