Search in sources :

Example 1 with DoubleValuesSource

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

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

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

the class TestFunctionScoreQuery method testScoreModifyingSource.

// CustomScoreQuery and BoostedQuery equivalent
public void testScoreModifyingSource() throws Exception {
    DoubleValuesSource iii = DoubleValuesSource.fromIntField("iii");
    DoubleValuesSource score = DoubleValuesSource.scoringFunction(iii, "v * s", (v, s) -> v * s);
    BooleanQuery bq = new BooleanQuery.Builder().add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD).add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD).build();
    TopDocs plain = searcher.search(bq, 1);
    FunctionScoreQuery fq = new FunctionScoreQuery(bq, score);
    QueryUtils.check(random(), fq, searcher, rarely());
    int[] expectedDocs = new int[] { 4, 7, 9, 8, 12 };
    TopDocs docs = searcher.search(fq, 5);
    assertEquals(plain.totalHits, docs.totalHits);
    for (int i = 0; i < expectedDocs.length; i++) {
        assertEquals(expectedDocs[i], docs.scoreDocs[i].doc);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Term(org.apache.lucene.index.Term) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 4 with DoubleValuesSource

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

the class TestFunctionMatchQuery method testRangeMatching.

public void testRangeMatching() throws IOException {
    DoubleValuesSource in = DoubleValuesSource.fromFloatField(FLOAT_FIELD);
    FunctionMatchQuery fmq = new FunctionMatchQuery(in, d -> d >= 2 && d < 4);
    TopDocs docs = searcher.search(fmq, 10);
    assertEquals(2, docs.totalHits);
    assertEquals(9, docs.scoreDocs[0].doc);
    assertEquals(13, docs.scoreDocs[1].doc);
    QueryUtils.check(random(), fmq, searcher, rarely());
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 5 with DoubleValuesSource

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

the class SimpleBindings method getDoubleValuesSource.

@Override
public DoubleValuesSource getDoubleValuesSource(String name) {
    Object o = map.get(name);
    if (o == null) {
        throw new IllegalArgumentException("Invalid reference '" + name + "'");
    } else if (o instanceof Expression) {
        return ((Expression) o).getDoubleValuesSource(this);
    } else if (o instanceof DoubleValuesSource) {
        return ((DoubleValuesSource) o);
    }
    SortField field = (SortField) o;
    switch(field.getType()) {
        case INT:
            return DoubleValuesSource.fromIntField(field.getField());
        case LONG:
            return DoubleValuesSource.fromLongField(field.getField());
        case FLOAT:
            return DoubleValuesSource.fromFloatField(field.getField());
        case DOUBLE:
            return DoubleValuesSource.fromDoubleField(field.getField());
        case SCORE:
            return DoubleValuesSource.SCORES;
        default:
            throw new UnsupportedOperationException();
    }
}
Also used : SortField(org.apache.lucene.search.SortField) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Aggregations

DoubleValuesSource (org.apache.lucene.search.DoubleValuesSource)12 DoubleValues (org.apache.lucene.search.DoubleValues)4 Explanation (org.apache.lucene.search.Explanation)4 Query (org.apache.lucene.search.Query)4 DrillDownQuery (org.apache.lucene.facet.DrillDownQuery)3 FacetsCollector (org.apache.lucene.facet.FacetsCollector)3 LeafReaderContext (org.apache.lucene.index.LeafReaderContext)3 Term (org.apache.lucene.index.Term)3 BooleanQuery (org.apache.lucene.search.BooleanQuery)3 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)3 SortField (org.apache.lucene.search.SortField)3 TermQuery (org.apache.lucene.search.TermQuery)3 TopDocs (org.apache.lucene.search.TopDocs)3 Document (org.apache.lucene.document.Document)2 DrillSideways (org.apache.lucene.facet.DrillSideways)2 Facets (org.apache.lucene.facet.Facets)2 FacetsConfig (org.apache.lucene.facet.FacetsConfig)2 MultiFacets (org.apache.lucene.facet.MultiFacets)2 IndexReader (org.apache.lucene.index.IndexReader)2 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)2