1 package org.apache.lucene.search;
4 * Copyright 2004 The Apache Software Foundation
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
19 import java.util.List;
21 import org.apache.lucene.analysis.MockAnalyzer;
22 import org.apache.lucene.document.Document;
23 import org.apache.lucene.document.Field;
24 import org.apache.lucene.index.IndexReader;
25 import org.apache.lucene.index.RandomIndexWriter;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.search.spans.SpanTermQuery;
28 import org.apache.lucene.store.Directory;
29 import org.apache.lucene.util.English;
30 import org.apache.lucene.util.LuceneTestCase;
32 public class TestSpanQueryFilter extends LuceneTestCase {
34 public void testFilterWorks() throws Exception {
35 Directory dir = newDirectory();
36 RandomIndexWriter writer = new RandomIndexWriter(random, dir, newIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(random)).setMergePolicy(newLogMergePolicy()));
37 for (int i = 0; i < 500; i++) {
38 Document document = new Document();
39 document.add(newField("field", English.intToEnglish(i) + " equals " + English.intToEnglish(i),
40 Field.Store.NO, Field.Index.ANALYZED));
41 writer.addDocument(document);
43 IndexReader reader = writer.getReader();
46 SpanTermQuery query = new SpanTermQuery(new Term("field", English.intToEnglish(10).trim()));
47 SpanQueryFilter filter = new SpanQueryFilter(query);
48 SpanFilterResult result = filter.bitSpans(reader);
49 DocIdSet docIdSet = result.getDocIdSet();
50 assertTrue("docIdSet is null and it shouldn't be", docIdSet != null);
51 assertContainsDocId("docIdSet doesn't contain docId 10", docIdSet, 10);
52 List<SpanFilterResult.PositionInfo> spans = result.getPositions();
53 assertTrue("spans is null and it shouldn't be", spans != null);
54 int size = getDocIdSetSize(docIdSet);
55 assertTrue("spans Size: " + spans.size() + " is not: " + size, spans.size() == size);
56 for (final SpanFilterResult.PositionInfo info: spans) {
57 assertTrue("info is null and it shouldn't be", info != null);
58 //The doc should indicate the bit is on
59 assertContainsDocId("docIdSet doesn't contain docId " + info.getDoc(), docIdSet, info.getDoc());
60 //There should be two positions in each
61 assertTrue("info.getPositions() Size: " + info.getPositions().size() + " is not: " + 2, info.getPositions().size() == 2);
67 int getDocIdSetSize(DocIdSet docIdSet) throws Exception {
69 DocIdSetIterator it = docIdSet.iterator();
70 while (it.nextDoc() != DocIdSetIterator.NO_MORE_DOCS) {
76 public void assertContainsDocId(String msg, DocIdSet docIdSet, int docId) throws Exception {
77 DocIdSetIterator it = docIdSet.iterator();
78 assertTrue(msg, it.advance(docId) != DocIdSetIterator.NO_MORE_DOCS);
79 assertTrue(msg, it.docID() == docId);