Search in sources :

Example 61 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        parsed = Option.fromXContent(parser);
        assertEquals(XContentParser.Token.END_OBJECT, parser.currentToken());
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getHighlighted(), parsed.getHighlighted());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.collateMatch(), parsed.collateMatch());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.Suggest.Suggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 62 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class CompletionSuggestionOptionTests method testFromXContent.

public void testFromXContent() throws IOException {
    Option option = createTestItem();
    XContentType xContentType = randomFrom(XContentType.values());
    boolean humanReadable = randomBoolean();
    BytesReference originalBytes = toXContent(option, xContentType, humanReadable);
    if (randomBoolean()) {
        try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
            originalBytes = shuffleXContent(parser, randomBoolean()).bytes();
        }
    }
    Option parsed;
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        parsed = Option.fromXContent(parser);
        assertNull(parser.nextToken());
    }
    assertEquals(option.getText(), parsed.getText());
    assertEquals(option.getHighlighted(), parsed.getHighlighted());
    assertEquals(option.getScore(), parsed.getScore(), Float.MIN_VALUE);
    assertEquals(option.collateMatch(), parsed.collateMatch());
    assertEquals(option.getContexts(), parsed.getContexts());
    assertToXContentEquivalent(originalBytes, toXContent(parsed, xContentType, humanReadable), xContentType);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) Option(org.elasticsearch.search.suggest.completion.CompletionSuggestion.Entry.Option) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 63 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class SuggestionTests method testFromXContentFailsWithoutTypeParam.

/**
     * test that we throw error if RestSearchAction.TYPED_KEYS_PARAM isn't set while rendering xContent
     */
public void testFromXContentFailsWithoutTypeParam() throws IOException {
    XContentType xContentType = randomFrom(XContentType.values());
    BytesReference originalBytes = toXContent(createTestItem(), xContentType, ToXContent.EMPTY_PARAMS, randomBoolean());
    try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
        ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser::getTokenLocation);
        ensureExpectedToken(XContentParser.Token.FIELD_NAME, parser.nextToken(), parser::getTokenLocation);
        ParsingException e = expectThrows(ParsingException.class, () -> Suggestion.fromXContent(parser));
        assertEquals("Cannot parse suggestion response without type information. " + "Set [typed_keys] parameter on the request to ensure the type information " + "is added to the response output", e.getMessage());
    }
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) ParsingException(org.elasticsearch.common.ParsingException) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 64 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class JsonXContentGenerator method writeRawField.

@Override
public final void writeRawField(String name, BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawField(name, content, contentType);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 65 with XContentType

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.common.xcontent.XContentType in project elasticsearch by elastic.

the class JsonXContentGenerator method writeRawValue.

@Override
public final void writeRawValue(BytesReference content) throws IOException {
    XContentType contentType = XContentFactory.xContentType(content);
    if (contentType == null) {
        throw new IllegalArgumentException("Can't write raw bytes whose xcontent-type can't be guessed");
    }
    writeRawValue(content, contentType);
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

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