1 4 package com.openedit.modules.search; 5 6 import java.util.Date ; 7 import java.util.HashMap ; 8 import java.util.Map ; 9 10 import org.apache.lucene.document.DateTools; 11 import org.apache.lucene.document.DateTools.Resolution; 12 13 import com.openedit.hittracker.SearchQuery; 14 import com.openedit.hittracker.Term; 15 16 public class LuceneSearchQuery extends SearchQuery 17 { 18 public void addAfter(String inFieldId, Date inDate, String inFriendlyFieldName) 19 { 20 String date = DateTools.dateToString(inDate, Resolution.DAY); 21 Term term = new Term() 23 { 24 public String toQuery() 25 { 26 String fin = getId() + ":[" + getValue() + " TO " + null + "]"; 27 return fin; 28 } 29 }; 30 term.setId(inFieldId); 31 term.setValue(date); 32 term.setFriendly(inFriendlyFieldName); 33 getTerms().add( term); 34 } 35 public void addBefore(String inFieldId, Date inDate, String inFriendlyFieldName) 36 { 37 String date = DateTools.dateToString(inDate, Resolution.DAY); 38 String fin = inFieldId + ":[ null TO " + date + "]"; 39 fieldTerms.add(fin); 40 41 Term term = new Term() 42 { 43 public String toQuery() 44 { 45 String fin = getId() + ":[ null TO " + getValue() + "]"; 46 return fin; 47 } 48 }; 49 term.setId(inFieldId); 50 term.setValue(date); 51 term.setFriendly(inFriendlyFieldName); 52 getTerms().add( term); 53 } 54 55 65 public void addOrsGroup(String inField, String inValue, String inFriendlyFieldName) 66 { 67 Term term = new Term() 68 { 69 public String toQuery() 70 { 71 StringBuffer orString = new StringBuffer (); 72 String [] orwords = getValue().split("\\s"); 73 if (orwords.length > 0) 74 { 75 orString.append("("); 76 for (int i = 0; i < orwords.length - 1; i++) 77 { 78 orString.append(orwords[i]); 79 orString.append(" OR "); 80 } 81 orString.append(orwords[orwords.length - 1]); 82 orString.append(")"); 83 } 84 return orString.toString(); 85 } 86 }; 87 term.setId(inField); 88 term.setValue(inValue); 89 term.setFriendly(inFriendlyFieldName); 90 getTerms().add( term); 91 } 92 93 public void addNots(String inId, String inNots, String inFriendlyFieldName) 94 { 95 Term term = new Term() 96 { 97 public String toQuery() 98 { 99 StringBuffer orString = new StringBuffer (); 100 101 String [] notwords = getValue().split("\\s"); 102 if (notwords.length > 0) 103 { 104 for (int i = 0; i < notwords.length; i++) 105 { 106 orString.append(" NOT " + notwords[i] ); 107 } 108 } 109 return orString.toString(); 110 } 111 }; 112 term.setId(inId); 113 term.setValue(inNots); 114 term.setFriendly(inFriendlyFieldName); 115 getTerms().add( term); 116 } 117 118 public void addNot(String inNot) 119 { 120 121 addNots(null, inNot, "not " + inNot); 122 } 124 125 159 160 public void addFilter(String inFilter) 161 { 162 163 if( inFilter != null && inFilter.length()> 1) 164 { 165 Term term = new Term(); 166 term.setValue(inFilter); 167 fieldTerms.add(term); 168 } 169 } 170 171 172 public String toQuery() 173 { 174 String op = " OR "; 175 if( isAndTogether()) 176 { 177 op = "+"; 178 } 179 StringBuffer done = new StringBuffer (); 180 for (int i = 0; i < fieldTerms.size(); i++) 181 { 182 Term field= (Term)fieldTerms.get(i); 183 String q = field.toQuery(); 184 if( i > 0 && !q.startsWith("+") && !q.startsWith("-")) 185 { 186 done.append(op); 187 } 188 done.append(q); 189 if( i+1 < fieldTerms.size() ) 190 { 191 done.append(" "); 192 } 193 } 194 return done.toString(); 195 } 196 197 public String toFriendly() 198 { 199 String op = " or "; 200 if( isAndTogether()) 201 { 202 op = " and "; 203 } 204 StringBuffer done = new StringBuffer (); 205 for (int i = 0; i < fieldTerms.size(); i++) 206 { 207 Term field= (Term)fieldTerms.get(i); 208 String q = field.getFriendly(); 209 if( q != null) 210 { 211 if( i > 0 && !q.startsWith("+") && !q.startsWith("-")) 212 { 213 done.append(op); 214 } 215 done.append(q); 216 if( i+1 < fieldTerms.size() ) 217 { 218 done.append(" "); 219 } 220 221 } 222 } 223 return done.toString(); 224 225 } 226 227 public void addExact(String inId, String inValue, String inFriendlyFieldName) 228 { 229 Term term = new Term() 230 { 231 public String toQuery() 232 { 233 return getId() + ":\"" + getValue() + "\""; 234 } 235 }; 236 term.setId(inId); 237 term.setValue(inValue); 238 term.setFriendly(inFriendlyFieldName); 239 getTerms().add( term); 240 241 242 243 } 244 245 public void addExact(String inValue, String inFriendlyFieldName) 246 { 247 Term term = new Term() 248 { 249 public String toQuery() 250 { 251 return "\"" + getValue() + "\""; 252 } 253 }; 254 255 term.setValue(inValue); 256 term.setFriendly(inFriendlyFieldName); 257 getTerms().add( term); 258 259 260 261 } 262 263 public void addNot(String inField, String inVal) 264 { 265 Term term = new Term() 266 { 267 public String toQuery() 268 { 269 return "-" + getId()+ ":" + getValue(); 270 } 271 }; 272 term.setId(inField); 273 term.setValue(inVal); 274 275 276 fieldTerms.add(term); 277 } 278 } 279 | Popular Tags |