KickJava   Java API By Example, From Geeks To Geeks.

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


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.PhraseQuery;
8 import org.apache.lucene.search.TermQuery;
9 import org.apache.lucene.index.Term;
10
11 import net.nutch.analysis.CommonGrams;
12
13 import net.nutch.searcher.Query.Clause;
14 import net.nutch.searcher.Query.Phrase;
15
16 /** Translate query fields to search the same-named field, as indexed by an
17  * IndexingFilter. Best for tokenized fields. */

18 public abstract class FieldQueryFilter implements QueryFilter {
19   private String JavaDoc field;
20   private float boost = 1.0f;
21
22   /** Construct for the named field.*/
23   protected FieldQueryFilter(String JavaDoc field) {
24     this(field, 1.0f);
25   }
26
27   /** Construct for the named field, boosting as specified.*/
28   protected FieldQueryFilter(String JavaDoc field, float boost) {
29     this.field = field;
30     this.boost = boost;
31   }
32
33   public BooleanQuery filter(Query input, BooleanQuery output)
34     throws QueryException {
35     
36     // examine each clause in the Nutch query
37
Clause[] clauses = input.getClauses();
38     for (int i = 0; i < clauses.length; i++) {
39       Clause c = clauses[i];
40
41       // skip non-matching clauses
42
if (!c.getField().equals(field))
43         continue;
44
45       // optimize phrase clause
46
if (c.isPhrase()) {
47         String JavaDoc[] opt = CommonGrams.optimizePhrase(c.getPhrase(), field);
48         if (opt.length==1) {
49           c = new Clause(new Query.Term(opt[0]),
50                          c.isRequired(), c.isProhibited());
51         } else {
52           c = new Clause(new Phrase(opt), c.isRequired(), c.isProhibited());
53         }
54       }
55
56       // construct appropriate Lucene clause
57
org.apache.lucene.search.Query luceneClause;
58       if (c.isPhrase()) {
59         Phrase nutchPhrase = c.getPhrase();
60         Query.Term[] terms = nutchPhrase.getTerms();
61         PhraseQuery lucenePhrase = new PhraseQuery();
62         for (int j = 0; j < terms.length; j++) {
63           lucenePhrase.add(new Term(field, terms[j].toString()));
64         }
65         luceneClause = lucenePhrase;
66       } else {
67         luceneClause = new TermQuery(new Term(field, c.getTerm().toString()));
68       }
69
70       // set boost
71
luceneClause.setBoost(boost);
72       // add it as specified in query
73
output.add(luceneClause, c.isRequired(), c.isProhibited());
74     }
75     
76     // return the modified Lucene query
77
return output;
78   }
79 }
80
Popular Tags