Search in sources :

Example 1 with DoubleValues

use of org.apache.lucene.search.DoubleValues in project lucene-solr by apache.

the class DoubleRangeFacetCounts method count.

private void count(DoubleValuesSource valueSource, List<MatchingDocs> matchingDocs) throws IOException {
    DoubleRange[] ranges = (DoubleRange[]) this.ranges;
    LongRange[] longRanges = new LongRange[ranges.length];
    for (int i = 0; i < ranges.length; i++) {
        DoubleRange range = ranges[i];
        longRanges[i] = new LongRange(range.label, NumericUtils.doubleToSortableLong(range.min), true, NumericUtils.doubleToSortableLong(range.max), true);
    }
    LongRangeCounter counter = new LongRangeCounter(longRanges);
    int missingCount = 0;
    for (MatchingDocs hits : matchingDocs) {
        DoubleValues fv = valueSource.getValues(hits.context, null);
        totCount += hits.totalHits;
        final DocIdSetIterator fastMatchDocs;
        if (fastMatchQuery != null) {
            final IndexReaderContext topLevelContext = ReaderUtil.getTopLevelContext(hits.context);
            final IndexSearcher searcher = new IndexSearcher(topLevelContext);
            searcher.setQueryCache(null);
            final Weight fastMatchWeight = searcher.createNormalizedWeight(fastMatchQuery, false);
            Scorer s = fastMatchWeight.scorer(hits.context);
            if (s == null) {
                continue;
            }
            fastMatchDocs = s.iterator();
        } else {
            fastMatchDocs = null;
        }
        DocIdSetIterator docs = hits.bits.iterator();
        for (int doc = docs.nextDoc(); doc != DocIdSetIterator.NO_MORE_DOCS; ) {
            if (fastMatchDocs != null) {
                int fastMatchDoc = fastMatchDocs.docID();
                if (fastMatchDoc < doc) {
                    fastMatchDoc = fastMatchDocs.advance(doc);
                }
                if (doc != fastMatchDoc) {
                    doc = docs.advance(fastMatchDoc);
                    continue;
                }
            }
            // Skip missing docs:
            if (fv.advanceExact(doc)) {
                counter.add(NumericUtils.doubleToSortableLong(fv.doubleValue()));
            } else {
                missingCount++;
            }
            doc = docs.nextDoc();
        }
    }
    missingCount += counter.fillCounts(counts);
    totCount -= missingCount;
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MatchingDocs(org.apache.lucene.facet.FacetsCollector.MatchingDocs) DoubleValues(org.apache.lucene.search.DoubleValues) Scorer(org.apache.lucene.search.Scorer) IndexReaderContext(org.apache.lucene.index.IndexReaderContext) Weight(org.apache.lucene.search.Weight) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator)

Example 2 with DoubleValues

use of org.apache.lucene.search.DoubleValues in project lucene-solr by apache.

the class FunctionMatchQuery method createWeight.

@Override
public Weight createWeight(IndexSearcher searcher, boolean needsScores, float boost) throws IOException {
    return new ConstantScoreWeight(this, boost) {

        @Override
        public Scorer scorer(LeafReaderContext context) throws IOException {
            DoubleValues values = source.getValues(context, null);
            DocIdSetIterator approximation = DocIdSetIterator.all(context.reader().maxDoc());
            TwoPhaseIterator twoPhase = new TwoPhaseIterator(approximation) {

                @Override
                public boolean matches() throws IOException {
                    return values.advanceExact(approximation.docID()) && filter.test(values.doubleValue());
                }

                @Override
                public float matchCost() {
                    // TODO maybe DoubleValuesSource should have a matchCost?
                    return 100;
                }
            };
            return new ConstantScoreScorer(this, score(), twoPhase);
        }
    };
}
Also used : TwoPhaseIterator(org.apache.lucene.search.TwoPhaseIterator) ConstantScoreScorer(org.apache.lucene.search.ConstantScoreScorer) DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DocIdSetIterator(org.apache.lucene.search.DocIdSetIterator) ConstantScoreWeight(org.apache.lucene.search.ConstantScoreWeight)

Example 3 with DoubleValues

