Search in sources :

Example 1 with Scorer

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

the class ScriptSortBuilder method build.

@Override
public SortFieldAndFormat build(QueryShardContext context) throws IOException {
    final SearchScript searchScript = context.getSearchScript(script, ScriptContext.Standard.SEARCH);
    MultiValueMode valueMode = null;
    if (sortMode != null) {
        valueMode = MultiValueMode.fromString(sortMode.toString());
    }
    boolean reverse = (order == SortOrder.DESC);
    if (valueMode == null) {
        valueMode = reverse ? MultiValueMode.MAX : MultiValueMode.MIN;
    }
    final Nested nested = resolveNested(context, nestedPath, nestedFilter);
    final IndexFieldData.XFieldComparatorSource fieldComparatorSource;
    switch(type) {
        case STRING:
            fieldComparatorSource = new BytesRefFieldComparatorSource(null, null, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedBinaryDocValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final BinaryDocValues values = new BinaryDocValues() {

                        final BytesRefBuilder spare = new BytesRefBuilder();

                        @Override
                        public BytesRef get(int docID) {
                            leafScript.setDocument(docID);
                            spare.copyChars(leafScript.run().toString());
                            return spare.get();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        case NUMBER:
            fieldComparatorSource = new DoubleValuesComparatorSource(null, Double.MAX_VALUE, valueMode, nested) {

                LeafSearchScript leafScript;

                @Override
                protected SortedNumericDoubleValues getValues(LeafReaderContext context) throws IOException {
                    leafScript = searchScript.getLeafSearchScript(context);
                    final NumericDoubleValues values = new NumericDoubleValues() {

                        @Override
                        public double get(int docID) {
                            leafScript.setDocument(docID);
                            return leafScript.runAsDouble();
                        }
                    };
                    return FieldData.singleton(values, null);
                }

                @Override
                protected void setScorer(Scorer scorer) {
                    leafScript.setScorer(scorer);
                }
            };
            break;
        default:
            throw new QueryShardException(context, "custom script sort type [" + type + "] not supported");
    }
    return new SortFieldAndFormat(new SortField("_script", fieldComparatorSource, reverse), DocValueFormat.RAW);
}
Also used : BytesRefBuilder(org.apache.lucene.util.BytesRefBuilder) DoubleValuesComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.DoubleValuesComparatorSource) Nested(org.elasticsearch.index.fielddata.IndexFieldData.XFieldComparatorSource.Nested) BytesRefFieldComparatorSource(org.elasticsearch.index.fielddata.fieldcomparator.BytesRefFieldComparatorSource) Scorer(org.apache.lucene.search.Scorer) NumericDoubleValues(org.elasticsearch.index.fielddata.NumericDoubleValues) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) SortField(org.apache.lucene.search.SortField) IOException(java.io.IOException) MultiValueMode(org.elasticsearch.search.MultiValueMode) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) BinaryDocValues(org.apache.lucene.index.BinaryDocValues) SortedBinaryDocValues(org.elasticsearch.index.fielddata.SortedBinaryDocValues) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) SearchScript(org.elasticsearch.script.SearchScript) LeafSearchScript(org.elasticsearch.script.LeafSearchScript) IndexFieldData(org.elasticsearch.index.fielddata.IndexFieldData) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) QueryShardException(org.elasticsearch.index.query.QueryShardException) SortedNumericDoubleValues(org.elasticsearch.index.fielddata.SortedNumericDoubleValues) BytesRef(org.apache.lucene.util.BytesRef)

Example 2 with Scorer

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

the class ProfileCollector method getLeafCollector.

@Override
public LeafCollector getLeafCollector(LeafReaderContext context) throws IOException {
    final long start = System.nanoTime();
    final LeafCollector inLeafCollector;
    try {
        inLeafCollector = super.getLeafCollector(context);
    } finally {
        time += Math.max(1, System.nanoTime() - start);
    }
    return new FilterLeafCollector(inLeafCollector) {

        @Override
        public void collect(int doc) throws IOException {
            final long start = System.nanoTime();
            try {
                super.collect(doc);
            } finally {
                time += Math.max(1, System.nanoTime() - start);
            }
        }

        @Override
        public void setScorer(Scorer scorer) throws IOException {
            final long start = System.nanoTime();
            try {
                super.setScorer(scorer);
            } finally {
                time += Math.max(1, System.nanoTime() - start);
            }
        }
    };
}
Also used : LeafCollector(org.apache.lucene.search.LeafCollector) FilterLeafCollector(org.apache.lucene.search.FilterLeafCollector) FilterLeafCollector(org.apache.lucene.search.FilterLeafCollector) Scorer(org.apache.lucene.search.Scorer)

