Search in sources :

Example 6 with ScoreDoc

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

the class CollapseTopFieldDocs method merge.

/**
     * Returns a new CollapseTopDocs, containing topN collapsed results across
     * the provided CollapseTopDocs, sorting by score. Each {@link CollapseTopFieldDocs} instance must be sorted.
     **/
public static CollapseTopFieldDocs merge(Sort sort, int start, int size, CollapseTopFieldDocs[] shardHits) throws IOException {
    String collapseField = shardHits[0].field;
    for (int i = 1; i < shardHits.length; i++) {
        if (collapseField.equals(shardHits[i].field) == false) {
            throw new IllegalArgumentException("collapse field differ across shards [" + collapseField + "] != [" + shardHits[i].field + "]");
        }
    }
    final PriorityQueue<ShardRef> queue = new MergeSortQueue(sort, shardHits);
    int totalHitCount = 0;
    int availHitCount = 0;
    float maxScore = Float.MIN_VALUE;
    for (int shardIDX = 0; shardIDX < shardHits.length; shardIDX++) {
        final CollapseTopFieldDocs shard = shardHits[shardIDX];
        // totalHits can be non-zero even if no hits were
        // collected, when searchAfter was used:
        totalHitCount += shard.totalHits;
        if (shard.scoreDocs != null && shard.scoreDocs.length > 0) {
            availHitCount += shard.scoreDocs.length;
            queue.add(new ShardRef(shardIDX));
            maxScore = Math.max(maxScore, shard.getMaxScore());
        }
    }
    if (availHitCount == 0) {
        maxScore = Float.NaN;
    }
    final ScoreDoc[] hits;
    final Object[] values;
    if (availHitCount <= start) {
        hits = new ScoreDoc[0];
        values = new Object[0];
    } else {
        List<ScoreDoc> hitList = new ArrayList<>();
        List<Object> collapseList = new ArrayList<>();
        int requestedResultWindow = start + size;
        int numIterOnHits = Math.min(availHitCount, requestedResultWindow);
        int hitUpto = 0;
        Set<Object> seen = new HashSet<>();
        while (hitUpto < numIterOnHits) {
            if (queue.size() == 0) {
                break;
            }
            ShardRef ref = queue.top();
            final ScoreDoc hit = shardHits[ref.shardIndex].scoreDocs[ref.hitIndex];
            final Object collapseValue = shardHits[ref.shardIndex].collapseValues[ref.hitIndex++];
            if (seen.contains(collapseValue)) {
                if (ref.hitIndex < shardHits[ref.shardIndex].scoreDocs.length) {
                    queue.updateTop();
                } else {
                    queue.pop();
                }
                continue;
            }
            seen.add(collapseValue);
            hit.shardIndex = ref.shardIndex;
            if (hitUpto >= start) {
                hitList.add(hit);
                collapseList.add(collapseValue);
            }
            hitUpto++;
            if (ref.hitIndex < shardHits[ref.shardIndex].scoreDocs.length) {
                // Not done with this these TopDocs yet:
                queue.updateTop();
            } else {
                queue.pop();
            }
        }
        hits = hitList.toArray(new ScoreDoc[0]);
        values = collapseList.toArray(new Object[0]);
    }
    return new CollapseTopFieldDocs(collapseField, totalHitCount, hits, sort.getSort(), values, maxScore);
}
Also used : ArrayList(java.util.ArrayList) ScoreDoc(org.apache.lucene.search.ScoreDoc) HashSet(java.util.HashSet)

Example 7 with ScoreDoc

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

the class SearchScrollQueryThenFetchAsyncAction method executeFetchPhase.

