Search in sources :

Example 1 with XContentType

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

the class ObjectPath method createFromResponse.

public static ObjectPath createFromResponse(Response response) throws IOException {
    byte[] bytes = EntityUtils.toByteArray(response.getEntity());
    String contentType = response.getHeader("Content-Type");
    XContentType xContentType = XContentType.fromMediaTypeOrFormat(contentType);
    return ObjectPath.createFromXContent(xContentType.xContent(), new BytesArray(bytes));
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType)

Example 2 with XContentType

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

the class ShardGetService method innerGetLoadFromStoredFields.

private GetResult innerGetLoadFromStoredFields(String type, String id, String[] gFields, FetchSourceContext fetchSourceContext, Engine.GetResult get, MapperService mapperService) {
    Map<String, GetField> fields = null;
    BytesReference source = null;
    Versions.DocIdAndVersion docIdAndVersion = get.docIdAndVersion();
    FieldsVisitor fieldVisitor = buildFieldsVisitors(gFields, fetchSourceContext);
    if (fieldVisitor != null) {
        try {
            docIdAndVersion.context.reader().document(docIdAndVersion.docId, fieldVisitor);
        } catch (IOException e) {
            throw new ElasticsearchException("Failed to get type [" + type + "] and id [" + id + "]", e);
        }
        source = fieldVisitor.source();
        if (!fieldVisitor.fields().isEmpty()) {
            fieldVisitor.postProcess(mapperService);
            fields = new HashMap<>(fieldVisitor.fields().size());
            for (Map.Entry<String, List<Object>> entry : fieldVisitor.fields().entrySet()) {
                fields.put(entry.getKey(), new GetField(entry.getKey(), entry.getValue()));
            }
        }
    }
    DocumentMapper docMapper = mapperService.documentMapper(type);
    if (docMapper.parentFieldMapper().active()) {
        String parentId = ParentFieldSubFetchPhase.getParentId(docMapper.parentFieldMapper(), docIdAndVersion.context.reader(), docIdAndVersion.docId);
        if (fields == null) {
            fields = new HashMap<>(1);
        }
        fields.put(ParentFieldMapper.NAME, new GetField(ParentFieldMapper.NAME, Collections.singletonList(parentId)));
    }
    if (gFields != null && gFields.length > 0) {
        for (String field : gFields) {
            FieldMapper fieldMapper = docMapper.mappers().smartNameFieldMapper(field);
            if (fieldMapper == null) {
                if (docMapper.objectMappers().get(field) != null) {
                    // Only fail if we know it is a object field, missing paths / fields shouldn't fail.
                    throw new IllegalArgumentException("field [" + field + "] isn't a leaf field");
                }
            }
        }
    }
    if (!fetchSourceContext.fetchSource()) {
        source = null;
    } else if (fetchSourceContext.includes().length > 0 || fetchSourceContext.excludes().length > 0) {
        Map<String, Object> sourceAsMap;
        XContentType sourceContentType = null;
        // TODO: The source might parsed and available in the sourceLookup but that one uses unordered maps so different. Do we care?
        Tuple<XContentType, Map<String, Object>> typeMapTuple = XContentHelper.convertToMap(source, true);
        sourceContentType = typeMapTuple.v1();
        sourceAsMap = typeMapTuple.v2();
        sourceAsMap = XContentMapValues.filter(sourceAsMap, fetchSourceContext.includes(), fetchSourceContext.excludes());
        try {
            source = XContentFactory.contentBuilder(sourceContentType).map(sourceAsMap).bytes();
        } catch (IOException e) {
            throw new ElasticsearchException("Failed to get type [" + type + "] and id [" + id + "] with includes/excludes set", e);
        }
    }
    return new GetResult(shardId.getIndexName(), type, id, get.version(), get.exists(), source, fields);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) FieldsVisitor(org.elasticsearch.index.fieldvisitor.FieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) Versions(org.elasticsearch.common.lucene.uid.Versions) XContentType(org.elasticsearch.common.xcontent.XContentType) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) UidFieldMapper(org.elasticsearch.index.mapper.UidFieldMapper) FieldMapper(org.elasticsearch.index.mapper.FieldMapper) ParentFieldMapper(org.elasticsearch.index.mapper.ParentFieldMapper) SourceFieldMapper(org.elasticsearch.index.mapper.SourceFieldMapper) Tuple(org.elasticsearch.common.collect.Tuple)

