Search in sources :

Example 91 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class InternalTopHits method doReduce.

@Override
public InternalAggregation doReduce(List<InternalAggregation> aggregations, ReduceContext reduceContext) {
    final SearchHits[] shardHits = new SearchHits[aggregations.size()];
    final int from;
    final int size;
    if (reduceContext.isFinalReduce()) {
        from = this.from;
        size = this.size;
    } else {
        // if we are not in the final reduce we need to ensure we maintain all possible elements during reduce
        // hence for pagination we need to maintain all hits until we are in the final phase.
        from = 0;
        size = this.from + this.size;
    }
    final TopDocs reducedTopDocs;
    final TopDocs[] shardDocs;
    if (topDocs instanceof TopFieldDocs) {
        Sort sort = new Sort(((TopFieldDocs) topDocs).fields);
        shardDocs = new TopFieldDocs[aggregations.size()];
        for (int i = 0; i < shardDocs.length; i++) {
            InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i);
            shardDocs[i] = topHitsAgg.topDocs;
            shardHits[i] = topHitsAgg.searchHits;
        }
        reducedTopDocs = TopDocs.merge(sort, from, size, (TopFieldDocs[]) shardDocs, true);
    } else {
        shardDocs = new TopDocs[aggregations.size()];
        for (int i = 0; i < shardDocs.length; i++) {
            InternalTopHits topHitsAgg = (InternalTopHits) aggregations.get(i);
            shardDocs[i] = topHitsAgg.topDocs;
            shardHits[i] = topHitsAgg.searchHits;
        }
        reducedTopDocs = TopDocs.merge(from, size, shardDocs, true);
    }
    final int[] tracker = new int[shardHits.length];
    SearchHit[] hits = new SearchHit[reducedTopDocs.scoreDocs.length];
    for (int i = 0; i < reducedTopDocs.scoreDocs.length; i++) {
        ScoreDoc scoreDoc = reducedTopDocs.scoreDocs[i];
        int position;
        do {
            position = tracker[scoreDoc.shardIndex]++;
        } while (shardDocs[scoreDoc.shardIndex].scoreDocs[position] != scoreDoc);
        hits[i] = shardHits[scoreDoc.shardIndex].getAt(position);
    }
    return new InternalTopHits(name, this.from, this.size, reducedTopDocs, new SearchHits(hits, reducedTopDocs.totalHits, reducedTopDocs.getMaxScore()), pipelineAggregators(), getMetaData());
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) SearchHit(org.elasticsearch.search.SearchHit) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) Sort(org.apache.lucene.search.Sort) SearchHits(org.elasticsearch.search.SearchHits) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 92 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class TopHitsAggregator method buildAggregation.

@Override
public InternalAggregation buildAggregation(long owningBucketOrdinal) {
    TopDocsAndLeafCollector topDocsCollector = topDocsCollectors.get(owningBucketOrdinal);
    final InternalTopHits topHits;
    if (topDocsCollector == null) {
        topHits = buildEmptyAggregation();
    } else {
        TopDocs topDocs = topDocsCollector.topLevelCollector.topDocs();
        if (subSearchContext.sort() == null) {
            for (RescoreSearchContext ctx : context().rescore()) {
                try {
                    topDocs = ctx.rescorer().rescore(topDocs, context, ctx);
                } catch (IOException e) {
                    throw new ElasticsearchException("Rescore TopHits Failed", e);
                }
            }
        }
        subSearchContext.queryResult().topDocs(topDocs, subSearchContext.sort() == null ? null : subSearchContext.sort().formats);
        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();
        SearchHit[] internalHits = fetchResult.fetchResult().hits().internalHits();
        for (int i = 0; i < internalHits.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            SearchHit searchHitFields = internalHits[i];
            searchHitFields.shard(subSearchContext.shardTarget());
            searchHitFields.score(scoreDoc.score);
            if (scoreDoc instanceof FieldDoc) {
                FieldDoc fieldDoc = (FieldDoc) scoreDoc;
                searchHitFields.sortValues(fieldDoc.fields, subSearchContext.sort().formats);
            }
        }
        topHits = new InternalTopHits(name, subSearchContext.from(), subSearchContext.size(), topDocs, fetchResult.hits(), pipelineAggregators(), metaData());
    }
    return topHits;
}
Also used : RescoreSearchContext(org.elasticsearch.search.rescore.RescoreSearchContext) SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) IOException(java.io.IOException) ElasticsearchException(org.elasticsearch.ElasticsearchException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs)

Example 93 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class InnerHitsFetchSubPhase method hitExecute.

@Override
public void hitExecute(SearchContext context, HitContext hitContext) {
    if ((context.innerHits() != null && context.innerHits().getInnerHits().size() > 0) == false) {
        return;
    }
    Map<String, SearchHits> results = new HashMap<>();
    for (Map.Entry<String, InnerHitsContext.BaseInnerHits> entry : context.innerHits().getInnerHits().entrySet()) {
        InnerHitsContext.BaseInnerHits innerHits = entry.getValue();
        TopDocs topDocs;
        try {
            topDocs = innerHits.topDocs(context, hitContext);
        } catch (IOException e) {
            throw ExceptionsHelper.convertToElastic(e);
        }
        innerHits.queryResult().topDocs(topDocs, innerHits.sort() == null ? null : innerHits.sort().formats);
        int[] docIdsToLoad = new int[topDocs.scoreDocs.length];
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            docIdsToLoad[i] = topDocs.scoreDocs[i].doc;
        }
        innerHits.docIdsToLoad(docIdsToLoad, 0, docIdsToLoad.length);
        fetchPhase.execute(innerHits);
        FetchSearchResult fetchResult = innerHits.fetchResult();
        SearchHit[] internalHits = fetchResult.fetchResult().hits().internalHits();
        for (int i = 0; i < internalHits.length; i++) {
            ScoreDoc scoreDoc = topDocs.scoreDocs[i];
            SearchHit searchHitFields = internalHits[i];
            searchHitFields.score(scoreDoc.score);
            if (scoreDoc instanceof FieldDoc) {
                FieldDoc fieldDoc = (FieldDoc) scoreDoc;
                searchHitFields.sortValues(fieldDoc.fields, innerHits.sort().formats);
            }
        }
        results.put(entry.getKey(), fetchResult.hits());
    }
    hitContext.hit().setInnerHits(results);
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) HashMap(java.util.HashMap) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) IOException(java.io.IOException) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) SearchHits(org.elasticsearch.search.SearchHits) HashMap(java.util.HashMap) Map(java.util.Map)

