Search in sources :

Example 1 with CustomFieldsVisitor

use of org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor in project elasticsearch by elastic.

the class FetchPhase method execute.

@Override
public void execute(SearchContext context) {
    final FieldsVisitor fieldsVisitor;
    Set<String> fieldNames = null;
    List<String> fieldNamePatterns = null;
    StoredFieldsContext storedFieldsContext = context.storedFieldsContext();
    if (storedFieldsContext == null) {
        // no fields specified, default to return source if no explicit indication
        if (!context.hasScriptFields() && !context.hasFetchSourceContext()) {
            context.fetchSourceContext(new FetchSourceContext(true));
        }
        fieldsVisitor = new FieldsVisitor(context.sourceRequested());
    } else if (storedFieldsContext.fetchFields() == false) {
        // disable stored fields entirely
        fieldsVisitor = null;
    } else {
        for (String fieldName : context.storedFieldsContext().fieldNames()) {
            if (fieldName.equals(SourceFieldMapper.NAME)) {
                FetchSourceContext fetchSourceContext = context.hasFetchSourceContext() ? context.fetchSourceContext() : FetchSourceContext.FETCH_SOURCE;
                context.fetchSourceContext(new FetchSourceContext(true, fetchSourceContext.includes(), fetchSourceContext.excludes()));
                continue;
            }
            if (Regex.isSimpleMatchPattern(fieldName)) {
                if (fieldNamePatterns == null) {
                    fieldNamePatterns = new ArrayList<>();
                }
                fieldNamePatterns.add(fieldName);
            } else {
                MappedFieldType fieldType = context.smartNameFieldType(fieldName);
                if (fieldType == null) {
                    // Only fail if we know it is a object field, missing paths / fields shouldn't fail.
                    if (context.getObjectMapper(fieldName) != null) {
                        throw new IllegalArgumentException("field [" + fieldName + "] isn't a leaf field");
                    }
                }
                if (fieldNames == null) {
                    fieldNames = new HashSet<>();
                }
                fieldNames.add(fieldName);
            }
        }
        boolean loadSource = context.sourceRequested();
        if (fieldNames == null && fieldNamePatterns == null) {
            // empty list specified, default to disable _source if no explicit indication
            fieldsVisitor = new FieldsVisitor(loadSource);
        } else {
            fieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.emptySet() : fieldNames, fieldNamePatterns == null ? Collections.emptyList() : fieldNamePatterns, loadSource);
        }
    }
    SearchHit[] hits = new SearchHit[context.docIdsToLoadSize()];
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    for (int index = 0; index < context.docIdsToLoadSize(); index++) {
        if (context.isCancelled()) {
            throw new TaskCancelledException("cancelled");
        }
        int docId = context.docIdsToLoad()[context.docIdsToLoadFrom() + index];
        int readerIndex = ReaderUtil.subIndex(docId, context.searcher().getIndexReader().leaves());
        LeafReaderContext subReaderContext = context.searcher().getIndexReader().leaves().get(readerIndex);
        int subDocId = docId - subReaderContext.docBase;
        final SearchHit searchHit;
        try {
            int rootDocId = findRootDocumentIfNested(context, subReaderContext, subDocId);
            if (rootDocId != -1) {
                searchHit = createNestedSearchHit(context, docId, subDocId, rootDocId, fieldNames, fieldNamePatterns, subReaderContext);
            } else {
                searchHit = createSearchHit(context, fieldsVisitor, docId, subDocId, subReaderContext);
            }
        } catch (IOException e) {
            throw ExceptionsHelper.convertToElastic(e);
        }
        hits[index] = searchHit;
        hitContext.reset(searchHit, subReaderContext, subDocId, context.searcher());
        for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }
    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        fetchSubPhase.hitsExecute(context, hits);
    }
    context.fetchResult().hits(new SearchHits(hits, context.queryResult().topDocs().totalHits, context.queryResult().topDocs().getMaxScore()));
}
Also used : FieldsVisitor(org.elasticsearch.index.fieldvisitor.FieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) SearchHit(org.elasticsearch.search.SearchHit) ArrayList(java.util.ArrayList) IOException(java.io.IOException) FetchSourceContext(org.elasticsearch.search.fetch.subphase.FetchSourceContext) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) MappedFieldType(org.elasticsearch.index.mapper.MappedFieldType) InnerHitsFetchSubPhase(org.elasticsearch.search.fetch.subphase.InnerHitsFetchSubPhase) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) TaskCancelledException(org.elasticsearch.tasks.TaskCancelledException) SearchHits(org.elasticsearch.search.SearchHits) HashSet(java.util.HashSet)

Example 2 with CustomFieldsVisitor

use of org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor in project elasticsearch by elastic.

the class FetchPhase method getSearchFields.

private Map<String, SearchHitField> getSearchFields(SearchContext context, int nestedSubDocId, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) {
    Map<String, SearchHitField> searchFields = null;
    if (context.hasStoredFields() && !context.storedFieldsContext().fieldNames().isEmpty()) {
        FieldsVisitor nestedFieldsVisitor = new CustomFieldsVisitor(fieldNames == null ? Collections.emptySet() : fieldNames, fieldNamePatterns == null ? Collections.emptyList() : fieldNamePatterns, false);
        if (nestedFieldsVisitor != null) {
            loadStoredFields(context, subReaderContext, nestedFieldsVisitor, nestedSubDocId);
            nestedFieldsVisitor.postProcess(context.mapperService());
            if (!nestedFieldsVisitor.fields().isEmpty()) {
                searchFields = new HashMap<>(nestedFieldsVisitor.fields().size());
                for (Map.Entry<String, List<Object>> entry : nestedFieldsVisitor.fields().entrySet()) {
                    searchFields.put(entry.getKey(), new SearchHitField(entry.getKey(), entry.getValue()));
                }
            }
        }
    }
    return searchFields;
}
Also used : FieldsVisitor(org.elasticsearch.index.fieldvisitor.FieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) SearchHitField(org.elasticsearch.search.SearchHitField) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with CustomFieldsVisitor

