KickJava   Java API By Example, From Geeks To Geeks.

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

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

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