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.Iterator;
23 import java.util.TreeSet;
25 import org.apache.lucene.index.IndexReader;
26 import org.apache.lucene.index.Term;
27 import org.apache.lucene.index.TermDocs;
28 import org.apache.lucene.util.FixedBitSet;
31 * Constructs a filter for docs matching any of the terms added to this class.
32 * Unlike a RangeFilter this can be used for filtering on multiple terms that are not necessarily in
33 * a sequence. An example might be a collection of primary keys from a database query result or perhaps
34 * a choice of "category" labels picked by the end user. As a filter, this is much faster than the
35 * equivalent query (a BooleanQuery with many "should" TermQueries)
38 public class TermsFilter extends Filter
40 Set<Term> terms=new TreeSet<Term>();
43 * Adds a term to the list of acceptable terms
46 public void addTerm(Term term)
52 * @see org.apache.lucene.search.Filter#getDocIdSet(org.apache.lucene.index.IndexReader)
55 public DocIdSet getDocIdSet(IndexReader reader) throws IOException
57 FixedBitSet result=new FixedBitSet(reader.maxDoc());
58 TermDocs td = reader.termDocs();
61 for (Iterator<Term> iter = terms.iterator(); iter.hasNext();)
63 Term term = iter.next();
79 public boolean equals(Object obj)
83 if((obj == null) || (obj.getClass() != this.getClass()))
85 TermsFilter test = (TermsFilter)obj;
86 return (terms == test.terms ||
87 (terms != null && terms.equals(test.terms)));
94 for (Iterator<Term> iter = terms.iterator(); iter.hasNext();)
96 Term term = iter.next();
97 hash = 31 * hash + term.hashCode();