use of org.apache.lucene.search.DoubleValues in project lucene-solr by apache.

the class ValueSource method asDoubleValuesSource.

/**
   * Expose this ValueSource as a DoubleValuesSource
   */
public DoubleValuesSource asDoubleValuesSource() {
    return new DoubleValuesSource() {

        @Override
        public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
            Map context = new HashMap<>();
            FakeScorer scorer = new FakeScorer();
            context.put("scorer", scorer);
            FunctionValues fv = ValueSource.this.getValues(context, ctx);
            return new DoubleValues() {

                @Override
                public double doubleValue() throws IOException {
                    return fv.doubleVal(scorer.current);
                }

                @Override
                public boolean advanceExact(int doc) throws IOException {
                    scorer.current = doc;
                    if (scores != null && scores.advanceExact(doc)) {
                        scorer.score = (float) scores.doubleValue();
                    } else
                        scorer.score = 0;
                    return fv.exists(doc);
                }
            };
        }

        @Override
        public boolean needsScores() {
            // be on the safe side
            return true;
        }

        @Override
        public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
            Map context = new HashMap<>();
            FakeScorer scorer = new FakeScorer();
            scorer.score = scoreExplanation.getValue();
            context.put("scorer", scorer);
            FunctionValues fv = ValueSource.this.getValues(context, ctx);
            return fv.explain(docId);
        }
    };
}
Also used : IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Explanation(org.apache.lucene.search.Explanation) DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) IdentityHashMap(java.util.IdentityHashMap) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with DoubleValues

use of org.apache.lucene.search.DoubleValues in project lucene-solr by apache.

the class TestRangeFacetCounts method testCustomDoubleValuesSource.

public void testCustomDoubleValuesSource() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir);
    Document doc = new Document();
    writer.addDocument(doc);
    writer.addDocument(doc);
    writer.addDocument(doc);
    // Test wants 3 docs in one segment:
    writer.forceMerge(1);
    final DoubleValuesSource vs = new DoubleValuesSource() {

        @Override
        public DoubleValues getValues(LeafReaderContext ctx, DoubleValues scores) throws IOException {
            return new DoubleValues() {

                int doc = -1;

                @Override
                public double doubleValue() throws IOException {
                    return doc + 1;
                }

                @Override
                public boolean advanceExact(int doc) throws IOException {
                    this.doc = doc;
                    return true;
                }
            };
        }

        @Override
        public boolean needsScores() {
            return false;
        }

        @Override
        public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
            return Explanation.match(docId + 1, "");
        }
    };
    FacetsConfig config = new FacetsConfig();
    FacetsCollector fc = new FacetsCollector();
    IndexReader r = writer.getReader();
    IndexSearcher s = newSearcher(r);
    s.search(new MatchAllDocsQuery(), fc);
    final DoubleRange[] ranges = new DoubleRange[] { new DoubleRange("< 1", 0.0, true, 1.0, false), new DoubleRange("< 2", 0.0, true, 2.0, false), new DoubleRange("< 5", 0.0, true, 5.0, false), new DoubleRange("< 10", 0.0, true, 10.0, false), new DoubleRange("< 20", 0.0, true, 20.0, false), new DoubleRange("< 50", 0.0, true, 50.0, false) };
    final Query fastMatchFilter;
    final AtomicBoolean filterWasUsed = new AtomicBoolean();
    if (random().nextBoolean()) {
        // Sort of silly:
        final Query in = new MatchAllDocsQuery();
        fastMatchFilter = new UsedQuery(in, filterWasUsed);
    } else {
        fastMatchFilter = null;
    }
    if (VERBOSE) {
        System.out.println("TEST: fastMatchFilter=" + fastMatchFilter);
    }
    Facets facets = new DoubleRangeFacetCounts("field", vs, fc, fastMatchFilter, ranges);
    assertEquals("dim=field path=[] value=3 childCount=6\n  < 1 (0)\n  < 2 (1)\n  < 5 (3)\n  < 10 (3)\n  < 20 (3)\n  < 50 (3)\n", facets.getTopChildren(10, "field").toString());
    assertTrue(fastMatchFilter == null || filterWasUsed.get());
    DrillDownQuery ddq = new DrillDownQuery(config);
    ddq.add("field", ranges[1].getQuery(fastMatchFilter, vs));
    // Test simple drill-down:
    assertEquals(1, s.search(ddq, 10).totalHits);
    // Test drill-sideways after drill-down
    DrillSideways ds = new DrillSideways(s, config, (TaxonomyReader) null) {

        @Override
        protected Facets buildFacetsResult(FacetsCollector drillDowns, FacetsCollector[] drillSideways, String[] drillSidewaysDims) throws IOException {
            assert drillSideways.length == 1;
            return new DoubleRangeFacetCounts("field", vs, drillSideways[0], fastMatchFilter, ranges);
        }

        @Override
        protected boolean scoreSubDocsAtOnce() {
            return random().nextBoolean();
        }
    };
    DrillSidewaysResult dsr = ds.search(ddq, 10);
    assertEquals(1, dsr.hits.totalHits);
    assertEquals("dim=field path=[] value=3 childCount=6\n  < 1 (0)\n  < 2 (1)\n  < 5 (3)\n  < 10 (3)\n  < 20 (3)\n  < 50 (3)\n", dsr.facets.getTopChildren(10, "field").toString());
    writer.close();
    IOUtils.close(r, dir);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) FacetsConfig(org.apache.lucene.facet.FacetsConfig) Query(org.apache.lucene.search.Query) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) MultiFacets(org.apache.lucene.facet.MultiFacets) Facets(org.apache.lucene.facet.Facets) Explanation(org.apache.lucene.search.Explanation) DrillDownQuery(org.apache.lucene.facet.DrillDownQuery) DoubleValues(org.apache.lucene.search.DoubleValues) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) FacetsCollector(org.apache.lucene.facet.FacetsCollector) DrillSidewaysResult(org.apache.lucene.facet.DrillSideways.DrillSidewaysResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) IndexReader(org.apache.lucene.index.IndexReader) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) DrillSideways(org.apache.lucene.facet.DrillSideways) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 5 with DoubleValues

