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.util.HashSet;
21 import org.apache.lucene.document.Document;
22 import org.apache.lucene.document.Field;
23 import org.apache.lucene.index.IndexReader;
24 import org.apache.lucene.index.RandomIndexWriter;
25 import org.apache.lucene.index.Term;
26 import org.apache.lucene.store.Directory;
27 import org.apache.lucene.util.LuceneTestCase;
28 import org.apache.lucene.util.FixedBitSet;
30 public class TermsFilterTest extends LuceneTestCase {
32 public void testCachability() throws Exception
34 TermsFilter a=new TermsFilter();
35 a.addTerm(new Term("field1","a"));
36 a.addTerm(new Term("field1","b"));
37 HashSet<Filter> cachedFilters=new HashSet<Filter>();
39 TermsFilter b=new TermsFilter();
40 b.addTerm(new Term("field1","a"));
41 b.addTerm(new Term("field1","b"));
43 assertTrue("Must be cached",cachedFilters.contains(b));
44 b.addTerm(new Term("field1","a")); //duplicate term
45 assertTrue("Must be cached",cachedFilters.contains(b));
46 b.addTerm(new Term("field1","c"));
47 assertFalse("Must not be cached",cachedFilters.contains(b));
50 public void testMissingTerms() throws Exception {
51 String fieldName="field1";
52 Directory rd=newDirectory();
53 RandomIndexWriter w = new RandomIndexWriter(random, rd);
54 for (int i = 0; i < 100; i++) {
55 Document doc=new Document();
56 int term=i*10; //terms are units of 10;
57 doc.add(newField(fieldName,""+term,Field.Store.YES,Field.Index.NOT_ANALYZED));
60 IndexReader reader = w.getReader();
63 TermsFilter tf=new TermsFilter();
64 tf.addTerm(new Term(fieldName,"19"));
65 FixedBitSet bits = (FixedBitSet)tf.getDocIdSet(reader);
66 assertEquals("Must match nothing", 0, bits.cardinality());
68 tf.addTerm(new Term(fieldName,"20"));
69 bits = (FixedBitSet)tf.getDocIdSet(reader);
70 assertEquals("Must match 1", 1, bits.cardinality());
72 tf.addTerm(new Term(fieldName,"10"));
73 bits = (FixedBitSet)tf.getDocIdSet(reader);
74 assertEquals("Must match 2", 2, bits.cardinality());
76 tf.addTerm(new Term(fieldName,"00"));
77 bits = (FixedBitSet)tf.getDocIdSet(reader);
78 assertEquals("Must match 2", 2, bits.cardinality());