Search in sources :

Example 1 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class SpatialFileQueryMaker method makeQueryFromShape.

protected Query makeQueryFromShape(Shape shape) {
    SpatialArgs args = new SpatialArgs(operation, shape);
    if (!Double.isNaN(distErrPct))
        args.setDistErrPct(distErrPct);
    Query filterQuery = strategy.makeQuery(args);
    if (score) {
        //wrap with distance computing query
        ValueSource valueSource = strategy.makeDistanceValueSource(shape.getCenter());
        return new BooleanQuery.Builder().add(new FunctionQuery(valueSource), //matches everything and provides score
        BooleanClause.Occur.MUST).add(filterQuery, //filters (score isn't used)
        BooleanClause.Occur.FILTER).build();
    } else {
        // assume constant scoring
        return filterQuery;
    }
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) SpatialArgs(org.apache.lucene.spatial.query.SpatialArgs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ValueSource(org.apache.lucene.queries.function.ValueSource)

Example 2 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestOrdValues method doTestExactScore.

// Test that queries based on reverse/ordFieldScore returns docs with expected score.
private void doTestExactScore(String field, boolean inOrder) throws Exception {
    IndexReader r = DirectoryReader.open(dir);
    IndexSearcher s = newSearcher(r);
    ValueSource vs;
    if (inOrder) {
        vs = new OrdFieldSource(field);
    } else {
        vs = new ReverseOrdFieldSource(field);
    }
    Query q = new FunctionQuery(vs);
    TopDocs td = s.search(q, 1000);
    assertEquals("All docs should be matched!", N_DOCS, td.totalHits);
    ScoreDoc[] sd = td.scoreDocs;
    for (int i = 0; i < sd.length; i++) {
        float score = sd[i].score;
        String id = s.getIndexReader().document(sd[i].doc).get(ID_FIELD);
        log("-------- " + i + ". Explain doc " + id);
        log(s.explain(q, sd[i].doc));
        float expectedScore = N_DOCS - i - 1;
        assertEquals("score of result " + i + " should be " + expectedScore + " != " + score, expectedScore, score, TEST_SCORE_TOLERANCE_DELTA);
        String expectedId = inOrder ? // in-order ==> larger  values first
        id2String(N_DOCS - i) : // reverse  ==> smaller values first
        id2String(i + 1);
        assertTrue("id of result " + i + " should be " + expectedId + " != " + score, expectedId.equals(id));
    }
    r.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader)

Example 3 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestCustomScoreExplanations method testSubExplanations.

public void testSubExplanations() throws IOException {
    Query query = new FunctionQuery(new ConstValueSource(5));
    IndexSearcher searcher = newSearcher(BaseExplanationTestCase.searcher.getIndexReader());
    searcher.setSimilarity(new BM25Similarity());
    Explanation expl = searcher.explain(query, 0);
    assertEquals(2, expl.getDetails().length);
    // function
    assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
    // boost
    assertEquals("boost", expl.getDetails()[1].getDescription());
    assertEquals(1f, expl.getDetails()[1].getValue(), 0f);
    query = new BoostQuery(query, 2);
    expl = searcher.explain(query, 0);
    assertEquals(2, expl.getDetails().length);
    // function
    assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
    // boost
    assertEquals("boost", expl.getDetails()[1].getDescription());
    assertEquals(2f, expl.getDetails()[1].getValue(), 0f);
    // in order to have a queryNorm != 1
    searcher.setSimilarity(new ClassicSimilarity());
    expl = searcher.explain(query, 0);
    assertEquals(2, expl.getDetails().length);
    // function
    assertEquals(5f, expl.getDetails()[0].getValue(), 0f);
    // boost
    assertEquals("boost", expl.getDetails()[1].getDescription());
    assertEquals(2f, expl.getDetails()[1].getValue(), 0f);
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) ClassicSimilarity(org.apache.lucene.search.similarities.ClassicSimilarity) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) 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) BM25Similarity(org.apache.lucene.search.similarities.BM25Similarity) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 4 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestCustomScoreExplanations method testBoost.

public void testBoost() throws Exception {
    Query q = new TermQuery(new Term(FIELD, "w1"));
    CustomScoreQuery csq = new CustomScoreQuery(q, new FunctionQuery(new ConstValueSource(5)));
    qtest(new BoostQuery(csq, 4), new int[] { 0, 1, 2, 3 });
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) Term(org.apache.lucene.index.Term) ConstValueSource(org.apache.lucene.queries.function.valuesource.ConstValueSource) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 5 with FunctionQuery

use of org.apache.lucene.queries.function.FunctionQuery in project lucene-solr by apache.

the class TestCustomScoreQuery method doTestCustomScore.

