use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestFunctionScoreExplanations method testSubExplanations.
public void testSubExplanations() throws IOException {
Query query = new FunctionScoreQuery(new MatchAllDocsQuery(), DoubleValuesSource.constant(5));
IndexSearcher searcher = newSearcher(BaseExplanationTestCase.searcher.getIndexReader());
searcher.setSimilarity(new BM25Similarity());
Explanation expl = searcher.explain(query, 0);
assertEquals("constant(5.0)", expl.getDescription());
assertEquals(0, expl.getDetails().length);
query = new BoostQuery(query, 2);
expl = searcher.explain(query, 0);
assertEquals(2, expl.getDetails().length);
// function
assertEquals(5f, expl.getDetails()[1].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[0].getDescription());
assertEquals(2f, expl.getDetails()[0].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()[1].getValue(), 0f);
// boost
assertEquals("boost", expl.getDetails()[0].getDescription());
assertEquals(2f, expl.getDetails()[0].getValue(), 0f);
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestFunctionScoreExplanations method testBoost.
public void testBoost() throws Exception {
Query q = new TermQuery(new Term(FIELD, "w1"));
FunctionScoreQuery csq = new FunctionScoreQuery(q, DoubleValuesSource.constant(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 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();
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestFunctionQueryExplanations method testBoost.
public void testBoost() throws Exception {
Query q = new BoostQuery(new FunctionQuery(new ConstValueSource(5)), 2);
qtest(q, new int[] { 0, 1, 2, 3 });
}
use of org.apache.lucene.search.BoostQuery in project lucene-solr by apache.
the class TestQueryParser method testSynonyms.
/** simple synonyms test */
public void testSynonyms() throws Exception {
Query expected = new SynonymQuery(new Term(FIELD, "dogs"), new Term(FIELD, "dog"));
QueryParser qp = new QueryParser(FIELD, new MockSynonymAnalyzer());
assertEquals(expected, qp.parse("dogs"));
assertEquals(expected, qp.parse("\"dogs\""));
qp.setDefaultOperator(Operator.AND);
assertEquals(expected, qp.parse("dogs"));
assertEquals(expected, qp.parse("\"dogs\""));
expected = new BoostQuery(expected, 2f);
assertEquals(expected, qp.parse("dogs^2"));
assertEquals(expected, qp.parse("\"dogs\"^2"));
}
Aggregations