use of org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor 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)

Example 4 with CustomFieldsVisitor

use of org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor in project elasticsearch by elastic.

the class StoredNumericValuesTests method testBytesAndNumericRepresentation.

public void testBytesAndNumericRepresentation() throws Exception {
    IndexWriter writer = new IndexWriter(new RAMDirectory(), new IndexWriterConfig(Lucene.STANDARD_ANALYZER));
    String mapping = XContentFactory.jsonBuilder().startObject().startObject("type").startObject("properties").startObject("field1").field("type", "byte").field("store", true).endObject().startObject("field2").field("type", "short").field("store", true).endObject().startObject("field3").field("type", "integer").field("store", true).endObject().startObject("field4").field("type", "float").field("store", true).endObject().startObject("field5").field("type", "long").field("store", true).endObject().startObject("field6").field("type", "double").field("store", true).endObject().startObject("field7").field("type", "ip").field("store", true).endObject().startObject("field8").field("type", "ip").field("store", true).endObject().startObject("field9").field("type", "date").field("store", true).endObject().startObject("field10").field("type", "boolean").field("store", true).endObject().endObject().endObject().endObject().string();
    MapperService mapperService = createIndex("test").mapperService();
    DocumentMapper mapper = mapperService.merge("type", new CompressedXContent(mapping), MergeReason.MAPPING_UPDATE, false);
    ParsedDocument doc = mapper.parse("test", "type", "1", XContentFactory.jsonBuilder().startObject().field("field1", 1).field("field2", 1).field("field3", 1).field("field4", 1.1).startArray("field5").value(1).value(2).value(3).endArray().field("field6", 1.1).field("field7", "192.168.1.1").field("field8", "2001:db8::2:1").field("field9", "2016-04-05").field("field10", true).endObject().bytes());
    writer.addDocument(doc.rootDoc());
    DirectoryReader reader = DirectoryReader.open(writer);
    IndexSearcher searcher = new IndexSearcher(reader);
    CustomFieldsVisitor fieldsVisitor = new CustomFieldsVisitor(Collections.emptySet(), Collections.singletonList("field*"), false);
    searcher.doc(0, fieldsVisitor);
    fieldsVisitor.postProcess(mapperService);
    assertThat(fieldsVisitor.fields().size(), equalTo(10));
    assertThat(fieldsVisitor.fields().get("field1").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field1").get(0), equalTo((byte) 1));
    assertThat(fieldsVisitor.fields().get("field2").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field2").get(0), equalTo((short) 1));
    assertThat(fieldsVisitor.fields().get("field3").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field3").get(0), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field4").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field4").get(0), equalTo(1.1f));
    assertThat(fieldsVisitor.fields().get("field5").size(), equalTo(3));
    assertThat(fieldsVisitor.fields().get("field5").get(0), equalTo(1L));
    assertThat(fieldsVisitor.fields().get("field5").get(1), equalTo(2L));
    assertThat(fieldsVisitor.fields().get("field5").get(2), equalTo(3L));
    assertThat(fieldsVisitor.fields().get("field6").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field6").get(0), equalTo(1.1));
    assertThat(fieldsVisitor.fields().get("field7").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field7").get(0), equalTo("192.168.1.1"));
    assertThat(fieldsVisitor.fields().get("field8").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field8").get(0), equalTo("2001:db8::2:1"));
    assertThat(fieldsVisitor.fields().get("field9").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field9").get(0), equalTo("2016-04-05T00:00:00.000Z"));
    assertThat(fieldsVisitor.fields().get("field10").size(), equalTo(1));
    assertThat(fieldsVisitor.fields().get("field10").get(0), equalTo(true));
    reader.close();
    writer.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ParsedDocument(org.elasticsearch.index.mapper.ParsedDocument) IndexWriter(org.apache.lucene.index.IndexWriter) DirectoryReader(org.apache.lucene.index.DirectoryReader) CustomFieldsVisitor(org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor) DocumentMapper(org.elasticsearch.index.mapper.DocumentMapper) CompressedXContent(org.elasticsearch.common.compress.CompressedXContent) RAMDirectory(org.apache.lucene.store.RAMDirectory) MapperService(org.elasticsearch.index.mapper.MapperService) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

CustomFieldsVisitor (org.elasticsearch.index.fieldvisitor.CustomFieldsVisitor)4 ArrayList (java.util.ArrayList)2 FieldsVisitor (org.elasticsearch.index.fieldvisitor.FieldsVisitor)2 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Map (java.util.Map)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 IndexWriter (org.apache.lucene.index.IndexWriter)1 IndexWriterConfig (org.apache.lucene.index.IndexWriterConfig)1 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)1 IndexSearcher (org.apache.lucene.search.IndexSearcher)1 RAMDirectory (org.apache.lucene.store.RAMDirectory)1 CompressedXContent (org.elasticsearch.common.compress.CompressedXContent)1 DocumentMapper (org.elasticsearch.index.mapper.DocumentMapper)1 MappedFieldType (org.elasticsearch.index.mapper.MappedFieldType)1 MapperService (org.elasticsearch.index.mapper.MapperService)1 ParsedDocument (org.elasticsearch.index.mapper.ParsedDocument)1 SearchHit (org.elasticsearch.search.SearchHit)1