KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > nemesis > forum > search > FieldFilter


1 /**
2  * FieldFilter.java
3  *
4  * Copyright (c) 2000 Douglass R. Cutting.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19  */

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

49 public class FieldFilter extends Filter {
50
51     private String JavaDoc field;
52     private String JavaDoc value;
53     private Term searchTerm;
54
55     /**
56      * Creates a new field filter. The name of the field and the value to filter
57      * on are specified. In order for a Document to pass this filter, it must:
58      * <ol>
59      * <li>The given field must exist in the document.
60      * <li>The field value in the Document must exactly match the given
61      * value.</ol>
62      *
63      * @param field the name of the field to filter on.
64      * @param value the value of the field that search results must match.
65      */

66     public FieldFilter(String JavaDoc field, String JavaDoc value) {
67         this.field = field;
68         this.value = value;
69         searchTerm = new Term(field, value);
70     }
71
72     public BitSet JavaDoc bits(IndexReader reader) throws IOException JavaDoc {
73         //Create a new BitSet with a capacity equal to the size of the index.
74
BitSet JavaDoc bits = new BitSet JavaDoc(reader.maxDoc());
75         //Get an enumeration of all the documents that match the specified field
76
//value.
77
TermDocs matchingDocs = reader.termDocs(searchTerm);
78         try {
79             while(matchingDocs.next()) {
80                 bits.set(matchingDocs.doc());
81             }
82         }
83         finally {
84             if (matchingDocs != null) {
85                 matchingDocs.close();
86             }
87         }
88         return bits;
89     }
90 }
91
Popular Tags