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 java.io.IOException;
21
import java.util.ArrayList;
22
import java.util.Arrays;
23
import java.util.Iterator;
25
import junit.framework.Assert;
27
import org.apache.lucene.document.Document;
28
import org.apache.lucene.document.Field.Index;
29
import org.apache.lucene.document.Field.Store;
30
import org.apache.lucene.index.IndexReader;
31
import org.apache.lucene.index.RandomIndexWriter;
32
import org.apache.lucene.store.Directory;
33
import org.apache.lucene.util.LuceneTestCase;
35
public class TestDocIdSet extends LuceneTestCase {
36
public void testFilteredDocIdSet() throws Exception {
38
final DocIdSet innerSet = new DocIdSet() {
41
public DocIdSetIterator iterator() {
42
return new DocIdSetIterator() {
52
public int nextDoc() throws IOException {
54
return docid < maxdoc ? docid : (docid = NO_MORE_DOCS);
58
public int advance(int target) throws IOException {
59
while (nextDoc() < target) {}
67
DocIdSet filteredSet = new FilteredDocIdSet(innerSet){
69
protected boolean match(int docid) {
70
return docid%2 == 0; //validate only even docids
74
DocIdSetIterator iter = filteredSet.iterator();
75
ArrayList<Integer> list = new ArrayList<Integer>();
76
int doc = iter.advance(3);
77
if (doc != DocIdSetIterator.NO_MORE_DOCS) {
78
list.add(Integer.valueOf(doc));
79
while((doc = iter.nextDoc()) != DocIdSetIterator.NO_MORE_DOCS) {
80
list.add(Integer.valueOf(doc));
84
int[] docs = new int[list.size()];
86
Iterator<Integer> intIter = list.iterator();
87
while(intIter.hasNext()) {
88
docs[c++] = intIter.next().intValue();
90
int[] answer = new int[]{4,6,8};
91
boolean same = Arrays.equals(answer, docs);
93
System.out.println("answer: " + Arrays.toString(answer));
94
System.out.println("gotten: " + Arrays.toString(docs));
99
public void testNullDocIdSet() throws Exception {
100
// Tests that if a Filter produces a null DocIdSet, which is given to
101
// IndexSearcher, everything works fine. This came up in LUCENE-1754.
102
Directory dir = newDirectory();
103
RandomIndexWriter writer = new RandomIndexWriter(random, dir);
104
Document doc = new Document();
105
doc.add(newField("c", "val", Store.NO, Index.NOT_ANALYZED_NO_NORMS));
106
writer.addDocument(doc);
107
IndexReader reader = writer.getReader();
110
// First verify the document is searchable.
111
IndexSearcher searcher = newSearcher(reader);
112
Assert.assertEquals(1, searcher.search(new MatchAllDocsQuery(), 10).totalHits);
114
// Now search w/ a Filter which returns a null DocIdSet
115
Filter f = new Filter() {
117
public DocIdSet getDocIdSet(IndexReader reader) throws IOException {
122
Assert.assertEquals(0, searcher.search(new MatchAllDocsQuery(), f, 10).totalHits);