Example 94 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class MatchedQueriesFetchSubPhase method hitsExecute.

@Override
public void hitsExecute(SearchContext context, SearchHit[] hits) {
    if (hits.length == 0 || // in case the request has only suggest, parsed query is null
    context.parsedQuery() == null) {
        return;
    }
    // don't modify the incoming hits
    hits = hits.clone();
    Arrays.sort(hits, (a, b) -> Integer.compare(a.docId(), b.docId()));
    @SuppressWarnings("unchecked") List<String>[] matchedQueries = new List[hits.length];
    for (int i = 0; i < matchedQueries.length; ++i) {
        matchedQueries[i] = new ArrayList<>();
    }
    Map<String, Query> namedQueries = new HashMap<>(context.parsedQuery().namedFilters());
    if (context.parsedPostFilter() != null) {
        namedQueries.putAll(context.parsedPostFilter().namedFilters());
    }
    try {
        for (Map.Entry<String, Query> entry : namedQueries.entrySet()) {
            String name = entry.getKey();
            Query query = entry.getValue();
            int readerIndex = -1;
            int docBase = -1;
            Weight weight = context.searcher().createNormalizedWeight(query, false);
            Bits matchingDocs = null;
            final IndexReader indexReader = context.searcher().getIndexReader();
            for (int i = 0; i < hits.length; ++i) {
                SearchHit hit = hits[i];
                int hitReaderIndex = ReaderUtil.subIndex(hit.docId(), indexReader.leaves());
                if (readerIndex != hitReaderIndex) {
                    readerIndex = hitReaderIndex;
                    LeafReaderContext ctx = indexReader.leaves().get(readerIndex);
                    docBase = ctx.docBase;
                    // scorers can be costly to create, so reuse them across docs of the same segment
                    Scorer scorer = weight.scorer(ctx);
                    matchingDocs = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), scorer);
                }
                if (matchingDocs.get(hit.docId() - docBase)) {
                    matchedQueries[i].add(name);
                }
            }
        }
        for (int i = 0; i < hits.length; ++i) {
            hits[i].matchedQueries(matchedQueries[i].toArray(new String[matchedQueries[i].size()]));
        }
    } catch (IOException e) {
        throw ExceptionsHelper.convertToElastic(e);
    } finally {
        context.clearReleasables(Lifetime.COLLECTION);
    }
}
Also used : Query(org.apache.lucene.search.Query) SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Weight(org.apache.lucene.search.Weight) IndexReader(org.apache.lucene.index.IndexReader) Bits(org.apache.lucene.util.Bits) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Example 95 with SearchHit

use of org.elasticsearch.search.SearchHit in project elasticsearch by elastic.

the class IndexLookupIT method checkValueInEachDoc.

private void checkValueInEachDoc(int value, Script script, int numExpectedDocs) {
    SearchResponse sr = client().prepareSearch("test").setQuery(QueryBuilders.matchAllQuery()).addScriptField("tvtest", script).execute().actionGet();
    assertHitCount(sr, numExpectedDocs);
    for (SearchHit hit : sr.getHits().getHits()) {
        Object result = hit.getFields().get("tvtest").getValues().get(0);
        if (result instanceof Integer) {
            assertThat(result, equalTo(value));
        } else if (result instanceof Long) {
            assertThat(((Long) result).intValue(), equalTo(value));
        } else {
            fail();
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

SearchHit (org.elasticsearch.search.SearchHit)166 SearchResponse (org.elasticsearch.action.search.SearchResponse)114 SearchHits (org.elasticsearch.search.SearchHits)52 ArrayList (java.util.ArrayList)31 ElasticsearchAssertions.assertSearchResponse (org.elasticsearch.test.hamcrest.ElasticsearchAssertions.assertSearchResponse)25 SearchRequestBuilder (org.elasticsearch.action.search.SearchRequestBuilder)22 IOException (java.io.IOException)20 Matchers.containsString (org.hamcrest.Matchers.containsString)17 IndexRequestBuilder (org.elasticsearch.action.index.IndexRequestBuilder)16 HashMap (java.util.HashMap)15 ScoreDoc (org.apache.lucene.search.ScoreDoc)14 SearchHitField (org.elasticsearch.search.SearchHitField)14 HashSet (java.util.HashSet)13 Map (java.util.Map)12 Test (org.junit.Test)12 AtomicReference (java.util.concurrent.atomic.AtomicReference)10 TopDocs (org.apache.lucene.search.TopDocs)10 Text (org.elasticsearch.common.text.Text)10 BulkRequestBuilder (org.elasticsearch.action.bulk.BulkRequestBuilder)9 BulkResponse (org.elasticsearch.action.bulk.BulkResponse)9