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"));
}
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();
}
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);
}
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 });
}
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 });
}
Aggregations