1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import org.apache.lucene.index.IndexReader; 22 import org.apache.lucene.index.Term; 23 import org.apache.lucene.util.ToStringUtils; 24 25 38 public abstract class MultiTermQuery extends Query { 39 private Term term; 40 41 42 public MultiTermQuery(Term term) { 43 this.term = term; 44 } 45 46 47 public Term getTerm() { return term; } 48 49 50 protected abstract FilteredTermEnum getEnum(IndexReader reader) 51 throws IOException ; 52 53 public Query rewrite(IndexReader reader) throws IOException { 54 FilteredTermEnum enumerator = getEnum(reader); 55 BooleanQuery query = new BooleanQuery(true); 56 try { 57 do { 58 Term t = enumerator.term(); 59 if (t != null) { 60 TermQuery tq = new TermQuery(t); tq.setBoost(getBoost() * enumerator.difference()); query.add(tq, BooleanClause.Occur.SHOULD); } 64 } while (enumerator.next()); 65 } finally { 66 enumerator.close(); 67 } 68 return query; 69 } 70 71 72 public String toString(String field) { 73 StringBuffer buffer = new StringBuffer (); 74 if (!term.field().equals(field)) { 75 buffer.append(term.field()); 76 buffer.append(":"); 77 } 78 buffer.append(term.text()); 79 buffer.append(ToStringUtils.boost(getBoost())); 80 return buffer.toString(); 81 } 82 83 public boolean equals(Object o) { 84 if (this == o) return true; 85 if (!(o instanceof MultiTermQuery)) return false; 86 87 final MultiTermQuery multiTermQuery = (MultiTermQuery) o; 88 89 if (!term.equals(multiTermQuery.term)) return false; 90 91 return getBoost() == multiTermQuery.getBoost(); 92 } 93 94 public int hashCode() { 95 return term.hashCode() + Float.floatToRawIntBits(getBoost()); 96 } 97 } 98 | Popular Tags |