org.elasticsearch.search.internal.InternalSearchHit Java Examples

The following examples show how to use org.elasticsearch.search.internal.InternalSearchHit. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: PercolateContext.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
public void initialize(Engine.Searcher docSearcher, ParsedDocument parsedDocument) {
    this.docSearcher = docSearcher;

    IndexReader indexReader = docSearcher.reader();
    LeafReaderContext atomicReaderContext = indexReader.leaves().get(0);
    LeafSearchLookup leafLookup = lookup().getLeafSearchLookup(atomicReaderContext);
    leafLookup.setDocument(0);
    leafLookup.source().setSource(parsedDocument.source());

    Map<String, SearchHitField> fields = new HashMap<>();
    for (IndexableField field : parsedDocument.rootDoc().getFields()) {
        fields.put(field.name(), new InternalSearchHitField(field.name(), Collections.emptyList()));
    }
    hitContext().reset(
            new InternalSearchHit(0, "unknown", new Text(parsedDocument.type()), fields),
            atomicReaderContext, 0, docSearcher.searcher()
    );
}
 
Example #2
Source File: IndexShard.java    From Elasticsearch with Apache License 2.0 6 votes vote down vote up
private void reindex(QueryFetchSearchResult hits, String index, String type) {
    logger.debug("Reindex: [index:{}, type:{}]", index, type);

    if (state == IndexShardState.STARTED) {
        for (InternalSearchHit hit : hits.fetchResult().hits().internalHits()) {
            // no difference between PRIMARY and REPLICA
            SourceToParse source = SourceToParse.source(SourceToParse.Origin.REPLICA, hit.sourceRef())
                    .index(index).type(type).id(hit.id());
            if (hit.field(ParentFieldMapper.NAME).getValue() != null) {
                source.parent((String) hit.field(ParentFieldMapper.NAME).getValue());
            }
            if (hit.field(TimestampFieldMapper.NAME).getValue() != null) {
                source.timestamp((long) hit.field(TimestampFieldMapper.NAME).getValue());
            }
            long version = 0;
            if (hit.field(VersionFieldMapper.NAME).getValue() != null) {
                version = (long) hit.field(VersionFieldMapper.NAME).getValue();
            }
            Engine.Index indexOp = prepareIndex(docMapper(source.type()), source, version, VersionType.EXTERNAL_GTE, Engine.Operation.Origin.RECOVERY, state != IndexShardState.STARTED);
            indexOp.setReindex(true);
            index(indexOp);
        }
    }
}
 
Example #3
Source File: TopHitsAggregator.java    From Elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public InternalAggregation buildAggregation(long owningBucketOrdinal) {
    TopDocsAndLeafCollector topDocsCollector = topDocsCollectors.get(owningBucketOrdinal);
    final InternalTopHits topHits;
    if (topDocsCollector == null) {
        topHits = buildEmptyAggregation();
    } else {
        final TopDocs topDocs = topDocsCollector.topLevelCollector.topDocs();

        subSearchContext.queryResult().topDocs(topDocs);
        int[] docIdsToLoad = new int[topDocs.scoreDocs.length];
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            docIdsToLoad[i] = topDocs.scoreDocs[i].doc;
        }
        subSearchContext.docIdsToLoad(docIdsToLoad, 0, docIdsToLoad.length);
        fetchPhase.execute(subSearchContext);
        FetchSearchResult fetchResult = subSearchContext.fetchResult();
        InternalSearchHit[] internalHits = fetchResult.fetchResult().hits().internalHits();
        for (int i = 0; i < internalHits.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            InternalSearchHit searchHitFields = internalHits[i];
            searchHitFields.shard(subSearchContext.shardTarget());
            searchHitFields.score(scoreDoc.score);
            if (scoreDoc instanceof FieldDoc) {
                FieldDoc fieldDoc = (FieldDoc) scoreDoc;
                searchHitFields.sortValues(fieldDoc.fields);
            }
        }
        topHits = new InternalTopHits(name, subSearchContext.from(), subSearchContext.size(), topDocs, fetchResult.hits(), pipelineAggregators(),
                metaData());
    }
    return topHits;
}
 
Example #4
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private InternalSearchHit[] parseInternalSearchHit(Map<String,?> map) {
    // public InternalSearchHit(int docId, String id, Text type, Map<String, SearchHitField> fields)
    List<InternalSearchHit> list = new LinkedList<>();
    Map<String,?> hitmap = (Map<String, ?>) map.get(HITS);
    List<Map<String,?>> hits = (List<Map<String, ?>>) hitmap.get(HITS);
    return list.toArray(new InternalSearchHit[list.size()]);
}
 
