Search in sources :

Example 41 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class FacetsCollector method doSearch.

private static TopDocs doSearch(IndexSearcher searcher, ScoreDoc after, Query q, int n, Sort sort, boolean doDocScores, boolean doMaxScore, Collector fc) throws IOException {
    int limit = searcher.getIndexReader().maxDoc();
    if (limit == 0) {
        limit = 1;
    }
    n = Math.min(n, limit);
    if (after != null && after.doc >= limit) {
        throw new IllegalArgumentException("after.doc exceeds the number of documents in the reader: after.doc=" + after.doc + " limit=" + limit);
    }
    TopDocs topDocs = null;
    if (n == 0) {
        TotalHitCountCollector totalHitCountCollector = new TotalHitCountCollector();
        searcher.search(q, MultiCollector.wrap(totalHitCountCollector, fc));
        topDocs = new TopDocs(totalHitCountCollector.getTotalHits(), new ScoreDoc[0], Float.NaN);
    } else {
        TopDocsCollector<?> hitsCollector;
        if (sort != null) {
            if (after != null && !(after instanceof FieldDoc)) {
                // remove this
                throw new IllegalArgumentException("after must be a FieldDoc; got " + after);
            }
            boolean fillFields = true;
            hitsCollector = TopFieldCollector.create(sort, n, (FieldDoc) after, fillFields, doDocScores, doMaxScore);
        } else {
            hitsCollector = TopScoreDocCollector.create(n, after);
        }
        searcher.search(q, MultiCollector.wrap(hitsCollector, fc));
        topDocs = hitsCollector.topDocs();
    }
    return topDocs;
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) FieldDoc(org.apache.lucene.search.FieldDoc) TotalHitCountCollector(org.apache.lucene.search.TotalHitCountCollector) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 42 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestFunctionQuerySort method testSearchAfterWhenSortingByFunctionValues.

public void testSearchAfterWhenSortingByFunctionValues() throws IOException {
    Directory dir = newDirectory();
    IndexWriterConfig iwc = newIndexWriterConfig(null);
    // depends on docid order
    iwc.setMergePolicy(newLogMergePolicy());
    RandomIndexWriter writer = new RandomIndexWriter(random(), dir, iwc);
    Document doc = new Document();
    Field field = new StoredField("value", 0);
    Field dvField = new NumericDocValuesField("value", 0);
    doc.add(field);
    doc.add(dvField);
    // Save docs unsorted (decreasing value n, n-1, ...)
    final int NUM_VALS = 5;
    for (int val = NUM_VALS; val > 0; val--) {
        field.setIntValue(val);
        dvField.setLongValue(val);
        writer.addDocument(doc);
    }
    // Open index
    IndexReader reader = writer.getReader();
    writer.close();
    IndexSearcher searcher = newSearcher(reader);
    // Trivial ValueSource function that bypasses single field ValueSource sort optimization
    ValueSource src = new SumFloatFunction(new ValueSource[] { new IntFieldSource("value"), new DoubleConstValueSource(1.0D) });
    // ...and make it a sort criterion
    SortField sf = src.getSortField(false).rewrite(searcher);
    Sort orderBy = new Sort(sf);
    // Get hits sorted by our FunctionValues (ascending values)
    Query q = new MatchAllDocsQuery();
    TopDocs hits = searcher.search(q, reader.maxDoc(), orderBy);
    assertEquals(NUM_VALS, hits.scoreDocs.length);
    // Verify that sorting works in general
    int i = 0;
    for (ScoreDoc hit : hits.scoreDocs) {
        int valueFromDoc = Integer.parseInt(reader.document(hit.doc).get("value"));
        assertEquals(++i, valueFromDoc);
    }
    // Now get hits after hit #2 using IS.searchAfter()
    int afterIdx = 1;
    FieldDoc afterHit = (FieldDoc) hits.scoreDocs[afterIdx];
    hits = searcher.searchAfter(afterHit, q, reader.maxDoc(), orderBy);
    // Expected # of hits: NUM_VALS - 2
    assertEquals(NUM_VALS - (afterIdx + 1), hits.scoreDocs.length);
    // Verify that hits are actually "after"
    int afterValue = ((Double) afterHit.fields[0]).intValue();
    for (ScoreDoc hit : hits.scoreDocs) {
        int val = Integer.parseInt(reader.document(hit.doc).get("value"));
        assertTrue(afterValue <= val);
        assertFalse(hit.doc == afterHit.doc);
    }
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) FieldDoc(org.apache.lucene.search.FieldDoc) SortField(org.apache.lucene.search.SortField) Document(org.apache.lucene.document.Document) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) TopDocs(org.apache.lucene.search.TopDocs) StoredField(org.apache.lucene.document.StoredField) SortField(org.apache.lucene.search.SortField) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) Field(org.apache.lucene.document.Field) StoredField(org.apache.lucene.document.StoredField) IntFieldSource(org.apache.lucene.queries.function.valuesource.IntFieldSource) NumericDocValuesField(org.apache.lucene.document.NumericDocValuesField) DoubleConstValueSource(org.apache.lucene.queries.function.valuesource.DoubleConstValueSource) IndexReader(org.apache.lucene.index.IndexReader) SumFloatFunction(org.apache.lucene.queries.function.valuesource.SumFloatFunction) Sort(org.apache.lucene.search.Sort) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory) IndexWriterConfig(org.apache.lucene.index.IndexWriterConfig)

