Search in sources :

Example 31 with BoostQuery

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

the class FieldQueryTest method testFlattenBoolean.

public void testFlattenBoolean() throws Exception {
    initBoost();
    BooleanQuery.Builder booleanQueryB = new BooleanQuery.Builder();
    booleanQueryB.add(tq("A"), Occur.MUST);
    booleanQueryB.add(tq("B"), Occur.MUST);
    booleanQueryB.add(tq("C"), Occur.SHOULD);
    BooleanQuery.Builder innerQuery = new BooleanQuery.Builder();
    innerQuery.add(tq("D"), Occur.MUST);
    innerQuery.add(tq("E"), Occur.MUST);
    booleanQueryB.add(innerQuery.build(), Occur.MUST_NOT);
    Query booleanQuery = booleanQueryB.build();
    booleanQuery = new BoostQuery(booleanQuery, boost);
    FieldQuery fq = new FieldQuery(booleanQuery, true, true);
    Set<Query> flatQueries = new HashSet<>();
    fq.flatten(booleanQuery, reader, flatQueries, 1f);
    assertCollectionQueries(flatQueries, tq(boost, "A"), tq(boost, "B"), tq(boost, "C"));
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) Query(org.apache.lucene.search.Query) PrefixQuery(org.apache.lucene.search.PrefixQuery) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) WildcardQuery(org.apache.lucene.search.WildcardQuery) TermQuery(org.apache.lucene.search.TermQuery) RegexpQuery(org.apache.lucene.search.RegexpQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) ToParentBlockJoinQuery(org.apache.lucene.search.join.ToParentBlockJoinQuery) BoostQuery(org.apache.lucene.search.BoostQuery) ConstantScoreQuery(org.apache.lucene.search.ConstantScoreQuery) TermRangeQuery(org.apache.lucene.search.TermRangeQuery) BoostQuery(org.apache.lucene.search.BoostQuery) HashSet(java.util.HashSet)

Example 32 with BoostQuery

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

the class MoreLikeThis method createQuery.

/**
   * Create the More like query from a PriorityQueue
   */
private Query createQuery(PriorityQueue<ScoreTerm> q) {
    BooleanQuery.Builder query = new BooleanQuery.Builder();
    ScoreTerm scoreTerm;
    float bestScore = -1;
    while ((scoreTerm = q.pop()) != null) {
        Query tq = new TermQuery(new Term(scoreTerm.topField, scoreTerm.word));
        if (boost) {
            if (bestScore == -1) {
                bestScore = (scoreTerm.score);
            }
            float myScore = (scoreTerm.score);
            tq = new BoostQuery(tq, boostFactor * myScore / bestScore);
        }
        try {
            query.add(tq, BooleanClause.Occur.SHOULD);
        } catch (BooleanQuery.TooManyClauses ignore) {
            break;
        }
    }
    return query.build();
}
Also used : BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) Term(org.apache.lucene.index.Term) BoostQuery(org.apache.lucene.search.BoostQuery)

Example 33 with BoostQuery

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

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

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

the class TestFunctionScoreExplanations method testTopLevelBoost.

public void testTopLevelBoost() throws Exception {
    Query q = new TermQuery(new Term(FIELD, "w1"));
    FunctionScoreQuery csq = new FunctionScoreQuery(q, DoubleValuesSource.constant(5));
    BooleanQuery.Builder bqB = new BooleanQuery.Builder();
    bqB.add(new MatchAllDocsQuery(), BooleanClause.Occur.MUST);
    bqB.add(csq, BooleanClause.Occur.MUST);
    BooleanQuery bq = bqB.build();
    qtest(new BoostQuery(bq, 6), new int[] { 0, 1, 2, 3 });
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) 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) Term(org.apache.lucene.index.Term) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) BoostQuery(org.apache.lucene.search.BoostQuery)

Aggregations

BoostQuery (org.apache.lucene.search.BoostQuery)110 Query (org.apache.lucene.search.Query)91 BooleanQuery (org.apache.lucene.search.BooleanQuery)81 TermQuery (org.apache.lucene.search.TermQuery)71 Term (org.apache.lucene.index.Term)48 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)43 PhraseQuery (org.apache.lucene.search.PhraseQuery)32 FuzzyQuery (org.apache.lucene.search.FuzzyQuery)27 PrefixQuery (org.apache.lucene.search.PrefixQuery)27 DisjunctionMaxQuery (org.apache.lucene.search.DisjunctionMaxQuery)25 ConstantScoreQuery (org.apache.lucene.search.ConstantScoreQuery)22 MatchNoDocsQuery (org.apache.lucene.search.MatchNoDocsQuery)21 MultiPhraseQuery (org.apache.lucene.search.MultiPhraseQuery)20 SynonymQuery (org.apache.lucene.search.SynonymQuery)19 WildcardQuery (org.apache.lucene.search.WildcardQuery)19 BooleanClause (org.apache.lucene.search.BooleanClause)18 TermRangeQuery (org.apache.lucene.search.TermRangeQuery)17 MultiTermQuery (org.apache.lucene.search.MultiTermQuery)16 SpanTermQuery (org.apache.lucene.search.spans.SpanTermQuery)16 ArrayList (java.util.ArrayList)14