1 16 17 package org.apache.cocoon.bean.query; 18 19 import java.io.IOException ; 20 import java.io.Serializable ; 21 import java.io.StringReader ; 22 import java.util.Vector ; 23 import org.apache.cocoon.components.search.LuceneXMLIndexer; 24 import org.apache.lucene.analysis.Analyzer; 25 import org.apache.lucene.analysis.Token; 26 import org.apache.lucene.analysis.TokenStream; 27 import org.apache.lucene.index.Term; 28 import org.apache.lucene.search.BooleanQuery; 29 import org.apache.lucene.search.FuzzyQuery; 30 import org.apache.lucene.search.PhraseQuery; 31 import org.apache.lucene.search.Query; 32 import org.apache.lucene.search.TermQuery; 33 34 35 44 public class SimpleLuceneCriterionBean implements SimpleLuceneCriterion, Cloneable , Serializable { 45 46 49 protected Long id; 50 51 54 protected String field; 55 56 59 protected String match; 60 61 64 protected String term; 65 66 69 public SimpleLuceneCriterionBean() { 70 } 71 72 73 80 public SimpleLuceneCriterionBean(String field, String match, String term) { 81 this.field = field; 82 this.match = match; 83 this.term = term; 84 } 85 86 public Object clone() throws CloneNotSupportedException { 87 SimpleLuceneCriterionBean criterion = (SimpleLuceneCriterionBean)super.clone(); 88 return criterion; 89 } 90 91 99 public Query getQuery (Analyzer analyzer) { 100 String f = this.field; 101 Query query = null; 102 if (ANY_FIELD.equals(this.field)) f = LuceneXMLIndexer.BODY_FIELD; 103 TokenStream tokens = analyzer.tokenStream(f, new StringReader (this.term)); 105 Vector words = new Vector (); 106 Token token; 107 while (true) { 108 try { 109 token = tokens.next(); 110 } catch (IOException e) { 111 token = null; 112 } 113 if (token == null) break; 114 words.addElement(token.termText ()); 115 } 116 try { 117 tokens.close(); 118 } catch (IOException e) {} 120 122 if (ANY_MATCH.equals(this.match)) { 123 if (words.size() > 1) { 124 query = new BooleanQuery(); 125 for (int i = 0; i < words.size(); i++) { 126 ((BooleanQuery)query).add(new TermQuery(new Term(f, (String )words.elementAt(i))), false, false); 127 } 128 } else if (words.size() == 1) { 129 query = new TermQuery(new Term(f, (String )words.elementAt(0))); 130 } 131 } 132 133 if (ALL_MATCH.equals(this.match)) { 134 if (words.size() > 1) { 135 query = new BooleanQuery(); 136 for (int i = 0; i < words.size(); i++) { 137 ((BooleanQuery)query).add(new TermQuery(new Term (f, (String )words.elementAt(i))), true, false); 138 } 139 } else if (words.size() == 1) { 140 query = new TermQuery(new Term(f, (String )words.elementAt(0))); 141 } 142 } 143 144 if (NOT_MATCH.equals(this.match)) { 145 if (words.size() > 1) { 146 query = new BooleanQuery(); 147 for (int i = 0; i < words.size(); i++) { 148 ((BooleanQuery)query).add(new TermQuery(new Term(f, (String )words.elementAt(i))), true, true); 149 } 150 } else if (words.size() == 1) { 151 query = new TermQuery(new Term(f, (String )words.elementAt(0))); 152 } 153 } 154 155 if (LIKE_MATCH.equals(this.match)) { 156 if (words.size() > 1) { 157 query = new BooleanQuery(); 158 for (int i = 0; i < words.size(); i++) { 159 ((BooleanQuery)query).add(new FuzzyQuery(new Term(f, (String )words.elementAt(i))), false, false); 160 } 161 } else if (words.size() == 1) { 162 query = new FuzzyQuery(new Term(f, (String )words.elementAt(0))); 163 } 164 } 165 166 if (PHRASE_MATCH.equals (this.match)) { 167 if (words.size() > 1) { 168 query = new PhraseQuery(); 169 ((PhraseQuery)query).setSlop(0); 170 for (int i = 0; i < words.size(); i++) { 171 ((PhraseQuery)query).add(new Term(f, (String )words.elementAt(i))); 172 } 173 } else if (words.size() == 1) { 174 query = new TermQuery(new Term(f, (String )words.elementAt(0))); 175 } 176 } 177 return query; 178 } 179 180 183 public boolean isProhibited () { 184 if (NOT_MATCH.equals(this.match)) return true; 185 return false; 186 } 187 188 189 191 196 public Long getId() { 197 return this.id; 198 } 199 200 205 public void setId(Long id) { 206 this.id = id; 207 } 208 209 214 public String getField() { 215 return this.field; 216 } 217 218 224 public void setField(String field) { 225 this.field = field; 226 } 227 228 233 public String getMatch() { 234 return this.match; 235 } 236 237 243 public void setMatch(String match) { 244 this.match = match; 245 } 246 247 252 public String getTerm() { 253 return this.term; 254 } 255 256 262 public void setTerm(String term) { 263 this.term = term; 264 } 265 266 } 267 | Popular Tags |