Search in sources :

Example 96 with SortField

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"));
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) TermQuery(org.apache.lucene.search.TermQuery) IndexReader(org.apache.lucene.index.IndexReader) Rescorer(org.apache.lucene.search.Rescorer) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Example 97 with SortField

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);
}
Also used : SortField(org.apache.lucene.search.SortField)

Example 98 with SortField

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();
    }
}
Also used : SortField(org.apache.lucene.search.SortField) DoubleValuesSource(org.apache.lucene.search.DoubleValuesSource)

Example 99 with SortField

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));
    }
}
Also used : TermQuery(org.apache.lucene.search.TermQuery) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) TermQuery(org.apache.lucene.search.TermQuery) FieldDoc(org.apache.lucene.search.FieldDoc) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term) Sort(org.apache.lucene.search.Sort)

Example 100 with SortField

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);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField)

Aggregations

SortField (org.apache.lucene.search.SortField)230 Sort (org.apache.lucene.search.Sort)174 Document (org.apache.lucene.document.Document)116 Directory (org.apache.lucene.store.Directory)110 IndexSearcher (org.apache.lucene.search.IndexSearcher)90 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)75 TopDocs (org.apache.lucene.search.TopDocs)74 IndexReader (org.apache.lucene.index.IndexReader)65 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)62 SortedNumericSortField (org.apache.lucene.search.SortedNumericSortField)56 SortedSetSortField (org.apache.lucene.search.SortedSetSortField)56 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)49 NumericDocValuesField (org.apache.lucene.document.NumericDocValuesField)37 TermQuery (org.apache.lucene.search.TermQuery)36 SortedNumericDocValuesField (org.apache.lucene.document.SortedNumericDocValuesField)32 Query (org.apache.lucene.search.Query)29 Term (org.apache.lucene.index.Term)25 BytesRef (org.apache.lucene.util.BytesRef)25 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)24 StoredField (org.apache.lucene.document.StoredField)23