private void executeFetchPhase() throws Exception {
    sortedShardDocs = searchPhaseController.sortDocs(true, queryResults);
    if (sortedShardDocs.length == 0) {
        finishHim(searchPhaseController.reducedQueryPhase(queryResults.asList()));
        return;
    }
    final IntArrayList[] docIdsToLoad = searchPhaseController.fillDocIdsToLoad(queryResults.length(), sortedShardDocs);
    SearchPhaseController.ReducedQueryPhase reducedQueryPhase = searchPhaseController.reducedQueryPhase(queryResults.asList());
    final ScoreDoc[] lastEmittedDocPerShard = searchPhaseController.getLastEmittedDocPerShard(reducedQueryPhase, sortedShardDocs, queryResults.length());
    final AtomicInteger counter = new AtomicInteger(docIdsToLoad.length);
    for (int i = 0; i < docIdsToLoad.length; i++) {
        final int index = i;
        final IntArrayList docIds = docIdsToLoad[index];
        if (docIds != null) {
            final QuerySearchResult querySearchResult = queryResults.get(index);
            ScoreDoc lastEmittedDoc = lastEmittedDocPerShard[index];
            ShardFetchRequest shardFetchRequest = new ShardFetchRequest(querySearchResult.id(), docIds, lastEmittedDoc);
            DiscoveryNode node = nodes.get(querySearchResult.shardTarget().getNodeId());
            searchTransportService.sendExecuteFetchScroll(node, shardFetchRequest, task, new ActionListener<FetchSearchResult>() {

                @Override
                public void onResponse(FetchSearchResult result) {
                    result.shardTarget(querySearchResult.shardTarget());
                    fetchResults.set(index, result);
                    if (counter.decrementAndGet() == 0) {
                        finishHim(reducedQueryPhase);
                    }
                }

                @Override
                public void onFailure(Exception t) {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Failed to execute fetch phase", t);
                    }
                    successfulOps.decrementAndGet();
                    if (counter.decrementAndGet() == 0) {
                        finishHim(reducedQueryPhase);
                    }
                }
            });
        } else {
            // the counter is set to the total size of docIdsToLoad which can have null values so we have to count them down too
            if (counter.decrementAndGet() == 0) {
                finishHim(reducedQueryPhase);
            }
        }
    }
}
Also used : DiscoveryNode(org.elasticsearch.cluster.node.DiscoveryNode) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ShardFetchRequest(org.elasticsearch.search.fetch.ShardFetchRequest) ScoreDoc(org.apache.lucene.search.ScoreDoc) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ScrollQuerySearchResult(org.elasticsearch.search.query.ScrollQuerySearchResult) QuerySearchResult(org.elasticsearch.search.query.QuerySearchResult) IntArrayList(com.carrotsearch.hppc.IntArrayList)

Example 8 with ScoreDoc

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

the class SearchPhaseController method fillDocIdsToLoad.

/**
     * Builds an array, with potential null elements, with docs to load.
     */
public IntArrayList[] fillDocIdsToLoad(int numShards, ScoreDoc[] shardDocs) {
    IntArrayList[] docIdsToLoad = new IntArrayList[numShards];
    for (ScoreDoc shardDoc : shardDocs) {
        IntArrayList shardDocIdsToLoad = docIdsToLoad[shardDoc.shardIndex];
        if (shardDocIdsToLoad == null) {
            shardDocIdsToLoad = docIdsToLoad[shardDoc.shardIndex] = new IntArrayList();
        }
        shardDocIdsToLoad.add(shardDoc.doc);
    }
    return docIdsToLoad;
}
Also used : IntArrayList(com.carrotsearch.hppc.IntArrayList) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 9 with ScoreDoc

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

the class SearchPhaseController method getHits.

private SearchHits getHits(ReducedQueryPhase reducedQueryPhase, boolean ignoreFrom, ScoreDoc[] sortedDocs, AtomicArray<? extends QuerySearchResultProvider> fetchResultsArr) {
    List<? extends AtomicArray.Entry<? extends QuerySearchResultProvider>> fetchResults = fetchResultsArr.asList();
    boolean sorted = false;
    int sortScoreIndex = -1;
    if (reducedQueryPhase.oneResult.topDocs() instanceof TopFieldDocs) {
        TopFieldDocs fieldDocs = (TopFieldDocs) reducedQueryPhase.oneResult.queryResult().topDocs();
        if (fieldDocs instanceof CollapseTopFieldDocs && fieldDocs.fields.length == 1 && fieldDocs.fields[0].getType() == SortField.Type.SCORE) {
            sorted = false;
        } else {
            sorted = true;
            for (int i = 0; i < fieldDocs.fields.length; i++) {
                if (fieldDocs.fields[i].getType() == SortField.Type.SCORE) {
                    sortScoreIndex = i;
                }
            }
        }
    }
    // clean the fetch counter
    for (AtomicArray.Entry<? extends QuerySearchResultProvider> entry : fetchResults) {
        entry.value.fetchResult().initCounter();
    }
    int from = ignoreFrom ? 0 : reducedQueryPhase.oneResult.queryResult().from();
    int numSearchHits = (int) Math.min(reducedQueryPhase.fetchHits - from, reducedQueryPhase.oneResult.size());
    // with collapsing we can have more fetch hits than sorted docs
    numSearchHits = Math.min(sortedDocs.length, numSearchHits);
    // merge hits
    List<SearchHit> hits = new ArrayList<>();
    if (!fetchResults.isEmpty()) {
        for (int i = 0; i < numSearchHits; i++) {
            ScoreDoc shardDoc = sortedDocs[i];
            QuerySearchResultProvider fetchResultProvider = fetchResultsArr.get(shardDoc.shardIndex);
            if (fetchResultProvider == null) {
                continue;
            }
            FetchSearchResult fetchResult = fetchResultProvider.fetchResult();
            int index = fetchResult.counterGetAndIncrement();
            if (index < fetchResult.hits().internalHits().length) {
                SearchHit searchHit = fetchResult.hits().internalHits()[index];
                searchHit.score(shardDoc.score);
                searchHit.shard(fetchResult.shardTarget());
                if (sorted) {
                    FieldDoc fieldDoc = (FieldDoc) shardDoc;
                    searchHit.sortValues(fieldDoc.fields, reducedQueryPhase.oneResult.sortValueFormats());
                    if (sortScoreIndex != -1) {
                        searchHit.score(((Number) fieldDoc.fields[sortScoreIndex]).floatValue());
                    }
                }
                hits.add(searchHit);
            }
        }
    }
    return new SearchHits(hits.toArray(new SearchHit[hits.size()]), reducedQueryPhase.totalHits, reducedQueryPhase.maxScore);
}
Also used : AtomicArray(org.elasticsearch.common.util.concurrent.AtomicArray) QuerySearchResultProvider(org.elasticsearch.search.query.QuerySearchResultProvider) SearchHit(org.elasticsearch.search.SearchHit) FieldDoc(org.apache.lucene.search.FieldDoc) FetchSearchResult(org.elasticsearch.search.fetch.FetchSearchResult) ArrayList(java.util.ArrayList) IntArrayList(com.carrotsearch.hppc.IntArrayList) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) SearchHits(org.elasticsearch.search.SearchHits) CollapseTopFieldDocs(org.apache.lucene.search.grouping.CollapseTopFieldDocs)