Example 3 with XContentType

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

the class SourceFieldMapper method parseCreateField.

@Override
protected void parseCreateField(ParseContext context, List<IndexableField> fields) throws IOException {
    if (!enabled) {
        return;
    }
    if (!fieldType().stored()) {
        return;
    }
    BytesReference source = context.sourceToParse().source();
    // Percolate and tv APIs may not set the source and that is ok, because these APIs will not index any data
    if (source == null) {
        return;
    }
    if (filter != null) {
        // we don't update the context source if we filter, we want to keep it as is...
        Tuple<XContentType, Map<String, Object>> mapTuple = XContentHelper.convertToMap(source, true, context.sourceToParse().getXContentType());
        Map<String, Object> filteredSource = filter.apply(mapTuple.v2());
        BytesStreamOutput bStream = new BytesStreamOutput();
        XContentType contentType = mapTuple.v1();
        XContentBuilder builder = XContentFactory.contentBuilder(contentType, bStream).map(filteredSource);
        builder.close();
        source = bStream.bytes();
    }
    BytesRef ref = source.toBytesRef();
    fields.add(new StoredField(fieldType().name(), ref.bytes, ref.offset, ref.length));
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) StoredField(org.apache.lucene.document.StoredField) XContentType(org.elasticsearch.common.xcontent.XContentType) Map(java.util.Map) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder) BytesRef(org.apache.lucene.util.BytesRef)

Example 4 with XContentType

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

the class RestRequest method contentOrSourceParamParser.

/**
     * A parser for the contents of this request if it has contents, otherwise a parser for the {@code source} parameter if there is one,
     * otherwise throws an {@link ElasticsearchParseException}. Use {@link #withContentOrSourceParamParserOrNull(CheckedConsumer)} instead
     * if you need to handle the absence request content gracefully.
     */
public final XContentParser contentOrSourceParamParser() throws IOException {
    Tuple<XContentType, BytesReference> tuple = contentOrSourceParam();
    BytesReference content = tuple.v2();
    if (content.length() == 0) {
        throw new ElasticsearchParseException("Body required");
    }
    return tuple.v1().xContent().createParser(xContentRegistry, content);
}
Also used : BytesReference(org.elasticsearch.common.bytes.BytesReference) XContentType(org.elasticsearch.common.xcontent.XContentType) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException)

Example 5 with XContentType

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

the class RestRequest method contentOrSourceParam.

/**
     * Get the content of the request or the contents of the {@code source} param. Prefer {@link #contentOrSourceParamParser()} or
     * {@link #withContentOrSourceParamParserOrNull(CheckedConsumer)} if you need a parser.
     */
public final Tuple<XContentType, BytesReference> contentOrSourceParam() {
    if (hasContent()) {
        if (xContentType.get() == null) {
            throw new IllegalStateException("unknown content type");
        }
        return new Tuple<>(xContentType.get(), content());
    }
    String source = param("source");
    String typeParam = param("source_content_type");
    if (source != null && typeParam != null) {
        BytesArray bytes = new BytesArray(source);
        final XContentType xContentType = parseContentType(Collections.singletonList(typeParam));
        if (xContentType == null) {
            throw new IllegalStateException("Unknown value for source_content_type [" + typeParam + "]");
        }
        return new Tuple<>(xContentType, bytes);
    }
    return new Tuple<>(XContentType.JSON, BytesArray.EMPTY);
}
Also used : BytesArray(org.elasticsearch.common.bytes.BytesArray) XContentType(org.elasticsearch.common.xcontent.XContentType) Tuple(org.elasticsearch.common.collect.Tuple)

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