KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > openedit > modules > search > LuceneSearchQuery


1 /*
2  * Created on Jul 19, 2006
3  */

4 package com.openedit.modules.search;
5
6 import java.util.Date JavaDoc;
7 import java.util.HashMap JavaDoc;
8 import java.util.Map JavaDoc;
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 JavaDoc inFieldId, Date JavaDoc inDate, String JavaDoc inFriendlyFieldName)
19     {
20         String JavaDoc date = DateTools.dateToString(inDate, Resolution.DAY);
21         //fieldDateFilter = RangeFilter.More(inFieldId, date); //more than or equal
22
Term term = new Term()
23         {
24             public String JavaDoc toQuery()
25             {
26                 String JavaDoc 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 JavaDoc inFieldId, Date JavaDoc inDate, String JavaDoc inFriendlyFieldName)
36     {
37         String JavaDoc date = DateTools.dateToString(inDate, Resolution.DAY);
38         String JavaDoc fin = inFieldId + ":[ null TO " + date + "]";
39         fieldTerms.add(fin);
40         
41         Term term = new Term()
42         {
43             public String JavaDoc toQuery()
44             {
45                 String JavaDoc 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 // public void addAnd(String inString)
56
// {
57
// if (out.length() > 0)
58
// {
59
// out.append("AND ");
60
// }
61
// out.append(inString);
62
// out.append(" ");
63
// }
64

65     public void addOrsGroup(String JavaDoc inField, String JavaDoc inValue, String JavaDoc inFriendlyFieldName)
66     {
67         Term term = new Term()
68         {
69             public String JavaDoc toQuery()
70             {
71                 StringBuffer JavaDoc orString = new StringBuffer JavaDoc();
72                 String JavaDoc[] 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 JavaDoc inId, String JavaDoc inNots, String JavaDoc inFriendlyFieldName)
94     {
95         Term term = new Term()
96         {
97             public String JavaDoc toQuery()
98             {
99                 StringBuffer JavaDoc orString = new StringBuffer JavaDoc();
100
101                 String JavaDoc[] 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 JavaDoc inNot)
119     {
120         
121         addNots(null, inNot, "not " + inNot);
122         //fieldTerms.add("NOT " + inNot);
123
}
124
125 // public void addDayRangeBeforeToday(String inDaterange)
126
// {
127
// GregorianCalendar d = new GregorianCalendar();
128
// SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
129
// String end = format.format(d.getTime());
130
// int less = Integer.parseInt(inDaterange);
131
// int m = 0 - less ;
132
// d.add(GregorianCalendar.DAY_OF_YEAR,m);
133
// //var start = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear();
134
// String start = format.format(d.getTime());
135
//
136
// String fin = "date:[" + start + " TO " + end + "]";
137
// //alert( );
138
// //query.value = query.value + ' AND ' + fin;
139
//
140
// fieldTerms.add(fin);
141
// }
142
//
143
// public void addMonthRangeBeforeToday(String inMonths)
144
// {
145
// int less = Integer.parseInt(inMonths);
146
// int m = 0 - less ;
147
// GregorianCalendar d = new GregorianCalendar();
148
// //var start = d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear();
149
// SimpleDateFormat format = new SimpleDateFormat("MM/dd/yyyy");
150
// String end = format.format(d.getTime());
151
// d.add(GregorianCalendar.MONTH,m);
152
// String start = format.format(d.getTime());
153
//
154
// //Is this right? Looks like wrong format
155
// String fin = "date:[" + start + " TO " + end + "]";
156
// fieldTerms.add(fin);
157
// }
158

159     
160     public void addFilter(String JavaDoc 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 JavaDoc toQuery()
173     {
174         String JavaDoc op = " OR ";
175         if( isAndTogether())
176         {
177             op = "+";
178         }
179         StringBuffer JavaDoc done = new StringBuffer JavaDoc();
180         for (int i = 0; i < fieldTerms.size(); i++)
181         {
182             Term field= (Term)fieldTerms.get(i);
183             String JavaDoc 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 JavaDoc toFriendly()
198     {
199         String JavaDoc op = " or ";
200         if( isAndTogether())
201         {
202             op = " and ";
203         }
204         StringBuffer JavaDoc done = new StringBuffer JavaDoc();
205         for (int i = 0; i < fieldTerms.size(); i++)
206         {
207             Term field= (Term)fieldTerms.get(i);
208             String JavaDoc 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 JavaDoc inId, String JavaDoc inValue, String JavaDoc inFriendlyFieldName)
228     {
229         Term term = new Term()
230         {
231             public String JavaDoc 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 JavaDoc inValue, String JavaDoc inFriendlyFieldName)
246     {
247         Term term = new Term()
248         {
249             public String JavaDoc 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 JavaDoc inField, String JavaDoc inVal)
264     {
265         Term term = new Term()
266         {
267             public String JavaDoc 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