01: /**
02: * FieldFilter.java
03: *
04: * Copyright (c) 2000 Douglass R. Cutting.
05: *
06: * This program is free software; you can redistribute it and/or modify
07: * it under the terms of the GNU General Public License as published by
08: * the Free Software Foundation; either version 2 of the License, or
09: * (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: */package org.nemesis.forum.search;
20:
21: import java.io.IOException;
22: import java.util.BitSet;
23:
24: import org.apache.lucene.index.IndexReader;
25: import org.apache.lucene.index.Term;
26: import org.apache.lucene.index.TermDocs;
27: import org.apache.lucene.search.Filter;
28:
29: /**
30: * A Filter that restricts search results to Documents that match a specified
31: * Field value.
32: *
33: * For example, suppose you create a search index to make your catalog of widgets
34: * searchable. When indexing, you add a field to each Document called "color"
35: * that has one of the following values: "blue", "green", "yellow", or "red".
36: * Now suppose that a user is executing a query but only wants to see green
37: * widgets in the results. The following code snippet yields that behavior:
38: * <pre>
39: * //In this example, we assume the Searcher and Query are already defined.
40: * //Define a FieldFilter to only show green colored widgets.
41: * Field myFilter = new FieldFilter("color", "green");
42: * Hits queryResults = mySearcher.execute(myQuery, myFilter);
43: * </pre>
44: *
45: * @author Matt Tucker (matt@Yasna.com)
46: */
47: public class FieldFilter extends Filter {
48:
49: private String field;
50: private String value;
51: private Term searchTerm;
52:
53: /**
54: * Creates a new field filter. The name of the field and the value to filter
55: * on are specified. In order for a Document to pass this filter, it must:
56: * <ol>
57: * <li>The given field must exist in the document.
58: * <li>The field value in the Document must exactly match the given
59: * value.</ol>
60: *
61: * @param field the name of the field to filter on.
62: * @param value the value of the field that search results must match.
63: */
64: public FieldFilter(String field, String value) {
65: this .field = field;
66: this .value = value;
67: searchTerm = new Term(field, value);
68: }
69:
70: public BitSet bits(IndexReader reader) throws IOException {
71: //Create a new BitSet with a capacity equal to the size of the index.
72: BitSet bits = new BitSet(reader.maxDoc());
73: //Get an enumeration of all the documents that match the specified field
74: //value.
75: TermDocs matchingDocs = reader.termDocs(searchTerm);
76: try {
77: while (matchingDocs.next()) {
78: bits.set(matchingDocs.doc());
79: }
80: } finally {
81: if (matchingDocs != null) {
82: matchingDocs.close();
83: }
84: }
85: return bits;
86: }
87: }
|