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