Search in sources :

Example 76 with ScoreDoc

use of org.apache.lucene.search.ScoreDoc 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();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) Query(org.apache.lucene.search.Query) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ScoreDoc(org.apache.lucene.search.ScoreDoc) TopDocs(org.apache.lucene.search.TopDocs) FunctionQuery(org.apache.lucene.queries.function.FunctionQuery) ValueSource(org.apache.lucene.queries.function.ValueSource) IndexReader(org.apache.lucene.index.IndexReader)

Example 77 with ScoreDoc

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

the class ClassificationUpdateProcessorIntegrationTest method getDoc.

private Document getDoc(String id) throws IOException {
    try (SolrQueryRequest req = req()) {
        SolrIndexSearcher searcher = req.getSearcher();
        TermQuery query = new TermQuery(new Term(ID, id));
        TopDocs doc1 = searcher.search(query, 1);
        ScoreDoc scoreDoc = doc1.scoreDocs[0];
        return searcher.doc(scoreDoc.doc);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) TermQuery(org.apache.lucene.search.TermQuery) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Term(org.apache.lucene.index.Term) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 78 with ScoreDoc

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

the class TestNumericRangeQuery64 method testOneMatchQuery.

@Test
public void testOneMatchQuery() throws Exception {
    LegacyNumericRangeQuery<Long> q = LegacyNumericRangeQuery.newLongRange("ascfield8", 8, 1000L, 1000L, true, true);
    TopDocs topDocs = searcher.search(q, noDocs);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", 1, sd.length);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 79 with ScoreDoc

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

the class TestNumericRangeQuery32 method testOneMatchQuery.

@Test
public void testOneMatchQuery() throws Exception {
    LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange("ascfield8", 8, 1000, 1000, true, true);
    TopDocs topDocs = searcher.search(q, noDocs);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", 1, sd.length);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 80 with ScoreDoc

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

the class TestNumericRangeQuery32 method testRightOpenRange.

private void testRightOpenRange(int precisionStep) throws Exception {
    String field = "field" + precisionStep;
    int count = 3000;
    int lower = (count - 1) * distance + (distance / 3) + startOffset;
    LegacyNumericRangeQuery<Integer> q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, true);
    TopDocs topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
    ScoreDoc[] sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", noDocs - count, sd.length);
    Document doc = searcher.doc(sd[0].doc);
    assertEquals("First doc", count * distance + startOffset, doc.getField(field).numericValue().intValue());
    doc = searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (noDocs - 1) * distance + startOffset, doc.getField(field).numericValue().intValue());
    q = LegacyNumericRangeQuery.newIntRange(field, precisionStep, lower, null, true, false);
    topDocs = searcher.search(q, noDocs, Sort.INDEXORDER);
    sd = topDocs.scoreDocs;
    assertNotNull(sd);
    assertEquals("Score doc count", noDocs - count, sd.length);
    doc = searcher.doc(sd[0].doc);
    assertEquals("First doc", count * distance + startOffset, doc.getField(field).numericValue().intValue());
    doc = searcher.doc(sd[sd.length - 1].doc);
    assertEquals("Last doc", (noDocs - 1) * distance + startOffset, doc.getField(field).numericValue().intValue());
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Document(org.apache.lucene.document.Document) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Aggregations

ScoreDoc (org.apache.lucene.search.ScoreDoc)222 TopDocs (org.apache.lucene.search.TopDocs)124 IndexSearcher (org.apache.lucene.search.IndexSearcher)98 Document (org.apache.lucene.document.Document)95 Query (org.apache.lucene.search.Query)71 TermQuery (org.apache.lucene.search.TermQuery)52 IOException (java.io.IOException)48 ArrayList (java.util.ArrayList)46 IndexReader (org.apache.lucene.index.IndexReader)45 Term (org.apache.lucene.index.Term)39 Directory (org.apache.lucene.store.Directory)37 BooleanQuery (org.apache.lucene.search.BooleanQuery)27 MatchAllDocsQuery (org.apache.lucene.search.MatchAllDocsQuery)26 Test (org.junit.Test)23 Sort (org.apache.lucene.search.Sort)22 MockAnalyzer (org.apache.lucene.analysis.MockAnalyzer)21 HashMap (java.util.HashMap)20 RandomIndexWriter (org.apache.lucene.index.RandomIndexWriter)20 FieldDoc (org.apache.lucene.search.FieldDoc)20 QueryParser (org.apache.lucene.queryparser.classic.QueryParser)18