// Test that FieldScoreQuery returns docs with expected score.
private void doTestCustomScore(ValueSource valueSource, double dboost) throws Exception {
    float boost = (float) dboost;
    FunctionQuery functionQuery = new FunctionQuery(valueSource);
    IndexReader r = DirectoryReader.open(dir);
    IndexSearcher s = newSearcher(r);
    // regular (boolean) query.
    BooleanQuery.Builder q1b = new BooleanQuery.Builder();
    q1b.add(new TermQuery(new Term(TEXT_FIELD, "first")), BooleanClause.Occur.SHOULD);
    q1b.add(new TermQuery(new Term(TEXT_FIELD, "aid")), BooleanClause.Occur.SHOULD);
    q1b.add(new TermQuery(new Term(TEXT_FIELD, "text")), BooleanClause.Occur.SHOULD);
    Query q1 = q1b.build();
    log(q1);
    // custom query, that should score the same as q1.
    BooleanQuery.Builder q2CustomNeutralB = new BooleanQuery.Builder();
    Query q2CustomNeutralInner = new CustomScoreQuery(q1);
    q2CustomNeutralB.add(new BoostQuery(q2CustomNeutralInner, (float) Math.sqrt(dboost)), BooleanClause.Occur.SHOULD);
    // a little tricky: we split the boost across an outer BQ and CustomScoreQuery
    // this ensures boosting is correct across all these functions (see LUCENE-4935)
    Query q2CustomNeutral = q2CustomNeutralB.build();
    q2CustomNeutral = new BoostQuery(q2CustomNeutral, (float) Math.sqrt(dboost));
    log(q2CustomNeutral);
    // custom query, that should (by default) multiply the scores of q1 by that of the field
    Query q3CustomMul;
    {
        CustomScoreQuery csq = new CustomScoreQuery(q1, functionQuery);
        q3CustomMul = csq;
    }
    q3CustomMul = new BoostQuery(q3CustomMul, boost);
    log(q3CustomMul);
    // custom query, that should add the scores of q1 to that of the field
    Query q4CustomAdd;
    {
        CustomScoreQuery csq = new CustomAddQuery(q1, functionQuery);
        q4CustomAdd = csq;
    }
    q4CustomAdd = new BoostQuery(q4CustomAdd, boost);
    log(q4CustomAdd);
    // custom query, that multiplies and adds the field score to that of q1
    Query q5CustomMulAdd;
    {
        CustomScoreQuery csq = new CustomMulAddQuery(q1, functionQuery, functionQuery);
        q5CustomMulAdd = csq;
    }
    q5CustomMulAdd = new BoostQuery(q5CustomMulAdd, boost);
    log(q5CustomMulAdd);
    // do al the searches 
    TopDocs td1 = s.search(q1, 1000);
    TopDocs td2CustomNeutral = s.search(q2CustomNeutral, 1000);
    TopDocs td3CustomMul = s.search(q3CustomMul, 1000);
    TopDocs td4CustomAdd = s.search(q4CustomAdd, 1000);
    TopDocs td5CustomMulAdd = s.search(q5CustomMulAdd, 1000);
    // put results in map so we can verify the scores although they have changed
    Map<Integer, Float> h1 = topDocsToMap(td1);
    Map<Integer, Float> h2CustomNeutral = topDocsToMap(td2CustomNeutral);
    Map<Integer, Float> h3CustomMul = topDocsToMap(td3CustomMul);
    Map<Integer, Float> h4CustomAdd = topDocsToMap(td4CustomAdd);
    Map<Integer, Float> h5CustomMulAdd = topDocsToMap(td5CustomMulAdd);
    verifyResults(boost, s, h1, h2CustomNeutral, h3CustomMul, h4CustomAdd, h5CustomMulAdd, q1, q2CustomNeutral, q3CustomMul, q4CustomAdd, q5CustomMulAdd);
    r.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) BooleanQuery(org.apache.lucene.search.BooleanQuery) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) BoostQuery(org.apache.lucene.search.BoostQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery) TopDocs(org.apache.lucene.search.TopDocs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) IndexReader(org.apache.lucene.index.IndexReader)

Aggregations

FunctionQuery (org.apache.lucene.queries.function.FunctionQuery)20 Query (org.apache.lucene.search.Query)17 ValueSource (org.apache.lucene.queries.function.ValueSource)11 BooleanQuery (org.apache.lucene.search.BooleanQuery)10 BoostQuery (org.apache.lucene.search.BoostQuery)8 TermQuery (org.apache.lucene.search.TermQuery)7 QueryValueSource (org.apache.lucene.queries.function.valuesource.QueryValueSource)6 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)6 Term (org.apache.lucene.index.Term)4 ConstValueSource (org.apache.lucene.queries.function.valuesource.ConstValueSource)4 IndexSearcher (org.apache.lucene.search.IndexSearcher)4 IndexReader (org.apache.lucene.index.IndexReader)3 ScoreDoc (org.apache.lucene.search.ScoreDoc)3 TopDocs (org.apache.lucene.search.TopDocs)3 ArrayList (java.util.ArrayList)2 BoostedQuery (org.apache.lucene.queries.function.BoostedQuery)2 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)2 SolrException (org.apache.solr.common.SolrException)2 SolrParams (org.apache.solr.common.params.SolrParams)2 SchemaField (org.apache.solr.schema.SchemaField)2