1 package org.apache.lucene.search; 2 3 18 19 import java.io.IOException ; 20 21 import java.util.HashSet ; 22 import java.util.Iterator ; 23 import java.util.Set ; 24 25 import org.apache.lucene.index.IndexReader; 26 27 46 public abstract class Query implements java.io.Serializable , Cloneable { 47 private float boost = 1.0f; 49 53 public void setBoost(float b) { boost = b; } 54 55 59 public float getBoost() { return boost; } 60 61 74 public abstract String toString(String field); 75 76 77 public String toString() { 78 return toString(""); 79 } 80 81 85 protected Weight createWeight(Searcher searcher) throws IOException { 86 throw new UnsupportedOperationException (); 87 } 88 89 90 public Weight weight(Searcher searcher) 91 throws IOException { 92 Query query = searcher.rewrite(this); 93 Weight weight = query.createWeight(searcher); 94 float sum = weight.sumOfSquaredWeights(); 95 float norm = getSimilarity(searcher).queryNorm(sum); 96 weight.normalize(norm); 97 return weight; 98 } 99 100 104 public Query rewrite(IndexReader reader) throws IOException { 105 return this; 106 } 107 108 119 public Query combine(Query[] queries) { 120 HashSet uniques = new HashSet (); 121 for (int i = 0; i < queries.length; i++) { 122 Query query = queries[i]; 123 BooleanClause[] clauses = null; 124 boolean splittable = (query instanceof BooleanQuery); 126 if(splittable){ 127 BooleanQuery bq = (BooleanQuery) query; 128 splittable = bq.isCoordDisabled(); 129 clauses = bq.getClauses(); 130 for (int j = 0; splittable && j < clauses.length; j++) { 131 splittable = (clauses[j].getOccur() == BooleanClause.Occur.SHOULD); 132 } 133 } 134 if(splittable){ 135 for (int j = 0; j < clauses.length; j++) { 136 uniques.add(clauses[j].getQuery()); 137 } 138 } else { 139 uniques.add(query); 140 } 141 } 142 if(uniques.size() == 1){ 144 return (Query)uniques.iterator().next(); 145 } 146 Iterator it = uniques.iterator(); 147 BooleanQuery result = new BooleanQuery(true); 148 while (it.hasNext()) 149 result.add((Query) it.next(), BooleanClause.Occur.SHOULD); 150 return result; 151 } 152 153 159 public void extractTerms(Set terms) { 160 throw new UnsupportedOperationException (); 162 } 163 164 165 170 public static Query mergeBooleanQueries(Query[] queries) { 171 HashSet allClauses = new HashSet (); 172 for (int i = 0; i < queries.length; i++) { 173 BooleanClause[] clauses = ((BooleanQuery)queries[i]).getClauses(); 174 for (int j = 0; j < clauses.length; j++) { 175 allClauses.add(clauses[j]); 176 } 177 } 178 179 boolean coordDisabled = 180 queries.length==0? false : ((BooleanQuery)queries[0]).isCoordDisabled(); 181 BooleanQuery result = new BooleanQuery(coordDisabled); 182 Iterator i = allClauses.iterator(); 183 while (i.hasNext()) { 184 result.add((BooleanClause)i.next()); 185 } 186 return result; 187 } 188 189 193 public Similarity getSimilarity(Searcher searcher) { 194 return searcher.getSimilarity(); 195 } 196 197 198 public Object clone() { 199 try { 200 return (Query)super.clone(); 201 } catch (CloneNotSupportedException e) { 202 throw new RuntimeException ("Clone not supported: " + e.getMessage()); 203 } 204 } 205 } 206 | Popular Tags |