Example #5
Source File: HttpSearchAction.java    From elasticsearch-helper with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private InternalSearchHits parseInternalSearchHits(Map<String,?> map) {
    // InternalSearchHits(InternalSearchHit[] hits, long totalHits, float maxScore)
    InternalSearchHit[] internalSearchHits = parseInternalSearchHit(map);
    map = (Map<String, ?>) map.get(HITS);
    long totalHits = map.containsKey(TOTAL) ? (Integer)map.get(TOTAL) : -1L;
    double maxScore = map.containsKey(MAXSCORE) ? (Double)map.get(MAXSCORE) : 0.0f;
    return new InternalSearchHits(internalSearchHits, totalHits, (float)maxScore);
}
 
Example #6
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #7
Source File: ElasticSearchService.java    From sakai with Educational Community License v2.0 5 votes vote down vote up
@Override
public SearchResponse search(String searchTerms, List<String> references, List<String> siteIds, int start, int end, Map<String,String> additionalSearchInfromation) {
    return new SearchResponse(
            new InternalSearchResponse(new InternalSearchHits(new InternalSearchHit[0], 0, 0.0f), new InternalFacets(Collections.EMPTY_LIST), new InternalAggregations(Collections.EMPTY_LIST), new Suggest(), false, false),
            "no-op",
            1,
            1,
            1,
            new ShardSearchFailure[0]
    );
}
 
Example #8
Source File: ExportCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    fieldsVisitor.reset();
    currentReader.document(doc, fieldsVisitor);

    Map<String, SearchHitField> searchFields = null;
    if (fieldsVisitor.fields() != null) {
        searchFields = new HashMap<String, SearchHitField>(fieldsVisitor.fields().size());
        for (Map.Entry<String, List<Object>> entry : fieldsVisitor.fields().entrySet()) {
            searchFields.put(entry.getKey(), new InternalSearchHitField(entry.getKey(), entry.getValue()));
        }
    }

    DocumentMapper documentMapper = context.mapperService()
            .documentMapper(fieldsVisitor.uid().type());
    Text typeText;
    if (documentMapper == null) {
        typeText = new StringAndBytesText(fieldsVisitor.uid().type());
    } else {
        typeText = documentMapper.typeText();
    }

    InternalSearchHit searchHit = new InternalSearchHit(doc,
            fieldsVisitor.uid().id(), typeText,
            sourceRequested ? fieldsVisitor.source() : null,
            searchFields);

    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
        if (fetchSubPhase.hitExecutionNeeded(context)) {
            hitContext.reset(searchHit, arc, doc, context.searcher().getIndexReader(), doc, fieldsVisitor);
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }

    searchHit.shardTarget(context.shardTarget());
    exportFields.hit(searchHit);
    BytesStreamOutput os = new BytesStreamOutput();
    XContentBuilder builder = new XContentBuilder(XContentFactory.xContent(XContentType.JSON), os);
    exportFields.toXContent(builder, ToXContent.EMPTY_PARAMS);
    builder.flush();
    BytesReference bytes = os.bytes();
    out.write(bytes.array(), bytes.arrayOffset(), bytes.length());
    out.write('\n');
    out.flush();
    numExported++;
}
 
Example #9
Source File: WriterCollector.java    From elasticsearch-inout-plugin with Apache License 2.0 4 votes vote down vote up
@Override
public void collect(int doc) throws IOException {
    fieldsVisitor.reset();
    currentReader.document(doc, fieldsVisitor);

    Map<String, SearchHitField> searchFields = null;
    if (fieldsVisitor.fields() != null) {
        searchFields = new HashMap<String, SearchHitField>(
                fieldsVisitor.fields().size());
        for (Map.Entry<String, List<Object>> entry : fieldsVisitor
                .fields().entrySet()) {
            searchFields.put(entry.getKey(), new InternalSearchHitField(
                    entry.getKey(), entry.getValue()));
        }
    }

    DocumentMapper documentMapper = context.mapperService().documentMapper(
            fieldsVisitor.uid().type());
    Text typeText;
    if (documentMapper == null) {
        typeText = new StringAndBytesText(fieldsVisitor.uid().type());
    } else {
        typeText = documentMapper.typeText();
    }

    InternalSearchHit searchHit = new InternalSearchHit(doc,
            fieldsVisitor.uid().id(), typeText,
            sourceRequested ? fieldsVisitor.source() : null, searchFields);

    // it looks like it is safe to reuse the HitContext,
    // the cache is only used by the highlighter which we do not use.
    FetchSubPhase.HitContext hitContext = new FetchSubPhase.HitContext();
    for (FetchSubPhase fetchSubPhase : fetchSubPhases) {
        if (fetchSubPhase.hitExecutionNeeded(context)) {
            hitContext.reset(searchHit, arc, doc,
                    context.searcher().getIndexReader(), doc,
                    fieldsVisitor);
            fetchSubPhase.hitExecute(context, hitContext);
        }
    }
    searchHit.shardTarget(context.shardTarget());
    collectHit(searchHit);
    numExported++;
}
 