use of org.apache.lucene.search.DoubleValues in project lucene-solr by apache.

the class ExpressionValueSource method getValues.

@Override
public DoubleValues getValues(LeafReaderContext readerContext, DoubleValues scores) throws IOException {
    Map<String, DoubleValues> valuesCache = new HashMap<>();
    DoubleValues[] externalValues = new DoubleValues[expression.variables.length];
    for (int i = 0; i < variables.length; ++i) {
        String externalName = expression.variables[i];
        DoubleValues values = valuesCache.get(externalName);
        if (values == null) {
            values = variables[i].getValues(readerContext, scores);
            if (values == null) {
                throw new RuntimeException("Internal error. External (" + externalName + ") does not exist.");
            }
            valuesCache.put(externalName, values);
        }
        externalValues[i] = zeroWhenUnpositioned(values);
    }
    return new ExpressionFunctionValues(expression, externalValues);
}
Also used : HashMap(java.util.HashMap) DoubleValues(org.apache.lucene.search.DoubleValues)

Aggregations

DoubleValues (org.apache.lucene.search.DoubleValues)10 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)5 DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)4 HashMap (java.util.HashMap)3 DocIdSetIterator (org.apache.lucene.search.DocIdSetIterator)3 Explanation (org.apache.lucene.search.Explanation)3 IdentityHashMap (java.util.IdentityHashMap)2 Map (java.util.Map)2 MatchingDocs (org.apache.lucene.facet.FacetsCollector.MatchingDocs)2 IndexSearcher (org.apache.lucene.search.IndexSearcher)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Document (org.apache.lucene.document.Document)1 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)1 DrillSideways (org.apache.lucene.facet.DrillSideways)1 DrillSidewaysResult (org.apache.lucene.facet.DrillSideways.DrillSidewaysResult)1 Facets (org.apache.lucene.facet.Facets)1 FacetsCollector (org.apache.lucene.facet.FacetsCollector)1 FacetsConfig (org.apache.lucene.facet.FacetsConfig)1 MultiFacets (org.apache.lucene.facet.MultiFacets)1 IndexReader (org.apache.lucene.index.IndexReader)1