Example 43 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestLatLonPointDistanceSort method testDistanceSort.

/** Add three points and sort by distance */
public void testDistanceSort() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    // add some docs
    Document doc = new Document();
    doc.add(new LatLonDocValuesField("location", 40.759011, -73.9844722));
    iw.addDocument(doc);
    doc = new Document();
    doc.add(new LatLonDocValuesField("location", 40.718266, -74.007819));
    iw.addDocument(doc);
    doc = new Document();
    doc.add(new LatLonDocValuesField("location", 40.7051157, -74.0088305));
    iw.addDocument(doc);
    IndexReader reader = iw.getReader();
    IndexSearcher searcher = newSearcher(reader);
    iw.close();
    Sort sort = new Sort(LatLonDocValuesField.newDistanceSort("location", 40.7143528, -74.0059731));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
    FieldDoc d = (FieldDoc) td.scoreDocs[0];
    assertEquals(462.1028401330431, (Double) d.fields[0], 0.0D);
    d = (FieldDoc) td.scoreDocs[1];
    assertEquals(1054.9842850974826, (Double) d.fields[0], 0.0D);
    d = (FieldDoc) td.scoreDocs[2];
    assertEquals(5285.881528419706, (Double) d.fields[0], 0.0D);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) FieldDoc(org.apache.lucene.search.FieldDoc) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 44 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestLatLonPointDistanceSort method testMissingLast.

/** Add two points (one doc missing) and sort by distance */
public void testMissingLast() throws Exception {
    Directory dir = newDirectory();
    RandomIndexWriter iw = new RandomIndexWriter(random(), dir);
    // missing
    Document doc = new Document();
    iw.addDocument(doc);
    doc = new Document();
    doc.add(new LatLonDocValuesField("location", 40.718266, -74.007819));
    iw.addDocument(doc);
    doc = new Document();
    doc.add(new LatLonDocValuesField("location", 40.7051157, -74.0088305));
    iw.addDocument(doc);
    IndexReader reader = iw.getReader();
    IndexSearcher searcher = newSearcher(reader);
    iw.close();
    Sort sort = new Sort(LatLonDocValuesField.newDistanceSort("location", 40.7143528, -74.0059731));
    TopDocs td = searcher.search(new MatchAllDocsQuery(), 3, sort);
    FieldDoc d = (FieldDoc) td.scoreDocs[0];
    assertEquals(462.1028401330431D, (Double) d.fields[0], 0.0D);
    d = (FieldDoc) td.scoreDocs[1];
    assertEquals(1054.9842850974826, (Double) d.fields[0], 0.0D);
    d = (FieldDoc) td.scoreDocs[2];
    assertEquals(Double.POSITIVE_INFINITY, (Double) d.fields[0], 0.0D);
    reader.close();
    dir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) TopDocs(org.apache.lucene.search.TopDocs) FieldDoc(org.apache.lucene.search.FieldDoc) IndexReader(org.apache.lucene.index.IndexReader) Sort(org.apache.lucene.search.Sort) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) RandomIndexWriter(org.apache.lucene.index.RandomIndexWriter) Directory(org.apache.lucene.store.Directory)

Example 45 with FieldDoc

use of org.apache.lucene.search.FieldDoc in project lucene-solr by apache.

the class TestDemoExpressions method testTwoOfSameBinding.

/** tests same binding used more than once in an expression */
public void testTwoOfSameBinding() throws Exception {
    Expression expr = JavascriptCompiler.compile("_score + _score");
    SimpleBindings bindings = new SimpleBindings();
    bindings.add(new SortField("_score", SortField.Type.SCORE));
    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 = 2 * 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) Sort(org.apache.lucene.search.Sort) TopFieldDocs(org.apache.lucene.search.TopFieldDocs) SortField(org.apache.lucene.search.SortField) Term(org.apache.lucene.index.Term)

Aggregations

FieldDoc (org.apache.lucene.search.FieldDoc)50 Sort (org.apache.lucene.search.Sort)26 TopFieldDocs (org.apache.lucene.search.TopFieldDocs)23 SortField (org.apache.lucene.search.SortField)21 ScoreDoc (org.apache.lucene.search.ScoreDoc)20 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)18 IndexSearcher (org.apache.lucene.search.IndexSearcher)17 ArrayList (java.util.ArrayList)14 Directory (org.apache.lucene.store.Directory)14 TopDocs (org.apache.lucene.search.TopDocs)12 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)10 TermQuery (org.apache.lucene.search.TermQuery)10 Document (org.apache.lucene.document.Document)9 Term (org.apache.lucene.index.Term)9 BytesRef (org.apache.lucene.util.BytesRef)9 HashMap (java.util.HashMap)8 Query (org.apache.lucene.search.Query)8 IndexReader (org.apache.lucene.index.IndexReader)7 HashSet (java.util.HashSet)5 DirectoryReader (org.apache.lucene.index.DirectoryReader)5