01: package org.apache.lucene.search;
02:
03: /**
04: * Licensed to the Apache Software Foundation (ASF) under one or more
05: * contributor license agreements. See the NOTICE file distributed with
06: * this work for additional information regarding copyright ownership.
07: * The ASF licenses this file to You under the Apache License, Version 2.0
08: * (the "License"); you may not use this file except in compliance with
09: * the License. You may obtain a copy of the License at
10: *
11: * http://www.apache.org/licenses/LICENSE-2.0
12: *
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.
18: */
19:
20: import java.io.IOException;
21: import java.util.ArrayList;
22: import java.util.BitSet;
23: import java.util.Iterator;
24:
25: import org.apache.lucene.index.IndexReader;
26: import org.apache.lucene.index.Term;
27: import org.apache.lucene.index.TermDocs;
28:
29: /**
30: * Constructs a filter for docs matching any of the terms added to this class.
31: * Unlike a RangeFilter this can be used for filtering on multiple terms that are not necessarily in
32: * a sequence. An example might be a collection of primary keys from a database query result or perhaps
33: * a choice of "category" labels picked by the end user. As a filter, this is much faster than the
34: * equivalent query (a BooleanQuery with many "should" TermQueries)
35: *
36: * @author maharwood
37: */
38: public class TermsFilter extends Filter {
39: ArrayList termsList = new ArrayList();
40:
41: /**
42: * Adds a term to the list of acceptable terms
43: * @param term
44: */
45: public void addTerm(Term term) {
46: termsList.add(term);
47: }
48:
49: /* (non-Javadoc)
50: * @see org.apache.lucene.search.Filter#bits(org.apache.lucene.index.IndexReader)
51: */
52: public BitSet bits(IndexReader reader) throws IOException {
53: BitSet result = new BitSet(reader.maxDoc());
54: for (Iterator iter = termsList.iterator(); iter.hasNext();) {
55: Term term = (Term) iter.next();
56: TermDocs td = reader.termDocs(term);
57: while (td.next()) {
58: result.set(td.doc());
59: }
60: }
61: return result;
62: }
63:
64: public boolean equals(Object obj) {
65: if (this == obj)
66: return true;
67: if ((obj == null) || (obj.getClass() != this .getClass()))
68: return false;
69: TermsFilter test = (TermsFilter) obj;
70: return (termsList == test.termsList || (termsList != null && termsList
71: .equals(test.termsList)));
72: }
73:
74: public int hashCode() {
75: int hash = 9;
76: for (Iterator iter = termsList.iterator(); iter.hasNext();) {
77: Term term = (Term) iter.next();
78: hash = 31 * hash + term.hashCode();
79: }
80: return hash;
81: }
82:
83: }
|