Example 10 with ScoreDoc

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

the class TermVectorsUnitTests method writeEmptyTermVector.

private void writeEmptyTermVector(TermVectorsResponse outResponse) throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig conf = new IndexWriterConfig(new StandardAnalyzer());
    conf.setOpenMode(OpenMode.CREATE);
    IndexWriter writer = new IndexWriter(dir, conf);
    FieldType type = new FieldType(TextField.TYPE_STORED);
    type.setStoreTermVectorOffsets(true);
    type.setStoreTermVectorPayloads(false);
    type.setStoreTermVectorPositions(true);
    type.setStoreTermVectors(true);
    type.freeze();
    Document d = new Document();
    d.add(new Field("id", "abc", StringField.TYPE_STORED));
    writer.updateDocument(new Term("id", "abc"), d);
    writer.commit();
    writer.close();
    DirectoryReader dr = DirectoryReader.open(dir);
    IndexSearcher s = new IndexSearcher(dr);
    TopDocs search = s.search(new TermQuery(new Term("id", "abc")), 1);
    ScoreDoc[] scoreDocs = search.scoreDocs;
    int doc = scoreDocs[0].doc;
    Fields fields = dr.getTermVectors(doc);
    EnumSet<Flag> flags = EnumSet.of(Flag.Positions, Flag.Offsets);
    outResponse.setFields(fields, null, flags, fields);
    outResponse.setExists(true);
    dr.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TermQuery(org.apache.lucene.search.TermQuery) DirectoryReader(org.apache.lucene.index.DirectoryReader) Term(org.apache.lucene.index.Term) Document(org.apache.lucene.document.Document) Flag(org.elasticsearch.action.termvectors.TermVectorsRequest.Flag) FieldType(org.apache.lucene.document.FieldType) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) StringField(org.apache.lucene.document.StringField) Field(org.apache.lucene.document.Field) TextField(org.apache.lucene.document.TextField) Fields(org.apache.lucene.index.Fields) IndexWriter(org.apache.lucene.index.IndexWriter) StandardAnalyzer(org.apache.lucene.analysis.standard.StandardAnalyzer) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Aggregations

ScoreDoc (org.apache.lucene.search.ScoreDoc)211 TopDocs (org.apache.lucene.search.TopDocs)119 IndexSearcher (org.apache.lucene.search.IndexSearcher)94 Document (org.apache.lucene.document.Document)89 Query (org.apache.lucene.search.Query)65 TermQuery (org.apache.lucene.search.TermQuery)49 ArrayList (java.util.ArrayList)46 IOException (java.io.IOException)44 IndexReader (org.apache.lucene.index.IndexReader)42 Term (org.apache.lucene.index.Term)38 Directory (org.apache.lucene.store.Directory)37 BooleanQuery (org.apache.lucene.search.BooleanQuery)26 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)23 Sort (org.apache.lucene.search.Sort)22 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)21 Test (org.junit.Test)21 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)20 FieldDoc (org.apache.lucene.search.FieldDoc)20 HashMap (java.util.HashMap)18 HashSet (java.util.HashSet)17