1 2 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 18 public abstract class FieldQueryFilter implements QueryFilter { 19 private String field; 20 private float boost = 1.0f; 21 22 23 protected FieldQueryFilter(String field) { 24 this(field, 1.0f); 25 } 26 27 28 protected FieldQueryFilter(String 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 Clause[] clauses = input.getClauses(); 38 for (int i = 0; i < clauses.length; i++) { 39 Clause c = clauses[i]; 40 41 if (!c.getField().equals(field)) 43 continue; 44 45 if (c.isPhrase()) { 47 String [] 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 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 luceneClause.setBoost(boost); 72 output.add(luceneClause, c.isRequired(), c.isProhibited()); 74 } 75 76 return output; 78 } 79 } 80 | Popular Tags |