Example #10
Source File: HighlightPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #11
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean isChildHit(InternalSearchHit hit) {
    DocumentMapper hitDocumentMapper = mapperService.documentMapper(hit.type());
    return documentMapper.type().equals(hitDocumentMapper.parentFieldMapper().type());
}
 
Example #12
Source File: InnerHitsContext.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean isParentHit(InternalSearchHit hit) {
    return hit.type().equals(documentMapper.parentFieldMapper().type());
}
 
Example #13
Source File: InnerHitsFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #14
Source File: FetchSourceSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #15
Source File: MatchedQueriesFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #16
Source File: FetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public InternalSearchHit hit() {
    return hit;
}
 
Example #17
Source File: FetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
public void reset(InternalSearchHit hit, LeafReaderContext context, int docId, IndexSearcher searcher) {
    this.hit = hit;
    this.readerContext = context;
    this.docId = docId;
    this.searcher = searcher;
}
 
Example #18
Source File: VersionFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #19
Source File: FieldDataFieldsFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #20
Source File: ExplainFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #21
Source File: ScriptFieldsFetchSubPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
@Override
public void hitsExecute(SearchContext context, InternalSearchHit[] hits) {
}
 
Example #22
Source File: FetchPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private InternalSearchHit.InternalNestedIdentity getInternalNestedIdentity(SearchContext context, int nestedSubDocId, LeafReaderContext subReaderContext, DocumentMapper documentMapper, ObjectMapper nestedObjectMapper) throws IOException {
    int currentParent = nestedSubDocId;
    ObjectMapper nestedParentObjectMapper;
    ObjectMapper current = nestedObjectMapper;
    String originalName = nestedObjectMapper.name();
    InternalSearchHit.InternalNestedIdentity nestedIdentity = null;
    do {
        Query parentFilter;
        nestedParentObjectMapper = documentMapper.findParentObjectMapper(current);
        if (nestedParentObjectMapper != null) {
            if (nestedParentObjectMapper.nested().isNested() == false) {
                current = nestedParentObjectMapper;
                continue;
            }
            parentFilter = nestedParentObjectMapper.nestedTypeFilter();
        } else {
            parentFilter = Queries.newNonNestedFilter();
        }

        Query childFilter = nestedObjectMapper.nestedTypeFilter();
        if (childFilter == null) {
            current = nestedParentObjectMapper;
            continue;
        }
        final Weight childWeight = context.searcher().createNormalizedWeight(childFilter, false);
        Scorer childScorer = childWeight.scorer(subReaderContext);
        if (childScorer == null) {
            current = nestedParentObjectMapper;
            continue;
        }
        DocIdSetIterator childIter = childScorer.iterator();

        BitSet parentBits = context.bitsetFilterCache().getBitSetProducer(parentFilter).getBitSet(subReaderContext);

        int offset = 0;
        int nextParent = parentBits.nextSetBit(currentParent);
        for (int docId = childIter.advance(currentParent + 1); docId < nextParent && docId != DocIdSetIterator.NO_MORE_DOCS; docId = childIter.nextDoc()) {
            offset++;
        }
        currentParent = nextParent;
        current = nestedObjectMapper = nestedParentObjectMapper;
        int currentPrefix = current == null ? 0 : current.name().length() + 1;
        nestedIdentity = new InternalSearchHit.InternalNestedIdentity(originalName.substring(currentPrefix), offset, nestedIdentity);
        if (current != null) {
            originalName = current.name();
        }
    } while (current != null);
    return nestedIdentity;
}
 
