1 16 17 package org.apache.cocoon.bean.query; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 import java.util.Date ; 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Enumeration ; 27 28 import org.apache.lucene.document.Document; 29 import org.apache.lucene.document.Field; 30 import org.apache.lucene.search.Hits; 31 import org.apache.lucene.search.Query; 32 import org.apache.lucene.search.BooleanQuery; 33 import org.apache.cocoon.components.search.LuceneCocoonSearcher; 34 import org.apache.cocoon.ProcessingException; 35 36 37 47 public class SimpleLuceneQueryBean implements SimpleLuceneQuery, Cloneable , Serializable { 48 49 53 public static Long DEFAULT_PAGE_SIZE = new Long (20); 54 55 59 public static Long DEFAULT_PAGE = new Long (0); 60 61 66 public static String SCORE_FIELD = "_lucene-score_"; 67 68 73 public static String INDEX_FIELD = "_lucene-index_"; 74 75 78 private Date date; 79 80 83 private List criteria; 84 85 88 private Long id; 89 90 93 private Long page; 94 95 98 private Long size; 99 100 103 private Long total; 104 105 108 private String bool; 109 110 113 private String name; 114 115 118 private String type; 119 120 123 private String user; 124 125 128 public SimpleLuceneQueryBean() { 129 } 130 131 140 public SimpleLuceneQueryBean(String type, String bool, String match, String field, String query) { 141 this.name = "My Query"; 142 this.type = type; 143 this.bool = bool; 144 this.size = DEFAULT_PAGE_SIZE; 145 this.page = DEFAULT_PAGE; 146 this.total = null; 147 this.user = null; 148 this.id = null; 149 this.addCriterion(new SimpleLuceneCriterionBean(field, match, query)); 150 } 151 152 public Object clone() throws CloneNotSupportedException { 153 SimpleLuceneQueryBean query = (SimpleLuceneQueryBean)super.clone(); 154 query.setCriteria(new ArrayList (this.criteria.size())); 155 Iterator it = this.getCriteria().iterator(); 156 while (it.hasNext()) query.addCriterion((SimpleLuceneCriterionBean)((SimpleLuceneCriterionBean)it.next()).clone()); 157 return query; 158 } 159 160 178 public List search (LuceneCocoonSearcher searcher) throws IOException , ProcessingException { 179 BooleanQuery query = new BooleanQuery(); 180 Iterator it = criteria.iterator(); 181 boolean required = false; 182 if (AND_BOOL.equals(this.bool)) required = true; 183 while (it.hasNext()) { 184 SimpleLuceneCriterion criterion = (SimpleLuceneCriterion)it.next(); 185 Query subquery = criterion.getQuery (searcher.getAnalyzer()); 186 query.add(subquery, required, criterion.isProhibited()); 187 } 188 Hits hits = searcher.search(query); 189 this.total = new Long (hits.length()); 190 this.date = new Date (); 191 return page(hits); 192 } 193 194 201 private List page (Hits hits) throws java.io.IOException { 202 ArrayList results = new ArrayList (); 203 int start = getPage().intValue() * getSize().intValue(); 204 if (start > this.total.intValue()) start = this.total.intValue(); 205 int end = start + getSize().intValue(); 206 if (end > this.total.intValue()) end = this.total.intValue(); 207 for (int i = start; i < end; i++) { 208 HashMap hit = new HashMap (); 209 hit.put(SCORE_FIELD, new Float (hits.score (i))); 210 hit.put(INDEX_FIELD, new Long (i)); 211 Document doc = hits.doc(i); 212 for (Enumeration e = doc.fields(); e.hasMoreElements(); ) { 213 Field field = (Field)e.nextElement(); 214 if (field.name().equals(SCORE_FIELD)) continue; 215 if (field.name().equals(INDEX_FIELD)) continue; 216 hit.put(field.name(), field.stringValue()); 217 } 218 results.add(hit); 219 } 220 return (results); 221 } 222 223 228 public Long getId() { 229 return this.id; 230 } 231 232 237 public void setId(Long id) { 238 this.id = id; 239 } 240 241 246 public String getName() { 247 return this.name; 248 } 249 250 255 public void setName(String name) { 256 this.name = name; 257 } 258 259 264 public String getType() { 265 return this.type; 266 } 267 268 273 public void setType(String type) { 274 this.type = type; 275 } 276 277 282 public String getBool() { 283 return this.bool; 284 } 285 286 292 public void setBool(String bool) { 293 this.bool = bool; 294 } 295 296 301 public String getUser() { 302 return this.user; 303 } 304 305 310 public void setUser(String user) { 311 this.user = user; 312 } 313 314 319 public Long getSize() { 320 if (this.size == null) { 321 return DEFAULT_PAGE_SIZE; 322 } else { 323 return this.size; 324 } 325 } 326 327 333 public void setSize(Long size) { 334 this.size = size; 335 } 336 337 342 public Long getPage() { 343 if (this.page == null) { 344 return DEFAULT_PAGE; 345 } else { 346 return this.page; 347 } 348 } 349 350 356 public void setPage(Long page) { 357 this.page = page; 358 } 359 360 365 public Long getTotal() { 366 return this.total; 367 } 368 369 374 public void setTotal(Long total) { 375 this.total = total; 376 } 377 378 383 public Date getDate() { 384 return this.date; 385 } 386 387 392 public void setDate(Date date) { 393 this.date = date; 394 } 395 396 401 public List getCriteria() { 402 return this.criteria; 403 } 404 405 410 public void setCriteria(List criteria) { 411 this.criteria = criteria; 412 } 413 414 419 public void addCriterion(SimpleLuceneCriterionBean criterion) { 420 if (this.criteria == null) this.criteria = new ArrayList (); 421 this.criteria.add(criterion); 422 } 423 424 429 public void removeCriterion(SimpleLuceneCriterionBean criterion) { 430 if (this.criteria != null) this.criteria.remove(criterion); 431 } 432 433 } 434 | Popular Tags |