pylucene 3.5.0-3
[pylucene.git] / lucene-java-3.5.0 / lucene / src / test / org / apache / lucene / search / TestSearchAfter.java
1 package org.apache.lucene.search;
2
3 /**
4  * Licensed to the Apache Software Foundation (ASF) under one or more
5  * contributor license agreements.  See the NOTICE file distributed with
6  * this work for additional information regarding copyright ownership.
7  * The ASF licenses this file to You under the Apache License, Version 2.0
8  * (the "License"); you may not use this file except in compliance with
9  * the License.  You may obtain a copy of the License at
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19
20 import org.apache.lucene.document.Document;
21 import org.apache.lucene.document.Field;
22 import org.apache.lucene.index.IndexReader;
23 import org.apache.lucene.index.RandomIndexWriter;
24 import org.apache.lucene.index.Term;
25 import org.apache.lucene.store.Directory;
26 import org.apache.lucene.util.English;
27 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.util._TestUtil;
29
30 /**
31  * Tests IndexSearcher's searchAfter() method
32  */
33 public class TestSearchAfter extends LuceneTestCase {
34   private Directory dir;
35   private IndexReader reader;
36   private IndexSearcher searcher;
37    
38   @Override
39   public void setUp() throws Exception {
40     super.setUp();
41     dir = newDirectory();
42     RandomIndexWriter iw = new RandomIndexWriter(random, dir);
43     int numDocs = atLeast(200);
44     for (int i = 0; i < numDocs; i++) {
45       Document document = new Document();
46       document.add(newField("english", English.intToEnglish(i), Field.Index.ANALYZED));
47       document.add(newField("oddeven", (i % 2 == 0) ? "even" : "odd", Field.Index.ANALYZED));
48       iw.addDocument(document);
49     }
50     reader = iw.getReader();
51     iw.close();
52     searcher = newSearcher(reader);
53   }
54
55   @Override
56   public void tearDown() throws Exception {
57     searcher.close();
58     reader.close();
59     dir.close();
60     super.tearDown();
61   }
62   
63   public void testQueries() throws Exception {
64     Filter odd = new QueryWrapperFilter(new TermQuery(new Term("oddeven", "odd")));
65     assertQuery(new MatchAllDocsQuery(), null);
66     assertQuery(new TermQuery(new Term("english", "one")), null);
67     assertQuery(new MatchAllDocsQuery(), odd);
68     assertQuery(new TermQuery(new Term("english", "four")), odd);
69     BooleanQuery bq = new BooleanQuery();
70     bq.add(new TermQuery(new Term("english", "one")), BooleanClause.Occur.SHOULD);
71     bq.add(new TermQuery(new Term("oddeven", "even")), BooleanClause.Occur.SHOULD);
72     assertQuery(bq, null);
73   }
74   
75   void assertQuery(Query query, Filter filter) throws Exception {
76     TopDocs all = searcher.search(query, filter, searcher.maxDoc());
77     int pageSize = _TestUtil.nextInt(random, 1, searcher.maxDoc()*2);
78     int pageStart = 0;
79     ScoreDoc lastBottom = null;
80     while (pageStart < all.totalHits) {
81       TopDocs paged = searcher.searchAfter(lastBottom, query, filter, pageSize);
82       if (paged.scoreDocs.length == 0) {
83         break;
84       }
85       assertPage(pageStart, all, paged);
86       pageStart += paged.scoreDocs.length;
87       lastBottom = paged.scoreDocs[paged.scoreDocs.length - 1];
88     }
89     assertEquals(all.scoreDocs.length, pageStart);
90   }
91
92   static void assertPage(int pageStart, TopDocs all, TopDocs paged) {
93     assertEquals(all.totalHits, paged.totalHits);
94     for (int i = 0; i < paged.scoreDocs.length; i++) {
95       assertEquals(all.scoreDocs[pageStart + i].doc, paged.scoreDocs[i].doc);
96       assertEquals(all.scoreDocs[pageStart + i].score, paged.scoreDocs[i].score, 0f);
97     }
98   }
99 }