Search in sources :

Example 6 with SourceLookup

use of org.elasticsearch.search.lookup.SourceLookup in project elasticsearch by elastic.

the class FetchSourceSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if (context.sourceRequested() == false) {
        return;
    }
    SourceLookup source = context.lookup().source();
    FetchSourceContext fetchSourceContext = context.fetchSourceContext();
    assert fetchSourceContext.fetchSource();
    if (fetchSourceContext.includes().length == 0 && fetchSourceContext.excludes().length == 0) {
        hitContext.hit().sourceRef(source.internalSourceRef());
        return;
    }
    if (source.internalSourceRef() == null) {
        throw new IllegalArgumentException("unable to fetch fields from _source field: _source is disabled in the mappings " + "for index [" + context.indexShard().shardId().getIndexName() + "]");
    }
    final Object value = source.filter(fetchSourceContext);
    try {
        final int initialCapacity = Math.min(1024, source.internalSourceRef().length());
        BytesStreamOutput streamOutput = new BytesStreamOutput(initialCapacity);
        XContentBuilder builder = new XContentBuilder(source.sourceContentType().xContent(), streamOutput);
        builder.value(value);
        hitContext.hit().sourceRef(builder.bytes());
    } catch (IOException e) {
        throw new ElasticsearchException("Error filtering source", e);
    }
}
Also used : SourceLookup(org.elasticsearch.search.lookup.SourceLookup) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) BytesStreamOutput(org.elasticsearch.common.io.stream.BytesStreamOutput) XContentBuilder(org.elasticsearch.common.xcontent.XContentBuilder)

Example 7 with SourceLookup

use of org.elasticsearch.search.lookup.SourceLookup in project elasticsearch by elastic.

the class HighlightUtils method loadFieldValues.

/**
     * Load field values for highlighting.
     */
public static List<Object> loadFieldValues(SearchContextHighlight.Field field, FieldMapper mapper, SearchContext searchContext, FetchSubPhase.HitContext hitContext) throws IOException {
    //percolator needs to always load from source, thus it sets the global force source to true
    boolean forceSource = searchContext.highlight().forceSource(field);
    List<Object> textsToHighlight;
    if (!forceSource && mapper.fieldType().stored()) {
        CustomFieldsVisitor fieldVisitor = new CustomFieldsVisitor(singleton(mapper.fieldType().name()), false);
        hitContext.reader().document(hitContext.docId(), fieldVisitor);
        textsToHighlight = fieldVisitor.fields().get(mapper.fieldType().name());
        if (textsToHighlight == null) {
            // Can happen if the document doesn't have the field to highlight
            textsToHighlight = Collections.emptyList();
        }
    } else {
        SourceLookup sourceLookup = searchContext.lookup().source();
        sourceLookup.setSegmentAndDocument(hitContext.readerContext(), hitContext.docId());
        textsToHighlight = sourceLookup.extractRawValues(mapper.fieldType().name());
    }
    assert textsToHighlight != null;
    return textsToHighlight;
}
Also used : SourceLookup(org.elasticsearch.search.lookup.SourceLookup) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor)

Aggregations

SourceLookup (org.elasticsearch.search.lookup.SourceLookup)7 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 List (java.util.List)2 Map (java.util.Map)2 Field (org.apache.lucene.document.Field)2 TextField (org.apache.lucene.document.TextField)2 ElasticsearchException (org.elasticsearch.ElasticsearchException)2 BytesReference (org.elasticsearch.common.bytes.BytesReference)2 BytesStreamOutput (org.elasticsearch.common.io.stream.BytesStreamOutput)2 XContentBuilder (org.elasticsearch.common.xcontent.XContentBuilder)2 CustomFieldsVisitor (org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor)2 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)2 SearchHit (org.elasticsearch.search.SearchHit)2 SearchHitField (org.elasticsearch.search.SearchHitField)2 Text (org.elasticsearch.common.text.Text)1 XContentType (org.elasticsearch.common.xcontent.XContentType)1 FieldsVisitor (org.elasticsearch.index.fieldvisitor.FieldsVisitor)1 GetField (org.elasticsearch.index.get.GetField)1