Search in sources :

Example 66 with XContentType

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

the class RestPutPipelineAction method prepareRequest.

@Override
public RestChannelConsumer prepareRequest(RestRequest restRequest, NodeClient client) throws IOException {
    Tuple<XContentType, BytesReference> sourceTuple = restRequest.contentOrSourceParam();
    PutPipelineRequest request = new PutPipelineRequest(restRequest.param("id"), sourceTuple.v2(), sourceTuple.v1());
    request.masterNodeTimeout(restRequest.paramAsTime("master_timeout", request.masterNodeTimeout()));
    request.timeout(restRequest.paramAsTime("timeout", request.timeout()));
    return channel -> client.admin().cluster().putPipeline(request, new AcknowledgedRestListener<>(channel));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) BaseRestHandler(org.elasticsearch.rest.BaseRestHandler) Settings(org.elasticsearch.common.settings.Settings) XContentType(org.elasticsearch.common.xcontent.XContentType) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest) RestRequest(org.elasticsearch.rest.RestRequest) NodeClient(org.elasticsearch.client.node.NodeClient) IOException(java.io.IOException) RestController(org.elasticsearch.rest.RestController) BytesReference(org.elasticsearch.common.bytes.BytesReference) Tuple(org.elasticsearch.common.collect.Tuple) AcknowledgedRestListener(org.elasticsearch.rest.action.AcknowledgedRestListener) XContentType(org.elasticsearch.common.xcontent.XContentType) PutPipelineRequest(org.elasticsearch.action.ingest.PutPipelineRequest)

Example 67 with XContentType

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

the class GetResultTests method testGetSourceAsBytes.

public void testGetSourceAsBytes() {
    XContentType xContentType = randomFrom(XContentType.values());
    Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
    GetResult getResult = tuple.v1();
    if (getResult.isExists() && getResult.isSourceEmpty() == false) {
        assertNotNull(getResult.sourceRef());
    } else {
        assertNull(getResult.sourceRef());
    }
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 68 with XContentType

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

the class SourceLookup method loadSourceIfNeeded.

private Map<String, Object> loadSourceIfNeeded() {
    if (source != null) {
        return source;
    }
    if (sourceAsBytes != null) {
        Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(sourceAsBytes);
        sourceContentType = tuple.v1();
        source = tuple.v2();
        return source;
    }
    try {
        FieldsVisitor sourceFieldVisitor = new FieldsVisitor(true);
        reader.document(docId, sourceFieldVisitor);
        BytesReference source = sourceFieldVisitor.source();
        if (source == null) {
            this.source = emptyMap();
            this.sourceContentType = null;
        } else {
            Tuple<XContentType, Map<String, Object>> tuple = sourceAsMapAndType(source);
            this.sourceContentType = tuple.v1();
            this.source = tuple.v2();
        }
    } catch (Exception e) {
        throw new ElasticsearchParseException("failed to parse / load source", e);
    }
    return this.source;
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) FieldsVisitor(org.elasticsearch.index.fieldvisitor.FieldsVisitor) XContentType(org.elasticsearch.common.xcontent.XContentType) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Collections.emptyMap(java.util.Collections.emptyMap) Map(java.util.Map) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 69 with XContentType

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

the class XContentHelperTests method testToXContent.

public void testToXContent() throws IOException {
    final XContentType xContentType = randomFrom(XContentType.values());
    final ToXContent toXContent;
    final boolean error;
    if (randomBoolean()) {
        if (randomBoolean()) {
            error = false;
            toXContent = (builder, params) -> builder.field("field", "value");
        } else {
            error = true;
            toXContent = (builder, params) -> builder.startObject().field("field", "value").endObject();
        }
    } else {
        if (randomBoolean()) {
            error = false;
            toXContent = (ToXContentObject) (builder, params) -> builder.startObject().field("field", "value").endObject();
        } else {
            error = true;
            toXContent = (ToXContentObject) (builder, params) -> builder.field("field", "value");
        }
    }
    if (error) {
        expectThrows(IOException.class, () -> XContentHelper.toXContent(toXContent, xContentType, randomBoolean()));
    } else {
        BytesReference bytes = XContentHelper.toXContent(toXContent, xContentType, randomBoolean());
        try (XContentParser parser = xContentType.xContent().createParser(NamedXContentRegistry.EMPTY, bytes)) {
            assertEquals(XContentParser.Token.START_OBJECT, parser.nextToken());
            assertEquals(XContentParser.Token.FIELD_NAME, parser.nextToken());
            assertTrue(parser.nextToken().isValue());
            assertEquals("value", parser.text());
            assertEquals(XContentParser.Token.END_OBJECT, parser.nextToken());
            assertNull(parser.nextToken());
        }
    }
}
Also used : ToXContent(org.elasticsearch.common.xcontent.ToXContent) Arrays(java.util.Arrays) XContentType(org.elasticsearch.common.xcontent.XContentType) ToXContent(org.elasticsearch.common.xcontent.ToXContent) Matchers(org.hamcrest.Matchers) IOException(java.io.IOException) HashMap(java.util.HashMap) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentHelper(org.elasticsearch.common.xcontent.XContentHelper) XContentParser(org.elasticsearch.common.xcontent.XContentParser) List(java.util.List) Map(java.util.Map) ToXContentObject(org.elasticsearch.common.xcontent.ToXContentObject) ESTestCase(org.elasticsearch.test.ESTestCase) NamedXContentRegistry(org.elasticsearch.common.xcontent.NamedXContentRegistry) BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 70 with XContentType

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

the class XContentMapValuesTests method testThatFilterIncludesEmptyObjectWhenUsingExcludes.

public void testThatFilterIncludesEmptyObjectWhenUsingExcludes() throws Exception {
    XContentBuilder builder = XContentFactory.jsonBuilder().startObject().startObject("obj").endObject().endObject();
    Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(builder.bytes(), true, builder.contentType());
    Map<String, Object> filteredSource = XContentMapValues.filter(mapTuple.v2(), Strings.EMPTY_ARRAY, new String[] { "nonExistingField" });
    assertThat(mapTuple.v2(), equalTo(filteredSource));
}
Also used : XContentType(org.elasticsearch.common.xcontent.XContentType) Collections.emptyMap(java.util.Collections.emptyMap) HashMap(java.util.HashMap) Map(java.util.Map) Collections.singletonMap(java.util.Collections.singletonMap) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

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