1 17 package org.alfresco.repo.search.impl.lucene.query; 18 19 import java.io.IOException ; 20 import java.util.ArrayList ; 21 import java.util.List ; 22 23 import org.alfresco.service.cmr.dictionary.DictionaryService; 24 import org.apache.lucene.index.IndexReader; 25 import org.apache.lucene.index.Term; 26 import org.apache.lucene.search.Explanation; 27 import org.apache.lucene.search.Query; 28 import org.apache.lucene.search.Scorer; 29 import org.apache.lucene.search.Searcher; 30 import org.apache.lucene.search.Weight; 31 32 44 public class PathQuery extends Query 45 { 46 49 private static final long serialVersionUID = 3832904355660707892L; 50 51 private String pathField = "PATH"; 52 53 private String qNameField = "QNAME"; 54 55 private int unitSize = 2; 56 57 private List <StructuredFieldPosition> pathStructuredFieldPositions = new ArrayList <StructuredFieldPosition>(); 58 59 private List <StructuredFieldPosition> qNameStructuredFieldPositions = new ArrayList <StructuredFieldPosition>(); 60 61 private DictionaryService dictionarySertvice; 62 63 private boolean repeats = false; 64 65 70 71 public PathQuery(DictionaryService dictionarySertvice) 72 { 73 super(); 74 this.dictionarySertvice = dictionarySertvice; 75 } 76 77 public void setQuery(List <StructuredFieldPosition> path, List <StructuredFieldPosition> qname) 78 { 79 qNameStructuredFieldPositions.clear(); 80 pathStructuredFieldPositions.clear(); 81 if (qname.size() != unitSize) 82 { 83 throw new UnsupportedOperationException (); 84 } 85 if (path.size() % unitSize != 0) 86 { 87 throw new UnsupportedOperationException (); 88 } 89 qNameStructuredFieldPositions.addAll(qname); 90 pathStructuredFieldPositions.addAll(path); 91 } 92 93 public void appendQuery(List <StructuredFieldPosition> sfps) 94 { 95 if (sfps.size() != unitSize) 96 { 97 throw new UnsupportedOperationException (); 98 } 99 100 StructuredFieldPosition last = null; 101 StructuredFieldPosition next = sfps.get(0); 102 103 if (qNameStructuredFieldPositions.size() > 0) 104 { 105 last = qNameStructuredFieldPositions.get(qNameStructuredFieldPositions.size() - 1); 106 } 107 108 if ((last != null) && next.linkParent() && !last.allowslinkingByParent()) 109 { 110 return; 111 } 112 113 if ((last != null) && next.linkSelf() && !last.allowsLinkingBySelf()) 114 { 115 return; 116 } 117 118 if (qNameStructuredFieldPositions.size() == unitSize) 119 { 120 pathStructuredFieldPositions.addAll(qNameStructuredFieldPositions); 121 } 122 qNameStructuredFieldPositions.clear(); 123 qNameStructuredFieldPositions.addAll(sfps); 124 } 125 126 public String getPathField() 127 { 128 return pathField; 129 } 130 131 public void setPathField(String pathField) 132 { 133 this.pathField = pathField; 134 } 135 136 public String getQnameField() 137 { 138 return qNameField; 139 } 140 141 public void setQnameField(String qnameField) 142 { 143 this.qNameField = qnameField; 144 } 145 146 public Term getPathRootTerm() 147 { 148 return new Term(getPathField(), ";"); 149 } 150 151 public Term getQNameRootTerm() 152 { 153 return new Term(getQnameField(), ";"); 154 } 155 156 159 protected Weight createWeight(Searcher searcher) 160 { 161 return new StructuredFieldWeight(searcher); 162 } 163 164 167 public String toString() 168 { 169 return ""; 170 } 171 172 175 public String toString(String field) 176 { 177 return ""; 178 } 179 180 private class StructuredFieldWeight implements Weight 181 { 182 183 186 private static final long serialVersionUID = 3257854259645985328L; 187 188 private Searcher searcher; 189 190 private float value; 191 192 private float idf; 193 194 private float queryNorm; 195 196 private float queryWeight; 197 198 public StructuredFieldWeight(Searcher searcher) 199 { 200 this.searcher = searcher; 201 202 } 203 204 208 public Explanation explain(IndexReader reader, int doc) throws IOException 209 { 210 throw new UnsupportedOperationException (); 211 } 212 213 216 public Query getQuery() 217 { 218 return PathQuery.this; 219 } 220 221 226 public float getValue() 227 { 228 return value; 229 } 230 231 236 public void normalize(float queryNorm) 237 { 238 this.queryNorm = queryNorm; 239 queryWeight *= queryNorm; value = queryWeight * idf; } 242 243 248 public Scorer scorer(IndexReader reader) throws IOException 249 { 250 return PathScorer.createPathScorer(getSimilarity(searcher), PathQuery.this, reader, this, dictionarySertvice, repeats); 251 252 } 253 254 259 public float sumOfSquaredWeights() throws IOException 260 { 261 idf = getSimilarity(searcher).idf(getTerms(), searcher); queryWeight = idf * getBoost(); return queryWeight * queryWeight; } 266 267 private ArrayList <Term> getTerms() 268 { 269 ArrayList <Term> answer = new ArrayList <Term>(pathStructuredFieldPositions.size()); 270 for (StructuredFieldPosition sfp : pathStructuredFieldPositions) 271 { 272 if (sfp.getTermText() != null) 273 { 274 Term term = new Term(pathField, sfp.getTermText()); 275 answer.add(term); 276 } 277 } 278 return answer; 279 } 280 } 281 282 public void removeDescendantAndSelf() 283 { 284 while ((getLast() != null) && getLast().linkSelf()) 285 { 286 removeLast(); 287 removeLast(); 288 } 289 } 290 291 private StructuredFieldPosition getLast() 292 293 { 294 if (qNameStructuredFieldPositions.size() > 0) 295 { 296 return qNameStructuredFieldPositions.get(qNameStructuredFieldPositions.size() - 1); 297 } 298 else 299 { 300 return null; 301 } 302 } 303 304 private void removeLast() 305 { 306 qNameStructuredFieldPositions.clear(); 307 for (int i = 0; i < unitSize; i++) 308 { 309 if (pathStructuredFieldPositions.size() > 0) 310 { 311 qNameStructuredFieldPositions.add(0, pathStructuredFieldPositions.remove(pathStructuredFieldPositions.size() - 1)); 312 } 313 } 314 } 315 316 public boolean isEmpty() 317 { 318 return qNameStructuredFieldPositions.size() == 0; 319 } 320 321 public List <StructuredFieldPosition> getPathStructuredFieldPositions() 322 { 323 return pathStructuredFieldPositions; 324 } 325 326 327 public List <StructuredFieldPosition> getQNameStructuredFieldPositions() 328 { 329 return qNameStructuredFieldPositions; 330 } 331 332 public void setRepeats(boolean repeats) 333 { 334 this.repeats = repeats; 335 } 336 337 338 339 340 341 } | Popular Tags |