Example #23
Source File: FetchPhase.java    From Elasticsearch with Apache License 2.0 4 votes vote down vote up
private InternalSearchHit createNestedSearchHit(SearchContext context, int nestedTopDocId, int nestedSubDocId, int rootSubDocId, List<String> extractFieldNames, boolean loadAllStored, Set<String> fieldNames, List<String> fieldNamePatterns, LeafReaderContext subReaderContext) throws IOException {
    // Also if highlighting is requested on nested documents we need to fetch the _source from the root document,
    // otherwise highlighting will attempt to fetch the _source from the nested doc, which will fail,
    // because the entire _source is only stored with the root document.
    final FieldsVisitor rootFieldsVisitor = new FieldsVisitor(context.sourceRequested() || extractFieldNames != null || context.highlight() != null);
    loadStoredFields(context, subReaderContext, rootFieldsVisitor, rootSubDocId);
    rootFieldsVisitor.postProcess(context.mapperService());

    Map<String, SearchHitField> searchFields = getSearchFields(context, nestedSubDocId, loadAllStored, fieldNames, fieldNamePatterns, subReaderContext);
    DocumentMapper documentMapper = context.mapperService().documentMapper(rootFieldsVisitor.uid().type());
    SourceLookup sourceLookup = context.lookup().source();
    sourceLookup.setSegmentAndDocument(subReaderContext, nestedSubDocId);

    ObjectMapper nestedObjectMapper = documentMapper.findNestedObjectMapper(nestedSubDocId, context, subReaderContext);
    assert nestedObjectMapper != null;
    InternalSearchHit.InternalNestedIdentity nestedIdentity = getInternalNestedIdentity(context, nestedSubDocId, subReaderContext, documentMapper, nestedObjectMapper);

    BytesReference source = rootFieldsVisitor.source();
    if (source != null) {
        Tuple<XContentType, Map<String, Object>> tuple = XContentHelper.convertToMap(source, true);
        Map<String, Object> sourceAsMap = tuple.v2();

        List<Map<String, Object>> nestedParsedSource;
        SearchHit.NestedIdentity nested = nestedIdentity;
        do {
            Object extractedValue = XContentMapValues.extractValue(nested.getField().string(), sourceAsMap);
            if (extractedValue == null) {
                // The nested objects may not exist in the _source, because it was filtered because of _source filtering
                break;
            } else if (extractedValue instanceof List) {
                // nested field has an array value in the _source
                nestedParsedSource = (List<Map<String, Object>>) extractedValue;
            } else if (extractedValue instanceof Map) {
                // nested field has an object value in the _source. This just means the nested field has just one inner object, which is valid, but uncommon.
                nestedParsedSource = Collections.singletonList((Map<String, Object>) extractedValue);
            } else {
                throw new IllegalStateException("extracted source isn't an object or an array");
            }
            sourceAsMap = nestedParsedSource.get(nested.getOffset());
            nested = nested.getChild();
        } while (nested != null);

        context.lookup().source().setSource(sourceAsMap);
        XContentType contentType = tuple.v1();
        BytesReference nestedSource = contentBuilder(contentType).map(sourceAsMap).bytes();
        context.lookup().source().setSource(nestedSource);
        context.lookup().source().setSourceContentType(contentType);
    }

    InternalSearchHit searchHit = new InternalSearchHit(nestedTopDocId, rootFieldsVisitor.uid().id(), documentMapper.typeText(), nestedIdentity, searchFields);
    if (extractFieldNames != null) {
        for (String extractFieldName : extractFieldNames) {
            List<Object> values = context.lookup().source().extractRawValues(extractFieldName);
            if (!values.isEmpty()) {
                if (searchHit.fieldsOrNull() == null) {
                    searchHit.fields(new HashMap<String, SearchHitField>(2));
                }

                SearchHitField hitField = searchHit.fields().get(extractFieldName);
                if (hitField == null) {
                    hitField = new InternalSearchHitField(extractFieldName, new ArrayList<>(2));
                    searchHit.fields().put(extractFieldName, hitField);
                }
                for (Object value : values) {
                    hitField.values().add(value);
                }
            }
        }
    }

    return searchHit;
}
 
Example #24
Source File: FetchSubPhase.java    From Elasticsearch with Apache License 2.0 votes vote down vote up
void hitsExecute(SearchContext context, InternalSearchHit[] hits);