Search in sources :

Example 26 with FieldDoc

use of org.apache.lucene.search.FieldDoc 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 27 with FieldDoc

use of org.apache.lucene.search.FieldDoc 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 28 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project elasticsearch by elastic.

the class CollapsingTopDocsCollector method getTopDocs.

/**
     * Transform {@link FirstPassGroupingCollector#getTopGroups(int, boolean)} output in
     * {@link CollapseTopFieldDocs}. The collapsing needs only one pass so we can create the final top docs at the end
     * of the first pass.
     */
public CollapseTopFieldDocs getTopDocs() {
    Collection<SearchGroup<T>> groups = super.getTopGroups(0, true);
    if (groups == null) {
        return new CollapseTopFieldDocs(collapseField, totalHitCount, new ScoreDoc[0], sort.getSort(), new Object[0], Float.NaN);
    }
    FieldDoc[] docs = new FieldDoc[groups.size()];
    Object[] collapseValues = new Object[groups.size()];
    int scorePos = -1;
    for (int index = 0; index < sort.getSort().length; index++) {
        SortField sortField = sort.getSort()[index];
        if (sortField.getType() == SCORE) {
            scorePos = index;
            break;
        }
    }
    int pos = 0;
    Iterator<CollectedSearchGroup<T>> it = orderedGroups.iterator();
    for (SearchGroup<T> group : groups) {
        assert it.hasNext();
        CollectedSearchGroup<T> col = it.next();
        float score = Float.NaN;
        if (scorePos != -1) {
            score = (float) group.sortValues[scorePos];
        }
        docs[pos] = new FieldDoc(col.topDoc, score, group.sortValues);
        collapseValues[pos] = group.groupValue;
        pos++;
    }
    return new CollapseTopFieldDocs(collapseField, totalHitCount, docs, sort.getSort(), collapseValues, maxScore);
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) SortField(org.apache.lucene.search.SortField)

Example 29 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project elasticsearch by elastic.

the class Lucene method writeTopDocs.

public static void writeTopDocs(StreamOutput out, TopDocs topDocs) throws IOException {
    if (topDocs instanceof CollapseTopFieldDocs) {
        out.writeByte((byte) 2);
        CollapseTopFieldDocs collapseDocs = (CollapseTopFieldDocs) topDocs;
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeString(collapseDocs.field);
        out.writeVInt(collapseDocs.fields.length);
        for (SortField sortField : collapseDocs.fields) {
            writeSortField(out, sortField);
        }
        out.writeVInt(topDocs.scoreDocs.length);
        for (int i = 0; i < topDocs.scoreDocs.length; i++) {
            ScoreDoc doc = collapseDocs.scoreDocs[i];
            writeFieldDoc(out, (FieldDoc) doc);
            writeSortValue(out, collapseDocs.collapseValues[i]);
        }
    } else if (topDocs instanceof TopFieldDocs) {
        out.writeByte((byte) 1);
        TopFieldDocs topFieldDocs = (TopFieldDocs) topDocs;
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeVInt(topFieldDocs.fields.length);
        for (SortField sortField : topFieldDocs.fields) {
            writeSortField(out, sortField);
        }
        out.writeVInt(topDocs.scoreDocs.length);
        for (ScoreDoc doc : topFieldDocs.scoreDocs) {
            writeFieldDoc(out, (FieldDoc) doc);
        }
    } else {
        out.writeByte((byte) 0);
        out.writeVInt(topDocs.totalHits);
        out.writeFloat(topDocs.getMaxScore());
        out.writeVInt(topDocs.scoreDocs.length);
        for (ScoreDoc doc : topDocs.scoreDocs) {
            writeScoreDoc(out, doc);
        }
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 30 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project elasticsearch by elastic.

the class Lucene method readTopDocs.

public static TopDocs readTopDocs(StreamInput in) throws IOException {
    byte type = in.readByte();
    if (type == 0) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        ScoreDoc[] scoreDocs = new ScoreDoc[in.readVInt()];
        for (int i = 0; i < scoreDocs.length; i++) {
            scoreDocs[i] = new ScoreDoc(in.readVInt(), in.readFloat());
        }
        return new TopDocs(totalHits, scoreDocs, maxScore);
    } else if (type == 1) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        SortField[] fields = new SortField[in.readVInt()];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = readSortField(in);
        }
        FieldDoc[] fieldDocs = new FieldDoc[in.readVInt()];
        for (int i = 0; i < fieldDocs.length; i++) {
            fieldDocs[i] = readFieldDoc(in);
        }
        return new TopFieldDocs(totalHits, fieldDocs, fields, maxScore);
    } else if (type == 2) {
        int totalHits = in.readVInt();
        float maxScore = in.readFloat();
        String field = in.readString();
        SortField[] fields = new SortField[in.readVInt()];
        for (int i = 0; i < fields.length; i++) {
            fields[i] = readSortField(in);
        }
        int size = in.readVInt();
        Object[] collapseValues = new Object[size];
        FieldDoc[] fieldDocs = new FieldDoc[size];
        for (int i = 0; i < fieldDocs.length; i++) {
            fieldDocs[i] = readFieldDoc(in);
            collapseValues[i] = readSortValue(in);
        }
        return new CollapseTopFieldDocs(field, totalHits, fieldDocs, fields, collapseValues, maxScore);
    } else {
        throw new IllegalStateException("Unknown type " + type);
    }
}
Also used : FieldDoc(org.apache.lucene.search.FieldDoc) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs)

Aggregations

FieldDoc (org.apache.lucene.search.FieldDoc)50 Sort (org.apache.lucene.search.Sort)26 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)23 SortField (org.apache.lucene.search.SortField)21 ScoreDoc (org.apache.lucene.search.ScoreDoc)20 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 ArrayList (java.util.ArrayList)14 Directory (org.apache.lucene.store.Directory)14 TopDocs (org.apache.lucene.search.TopDocs)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 TermQuery (org.apache.lucene.search.TermQuery)10 Document (org.apache.lucene.document.Document)9 Term (org.apache.lucene.index.Term)9 BytesRef (org.apache.lucene.util.BytesRef)9 HashMap (java.util.HashMap)8 Query (org.apache.lucene.search.Query)8 IndexReader (org.apache.lucene.index.IndexReader)7 HashSet (java.util.HashSet)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5