KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > searcher > RawFieldQueryFilter


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.searcher;
5
6 import org.apache.lucene.search.BooleanQuery;
7 import org.apache.lucene.search.TermQuery;
8 import org.apache.lucene.index.Term;
9
10 import net.nutch.searcher.Query.Clause;
11
12 /** Translate raw query fields to search the same-named field, as indexed by an
13  * IndexingFilter. */

14 public abstract class RawFieldQueryFilter implements QueryFilter {
15   private String JavaDoc field;
16   private boolean lowerCase;
17   private float boost;
18
19   /** Construct for the named field, lowercasing query values.*/
20   protected RawFieldQueryFilter(String JavaDoc field) {
21     this(field, true);
22   }
23
24   /** Construct for the named field, lowercasing query values.*/
25   protected RawFieldQueryFilter(String JavaDoc field, float boost) {
26     this(field, true, boost);
27   }
28
29   /** Construct for the named field, potentially lowercasing query values.*/
30   protected RawFieldQueryFilter(String JavaDoc field, boolean lowerCase) {
31     this(field, lowerCase, 0.0f);
32   }
33
34   /** Construct for the named field, potentially lowercasing query values.*/
35   protected RawFieldQueryFilter(String JavaDoc field, boolean lowerCase, float boost) {
36     this.field = field;
37     this.lowerCase = lowerCase;
38     this.boost = boost;
39   }
40
41   public BooleanQuery filter(Query input, BooleanQuery output)
42     throws QueryException {
43     
44     // examine each clause in the Nutch query
45
Clause[] clauses = input.getClauses();
46     for (int i = 0; i < clauses.length; i++) {
47       Clause c = clauses[i];
48
49       // skip non-matching clauses
50
if (!c.getField().equals(field))
51         continue;
52
53       // get the field value from the clause
54
// raw fields are guaranteed to be Terms, not Phrases
55
String JavaDoc value = c.getTerm().toString();
56       if (lowerCase)
57         value = value.toLowerCase();
58
59       // add a Lucene TermQuery for this clause
60
TermQuery clause = new TermQuery(new Term(field, value));
61       // set boost
62
clause.setBoost(boost);
63       // add it as specified in query
64
output.add(clause, c.isRequired(), c.isProhibited());
65     }
66     
67     // return the modified Lucene query
68
return output;
69   }
70 }
71
Popular Tags