Example 3 with Scorer

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

the class BestBucketsDeferringCollector method prepareSelectedBuckets.

/**
     * Replay the wrapped collector, but only on a selection of buckets.
     */
@Override
public void prepareSelectedBuckets(long... selectedBuckets) throws IOException {
    if (!finished) {
        throw new IllegalStateException("Cannot replay yet, collection is not finished: postCollect() has not been called");
    }
    if (this.selectedBuckets != null) {
        throw new IllegalStateException("Already been replayed");
    }
    final LongHash hash = new LongHash(selectedBuckets.length, BigArrays.NON_RECYCLING_INSTANCE);
    for (long bucket : selectedBuckets) {
        hash.add(bucket);
    }
    this.selectedBuckets = hash;
    boolean needsScores = collector.needsScores();
    Weight weight = null;
    if (needsScores) {
        weight = searchContext.searcher().createNormalizedWeight(searchContext.query(), true);
    }
    for (Entry entry : entries) {
        final LeafBucketCollector leafCollector = collector.getLeafCollector(entry.context);
        DocIdSetIterator docIt = null;
        if (needsScores && entry.docDeltas.size() > 0) {
            Scorer scorer = weight.scorer(entry.context);
            // We don't need to check if the scorer is null
            // since we are sure that there are documents to replay (entry.docDeltas it not empty).
            docIt = scorer.iterator();
            leafCollector.setScorer(scorer);
        }
        final PackedLongValues.Iterator docDeltaIterator = entry.docDeltas.iterator();
        final PackedLongValues.Iterator buckets = entry.buckets.iterator();
        int doc = 0;
        for (long i = 0, end = entry.docDeltas.size(); i < end; ++i) {
            doc += docDeltaIterator.next();
            final long bucket = buckets.next();
            final long rebasedBucket = hash.find(bucket);
            if (rebasedBucket != -1) {
                if (needsScores) {
                    if (docIt.docID() < doc) {
                        docIt.advance(doc);
                    }
                    // aggregations should only be replayed on matching documents
                    assert docIt.docID() == doc;
                }
                leafCollector.collect(doc, rebasedBucket);
            }
        }
    }
    collector.postCollection();
}
Also used : PackedLongValues(org.apache.lucene.util.packed.PackedLongValues) LongHash(org.elasticsearch.common.util.LongHash) LeafBucketCollector(org.elasticsearch.search.aggregations.LeafBucketCollector) Scorer(org.apache.lucene.search.Scorer) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) Weight(org.apache.lucene.search.Weight)

Example 4 with Scorer

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

the class ParentToChildrenAggregator method getLeafCollector.

@Override
public LeafBucketCollector getLeafCollector(LeafReaderContext ctx, final LeafBucketCollector sub) throws IOException {
    if (valuesSource == null) {
        return LeafBucketCollector.NO_OP_COLLECTOR;
    }
    final SortedDocValues globalOrdinals = valuesSource.globalOrdinalsValues(parentType, ctx);
    assert globalOrdinals != null;
    Scorer parentScorer = parentFilter.scorer(ctx);
    final Bits parentDocs = Lucene.asSequentialAccessBits(ctx.reader().maxDoc(), parentScorer);
    return new LeafBucketCollector() {

        @Override
        public void collect(int docId, long bucket) throws IOException {
            if (parentDocs.get(docId)) {
                long globalOrdinal = globalOrdinals.getOrd(docId);
                if (globalOrdinal != -1) {
                    if (parentOrdToBuckets.get(globalOrdinal) == -1) {
                        parentOrdToBuckets.set(globalOrdinal, bucket);
                    } else {
                        long[] bucketOrds = parentOrdToOtherBuckets.get(globalOrdinal);
                        if (bucketOrds != null) {
                            bucketOrds = Arrays.copyOf(bucketOrds, bucketOrds.length + 1);
                            bucketOrds[bucketOrds.length - 1] = bucket;
                            parentOrdToOtherBuckets.put(globalOrdinal, bucketOrds);
                        } else {
                            parentOrdToOtherBuckets.put(globalOrdinal, new long[] { bucket });
                        }
                        multipleBucketsPerParentOrd = true;
                    }
                }
            }
        }
    };
}
Also used : LeafBucketCollector(org.elasticsearch.search.aggregations.LeafBucketCollector) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) Scorer(org.apache.lucene.search.Scorer) Bits(org.apache.lucene.util.Bits) SortedDocValues(org.apache.lucene.index.SortedDocValues)

Example 5 with Scorer

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

