Search in sources :

Example 6 with DoubleValuesSource

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

the class ExpressionValueSource method explain.

@Override
public Explanation explain(LeafReaderContext ctx, int docId, Explanation scoreExplanation) throws IOException {
    Explanation[] explanations = new Explanation[variables.length];
    DoubleValues dv = getValues(ctx, DoubleValuesSource.constant(scoreExplanation.getValue()).getValues(ctx, null));
    if (dv.advanceExact(docId) == false) {
        return Explanation.noMatch(expression.sourceText);
    }
    int i = 0;
    for (DoubleValuesSource var : variables) {
        explanations[i++] = var.explain(ctx, docId, scoreExplanation);
    }
    return Explanation.match((float) dv.doubleValue(), expression.sourceText + ", computed from:", explanations);
}
Also used : Explanation(org.apache.lucene.search.Explanation) DoubleValues(org.apache.lucene.search.DoubleValues) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 7 with DoubleValuesSource

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

the class TestExpressionValueSource method testDoubleValuesSourceEquals.

public void testDoubleValuesSourceEquals() throws Exception {
    Expression expr = JavascriptCompiler.compile("sqrt(a) + ln(b)");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("a", SortField.Type.INT));
    bindings.add(new SortField("b", SortField.Type.INT));
    DoubleValuesSource vs1 = expr.getDoubleValuesSource(bindings);
    // same instance
    assertEquals(vs1, vs1);
    // null
    assertFalse(vs1.equals(null));
    // other object
    assertFalse(vs1.equals("foobar"));
    // same bindings and expression instances
    DoubleValuesSource vs2 = expr.getDoubleValuesSource(bindings);
    assertEquals(vs1.hashCode(), vs2.hashCode());
    assertEquals(vs1, vs2);
    // equiv bindings (different instance)
    SimpleBindings bindings2 = new SimpleBindings();
    bindings2.add(new SortField("a", SortField.Type.INT));
    bindings2.add(new SortField("b", SortField.Type.INT));
    DoubleValuesSource vs3 = expr.getDoubleValuesSource(bindings2);
    assertEquals(vs1, vs3);
    // different bindings (same names, different types)
    SimpleBindings bindings3 = new SimpleBindings();
    bindings3.add(new SortField("a", SortField.Type.LONG));
    bindings3.add(new SortField("b", SortField.Type.FLOAT));
    DoubleValuesSource vs4 = expr.getDoubleValuesSource(bindings3);
    assertFalse(vs1.equals(vs4));
}
Also used : SortField(org.apache.lucene.search.SortField) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 8 with DoubleValuesSource

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

the class TestExpressionValueSource method testDoubleValuesSourceTypes.

public void testDoubleValuesSourceTypes() throws Exception {
    Expression expr = JavascriptCompiler.compile("2*popularity + count");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("popularity", SortField.Type.LONG));
    bindings.add(new SortField("count", SortField.Type.LONG));
    DoubleValuesSource vs = expr.getDoubleValuesSource(bindings);
    assertEquals(1, reader.leaves().size());
    LeafReaderContext leaf = reader.leaves().get(0);
    DoubleValues values = vs.getValues(leaf, null);
    assertTrue(values.advanceExact(0));
    assertEquals(10, values.doubleValue(), 0);
    assertTrue(values.advanceExact(1));
    assertEquals(41, values.doubleValue(), 0);
    assertTrue(values.advanceExact(2));
    assertEquals(4, values.doubleValue(), 0);
}
Also used : DoubleValues(org.apache.lucene.search.DoubleValues) LeafReaderContext(org.apache.lucene.index.LeafReaderContext) SortField(org.apache.lucene.search.SortField) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 9 with DoubleValuesSource

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

the class TestFunctionScoreExplanations method testExplanationsIncludingScore.

public void testExplanationsIncludingScore() throws Exception {
    DoubleValuesSource scores = DoubleValuesSource.function(DoubleValuesSource.SCORES, "v * 2", v -> v * 2);
    Query q = new TermQuery(new Term(FIELD, "w1"));
    FunctionScoreQuery csq = new FunctionScoreQuery(q, scores);
    qtest(csq, new int[] { 0, 1, 2, 3 });
    Explanation e1 = searcher.explain(q, 0);
    Explanation e = searcher.explain(csq, 0);
    assertEquals(e.getDetails().length, 2);
    assertEquals(e1.getValue() * 2, e.getValue(), 0.00001);
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Explanation(org.apache.lucene.search.Explanation) Term(org.apache.lucene.index.Term) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 10 with DoubleValuesSource

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

the class TestFunctionScoreQuery method testBoostsAreAppliedLast.

// check boosts with non-distributive score source
public void testBoostsAreAppliedLast() throws Exception {
    DoubleValuesSource scores = DoubleValuesSource.function(DoubleValuesSource.SCORES, "ln(v + 4)", v -> Math.log(v + 4));
    Query q1 = new FunctionScoreQuery(new TermQuery(new Term(TEXT_FIELD, "text")), scores);
    TopDocs plain = searcher.search(q1, 5);
    Query boosted = new BoostQuery(q1, 2);
    TopDocs afterboost = searcher.search(boosted, 5);
    assertEquals(plain.totalHits, afterboost.totalHits);
    for (int i = 0; i < 5; i++) {
        assertEquals(plain.scoreDocs[i].doc, afterboost.scoreDocs[i].doc);
        assertEquals(plain.scoreDocs[i].score, afterboost.scoreDocs[i].score / 2, 0.0001);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource) BoostQuery(org.apache.lucene.search.BoostQuery)

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