use of org.apache.lucene.search.SortField in project lucene-solr by apache.
the class TestExpressionRescorer method testBasic.
public void testBasic() throws Exception {
// create a sort field and sort by it (reverse order)
Query query = new TermQuery(new Term("body", "contents"));
IndexReader r = searcher.getIndexReader();
// Just first pass query
TopDocs hits = searcher.search(query, 10);
assertEquals(3, hits.totalHits);
assertEquals("3", r.document(hits.scoreDocs[0].doc).get("id"));
assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id"));
assertEquals("2", r.document(hits.scoreDocs[2].doc).get("id"));
// Now, rescore:
Expression e = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("popularity", SortField.Type.INT));
bindings.add(new SortField("_score", SortField.Type.SCORE));
Rescorer rescorer = e.getRescorer(bindings);
hits = rescorer.rescore(searcher, hits, 10);
assertEquals(3, hits.totalHits);
assertEquals("2", r.document(hits.scoreDocs[0].doc).get("id"));
assertEquals("1", r.document(hits.scoreDocs[1].doc).get("id"));
assertEquals("3", r.document(hits.scoreDocs[2].doc).get("id"));
String expl = rescorer.explain(searcher, searcher.explain(query, hits.scoreDocs[0].doc), hits.scoreDocs[0].doc).toString();
// Confirm the explanation breaks out the individual
// variables:
assertTrue(expl.contains("= double(popularity)"));
// Confirm the explanation includes first pass details:
assertTrue(expl.contains("= first pass score"));
assertTrue(expl.contains("body:contents in"));
}
use of org.apache.lucene.search.SortField in project lucene-solr by apache.
the class TestExpressionSortField method testEquals.
public void testEquals() throws Exception {
Expression expr = JavascriptCompiler.compile("sqrt(_score) + ln(popularity)");
SimpleBindings bindings = new SimpleBindings();
bindings.add(new SortField("_score", SortField.Type.SCORE));
bindings.add(new SortField("popularity", SortField.Type.INT));
SimpleBindings otherBindings = new SimpleBindings();
otherBindings.add(new SortField("_score", SortField.Type.LONG));
otherBindings.add(new SortField("popularity", SortField.Type.INT));
SortField sf1 = expr.getSortField(bindings, true);
// different order
SortField sf2 = expr.getSortField(bindings, false);
assertFalse(sf1.equals(sf2));
// different bindings
sf2 = expr.getSortField(otherBindings, true);
assertFalse(sf1.equals(sf2));
// different expression
Expression other = JavascriptCompiler.compile("popularity/2");
sf2 = other.getSortField(bindings, true);
assertFalse(sf1.equals(sf2));
// null
assertFalse(sf1.equals(null));
// same instance:
assertEquals(sf1, sf1);
}
use of org.apache.lucene.search.SortField in project lucene-solr by apache.
the class SimpleBindings method getDoubleValuesSource.
@Override
public DoubleValuesSource getDoubleValuesSource(String name) {
Object o = map.get(name);
if (o == null) {
throw new IllegalArgumentException("Invalid reference '" + name + "'");
} else if (o instanceof Expression) {
return ((Expression) o).getDoubleValuesSource(this);
} else if (o instanceof DoubleValuesSource) {
return ((DoubleValuesSource) o);
}
SortField field = (SortField) o;
switch(field.getType()) {
case INT:
return DoubleValuesSource.fromIntField(field.getField());
case LONG:
return DoubleValuesSource.fromLongField(field.getField());
case FLOAT:
return DoubleValuesSource.fromFloatField(field.getField());
case DOUBLE:
return DoubleValuesSource.fromDoubleField(field.getField());
case SCORE:
return DoubleValuesSource.SCORES;
default:
throw new UnsupportedOperationException();
}
}
use of org.apache.lucene.search.SortField in project lucene-solr by apache.
the class TestDemoExpressions method doTestLotsOfBindings.
private void doTestLotsOfBindings(int n) throws Exception {
SimpleBindings bindings = new SimpleBindings();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
if (i > 0) {
sb.append("+");
}
sb.append("x" + i);
bindings.add(new SortField("x" + i, SortField.Type.SCORE));
}
Expression expr = JavascriptCompiler.compile(sb.toString());
Sort sort = new Sort(expr.getSortField(bindings, true));
Query query = new TermQuery(new Term("body", "contents"));
TopFieldDocs td = searcher.search(query, 3, sort, true, true);
for (int i = 0; i < 3; i++) {
FieldDoc d = (FieldDoc) td.scoreDocs[i];
float expected = n * d.score;
float actual = ((Double) d.fields[0]).floatValue();
assertEquals(expected, actual, CheckHits.explainToleranceDelta(expected, actual));
}
}
use of org.apache.lucene.search.SortField in project lucene-solr by apache.
the class TestExpressionSorts method assertQuery.
void assertQuery(Query query, Sort sort) throws Exception {
int size = TestUtil.nextInt(random(), 1, searcher.getIndexReader().maxDoc() / 5);
TopDocs expected = searcher.search(query, size, sort, random().nextBoolean(), random().nextBoolean());
// make our actual sort, mutating original by replacing some of the
// sortfields with equivalent expressions
SortField[] original = sort.getSort();
SortField[] mutated = new SortField[original.length];
for (int i = 0; i < mutated.length; i++) {
if (random().nextInt(3) > 0) {
SortField s = original[i];
Expression expr = JavascriptCompiler.compile(s.getField());
SimpleBindings simpleBindings = new SimpleBindings();
simpleBindings.add(s);
boolean reverse = s.getType() == SortField.Type.SCORE || s.getReverse();
mutated[i] = expr.getSortField(simpleBindings, reverse);
} else {
mutated[i] = original[i];
}
}
Sort mutatedSort = new Sort(mutated);
TopDocs actual = searcher.search(query, size, mutatedSort, random().nextBoolean(), random().nextBoolean());
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
if (size < actual.totalHits) {
expected = searcher.searchAfter(expected.scoreDocs[size - 1], query, size, sort);
actual = searcher.searchAfter(actual.scoreDocs[size - 1], query, size, mutatedSort);
CheckHits.checkEqual(query, expected.scoreDocs, actual.scoreDocs);
}
}
Aggregations