the class PercolateQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores) throws IOException {
    final Weight verifiedMatchesWeight = verifiedMatchesQuery.createWeight(searcher, false);
    final Weight candidateMatchesWeight = candidateMatchesQuery.createWeight(searcher, false);
    return new Weight(this) {

        @Override
        public void extractTerms(Set<Term> set) {
        }

        @Override
        public Explanation explain(LeafReaderContext leafReaderContext, int docId) throws IOException {
            Scorer scorer = scorer(leafReaderContext);
            if (scorer != null) {
                TwoPhaseIterator twoPhaseIterator = scorer.twoPhaseIterator();
                int result = twoPhaseIterator.approximation().advance(docId);
                if (result == docId) {
                    if (twoPhaseIterator.matches()) {
                        if (needsScores) {
                            CheckedFunction<Integer, Query, IOException> percolatorQueries = queryStore.getQueries(leafReaderContext);
                            Query query = percolatorQueries.apply(docId);
                            Explanation detail = percolatorIndexSearcher.explain(query, 0);
                            return Explanation.match(scorer.score(), "PercolateQuery", detail);
                        } else {
                            return Explanation.match(scorer.score(), "PercolateQuery");
                        }
                    }
                }
            }
            return Explanation.noMatch("PercolateQuery");
        }

        @Override
        public float getValueForNormalization() throws IOException {
            return candidateMatchesWeight.getValueForNormalization();
        }

        @Override
        public void normalize(float v, float v1) {
            candidateMatchesWeight.normalize(v, v1);
        }

        @Override
        public Scorer scorer(LeafReaderContext leafReaderContext) throws IOException {
            final Scorer approximation = candidateMatchesWeight.scorer(leafReaderContext);
            if (approximation == null) {
                return null;
            }
            final CheckedFunction<Integer, Query, IOException> queries = queryStore.getQueries(leafReaderContext);
            if (needsScores) {
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    float score;

                    @Override
                    boolean matchDocId(int docId) throws IOException {
                        Query query = percolatorQueries.apply(docId);
                        if (query != null) {
                            TopDocs topDocs = percolatorIndexSearcher.search(query, 1);
                            if (topDocs.totalHits > 0) {
                                score = topDocs.scoreDocs[0].score;
                                return true;
                            } else {
                                return false;
                            }
                        } else {
                            return false;
                        }
                    }

                    @Override
                    public float score() throws IOException {
                        return score;
                    }
                };
            } else {
                Scorer verifiedDocsScorer = verifiedMatchesWeight.scorer(leafReaderContext);
                Bits verifiedDocsBits = Lucene.asSequentialAccessBits(leafReaderContext.reader().maxDoc(), verifiedDocsScorer);
                return new BaseScorer(this, approximation, queries, percolatorIndexSearcher) {

                    @Override
                    public float score() throws IOException {
                        return 0f;
                    }

                    boolean matchDocId(int docId) throws IOException {
                        // the MemoryIndex verification.
                        if (verifiedDocsBits.get(docId)) {
                            return true;
                        }
                        Query query = percolatorQueries.apply(docId);
                        return query != null && Lucene.exists(percolatorIndexSearcher, query);
                    }
                };
            }
        }
    };
}
Also used : Set(java.util.Set) TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) Query(org.apache.lucene.search.Query) Explanation(org.apache.lucene.search.Explanation) Scorer(org.apache.lucene.search.Scorer) IOException(java.io.IOException) Weight(org.apache.lucene.search.Weight) TopDocs(org.apache.lucene.search.TopDocs) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) Bits(org.apache.lucene.util.Bits)

Aggregations

Scorer (org.apache.lucene.search.Scorer)56 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)35 Weight (org.apache.lucene.search.Weight)35 IndexSearcher (org.apache.lucene.search.IndexSearcher)21 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)20 Query (org.apache.lucene.search.Query)16 IOException (java.io.IOException)14 Bits (org.apache.lucene.util.Bits)11 Set (java.util.Set)9 ConstantScoreScorer (org.apache.lucene.search.ConstantScoreScorer)9 TermQuery (org.apache.lucene.search.TermQuery)9 BytesRef (org.apache.lucene.util.BytesRef)9 Document (org.apache.lucene.document.Document)8 IndexReader (org.apache.lucene.index.IndexReader)8 HashSet (java.util.HashSet)7 IndexReaderContext (org.apache.lucene.index.IndexReaderContext)7 TwoPhaseIterator (org.apache.lucene.search.TwoPhaseIterator)7 Directory (org.apache.lucene.store.Directory)7 BitSet (org.apache.lucene.util.BitSet)7 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6