1 package org.apache.lucene.search;
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
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;
31 * Tests IndexSearcher's searchAfter() method
33 public class TestSearchAfter extends LuceneTestCase {
34 private Directory dir;
35 private IndexReader reader;
36 private IndexSearcher searcher;
39 public void setUp() throws Exception {
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);
50 reader = iw.getReader();
52 searcher = newSearcher(reader);
56 public void tearDown() throws Exception {
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);
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);
79 ScoreDoc lastBottom = null;
80 while (pageStart < all.totalHits) {
81 TopDocs paged = searcher.searchAfter(lastBottom, query, filter, pageSize);
82 if (paged.scoreDocs.length == 0) {
85 assertPage(pageStart, all, paged);
86 pageStart += paged.scoreDocs.length;
87 lastBottom = paged.scoreDocs[paged.scoreDocs.length - 1];
89 assertEquals(all.scoreDocs.length, pageStart);
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);