Search in sources :

Example 46 with ScoreDoc

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

the class TestLongNormValueSource method assertHits.

void assertHits(Query q, float[] scores) throws Exception {
    ScoreDoc[] expected = new ScoreDoc[scores.length];
    int[] expectedDocs = new int[scores.length];
    for (int i = 0; i < expected.length; i++) {
        expectedDocs[i] = i;
        expected[i] = new ScoreDoc(i, scores[i]);
    }
    TopDocs docs = searcher.search(q, 2, new Sort(new SortField("id", SortField.Type.STRING)));
    /*
    for (int i=0;i<docs.scoreDocs.length;i++) {
      System.out.println(searcher.explain(q, docs.scoreDocs[i].doc));
    }
    */
    CheckHits.checkHits(random(), q, "", searcher, expectedDocs);
    CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);
    CheckHits.checkExplanations(q, "", searcher);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 47 with ScoreDoc

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

the class TestFunctionRangeQuery method testExplain.

@Test
public void testExplain() throws IOException {
    Query rangeQuery = new FunctionRangeQuery(INT_VALUESOURCE, 2, 2, true, true);
    ScoreDoc[] scoreDocs = indexSearcher.search(rangeQuery, N_DOCS).scoreDocs;
    Explanation explain = indexSearcher.explain(rangeQuery, scoreDocs[0].doc);
    // Just validate it looks reasonable
    assertEquals("2.0 = frange(int(" + INT_FIELD + ")):[2 TO 2]\n" + "  2.0 = int(" + INT_FIELD + ")=2\n", explain.toString());
}
Also used : Query(org.apache.lucene.search.Query) Explanation(org.apache.lucene.search.Explanation) ScoreDoc(org.apache.lucene.search.ScoreDoc) Test(org.junit.Test)

Example 48 with ScoreDoc

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

the class TestBoostedQuery method assertHits.

void assertHits(Query q, float[] scores) throws Exception {
    ScoreDoc[] expected = new ScoreDoc[scores.length];
    int[] expectedDocs = new int[scores.length];
    for (int i = 0; i < expected.length; i++) {
        expectedDocs[i] = i;
        expected[i] = new ScoreDoc(i, scores[i]);
    }
    TopDocs docs = is.search(q, 10, new Sort(new SortField("id", SortField.Type.STRING)));
    CheckHits.checkHits(random(), q, "", is, expectedDocs);
    CheckHits.checkHitsQuery(q, expected, docs.scoreDocs, expectedDocs);
    CheckHits.checkExplanations(q, "", is);
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) Sort(org.apache.lucene.search.Sort) SortField(org.apache.lucene.search.SortField) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 49 with ScoreDoc

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

the class TestMultiFieldQueryParser method testStopWordSearching.

public void testStopWordSearching() throws Exception {
    Analyzer analyzer = new MockAnalyzer(random());
    Directory ramDir = newDirectory();
    IndexWriter iw = new IndexWriter(ramDir, newIndexWriterConfig(analyzer));
    Document doc = new Document();
    doc.add(newTextField("body", "blah the footest blah", Field.Store.NO));
    iw.addDocument(doc);
    iw.close();
    MultiFieldQueryParser mfqp = new MultiFieldQueryParser(new String[] { "body" }, analyzer);
    mfqp.setDefaultOperator(QueryParser.Operator.AND);
    Query q = mfqp.parse("the footest");
    IndexReader ir = DirectoryReader.open(ramDir);
    IndexSearcher is = newSearcher(ir);
    ScoreDoc[] hits = is.search(q, 1000).scoreDocs;
    assertEquals(1, hits.length);
    ir.close();
    ramDir.close();
}
Also used : IndexSearcher(org.apache.lucene.search.IndexSearcher) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Query(org.apache.lucene.search.Query) RegexpQuery(org.apache.lucene.search.RegexpQuery) BooleanQuery(org.apache.lucene.search.BooleanQuery) IndexWriter(org.apache.lucene.index.IndexWriter) IndexReader(org.apache.lucene.index.IndexReader) Analyzer(org.apache.lucene.analysis.Analyzer) MockAnalyzer(org.apache.lucene.analysis.MockAnalyzer) Document(org.apache.lucene.document.Document) Directory(org.apache.lucene.store.Directory) ScoreDoc(org.apache.lucene.search.ScoreDoc)

Example 50 with ScoreDoc

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

the class SpatialTestCase method executeQuery.

protected SearchResults executeQuery(Query query, int numDocs) {
    try {
        TopDocs topDocs = indexSearcher.search(query, numDocs);
        List<SearchResult> results = new ArrayList<>();
        for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
            results.add(new SearchResult(scoreDoc.score, indexSearcher.doc(scoreDoc.doc)));
        }
        return new SearchResults(topDocs.totalHits, results);
    } catch (IOException ioe) {
        throw new RuntimeException("IOException thrown while executing query", ioe);
    }
}
Also used : TopDocs(org.apache.lucene.search.TopDocs) ArrayList(java.util.ArrayList) IOException(java.io.IOException) 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