KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > search > FieldFilter


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.search;
17
18 import java.io.IOException JavaDoc;
19 import java.util.BitSet JavaDoc;
20
21 import org.apache.lucene.index.IndexReader;
22 import org.apache.lucene.index.Term;
23 import org.apache.lucene.index.TermDocs;
24
25 /**
26  * A Filter that restricts search results to Documents that match a set of
27  * specified Field values.
28  *
29  * For example, suppose you create a search index to make your catalog of widgets
30  * searchable. When indexing, you add a field to each Document called "color"
31  * that has one of the following values: "blue", "green", "yellow", or "red".
32  * Now suppose that a user is executing a query but only wants to see green
33  * widgets in the results. The following code snippet yields that behavior:
34  * <pre>
35  * //In this example, we assume the Searcher and Query are already defined.
36  * //Define a FieldFilter to only show green colored widgets.
37  * Field myFilter = new FieldFilter("color", "green");
38  * Hits queryResults = mySearcher.execute(myQuery, myFilter);
39  * </pre>
40  *
41  * @author Matt Tucker (matt@coolservlets.com)
42  */

43 public class FieldFilter extends org.apache.lucene.search.Filter {
44
45     private Term [] searchTerms;
46
47     /**
48      * Creates a new field filter. The name of the field and the values to filter
49      * on are specified. In order for a Document to pass this filter, it must:
50      * <ol>
51      * <li>The given field must exist in the document.
52      * <li>The field value in the Document must exactly match one of the
53      * given values.</ol>
54      *
55      * @param field the name of the field to filter on.
56      * @param values the possible values of the field that search results must
57      * match.
58      */

59     public FieldFilter(String JavaDoc field, String JavaDoc [] values) {
60         searchTerms = new Term[values.length];
61         for (int i=0; i<values.length; i++) {
62             searchTerms[i] = new Term(field, values[i]);
63         }
64     }
65
66     /**
67      * Creates a new field filter. The name of the field and the value to filter
68      * on are specified. In order for a Document to pass this filter, it must:
69      * <ol>
70      * <li>The given field must exist in the document.
71      * <li>The field value in the Document must exactly match one of the
72      * given values.</ol>
73      *
74      * @param field the name of the field to filter on.
75      * @param value the value of the field that search results must match.
76      */

77     public FieldFilter(String JavaDoc field, String JavaDoc value) {
78         this(field, new String JavaDoc[] { value });
79     }
80
81     public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc {
82         //Create a new BitSet with a capacity equal to the size of the index.
83
BitSet JavaDoc bits = new BitSet JavaDoc(reader.maxDoc());
84         //Match all search terms.
85
for (int i=0; i < searchTerms.length; i++) {
86             //Get an enumeration of all the documents that match the specified
87
//field value.
88
TermDocs matchingDocs = reader.termDocs(searchTerms[i]);
89             try {
90                 if (matchingDocs != null) {
91                     while(matchingDocs.next()) {
92                         bits.set(matchingDocs.doc());
93                     }
94                 }
95             }
96             finally {
97                 if (matchingDocs != null) {
98                     matchingDocs.close();
99                 }
100             }
101         }
102         return bits;
103     }
104 }
Popular Tags