KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > lucene > BugSearcher


1 /*
2   Copyright (C) 2003-2006 Know Gate S.L. All rights reserved.
3                            C/Oņa, 107 1ē2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.lucene;
34
35 import java.util.Arrays JavaDoc;
36 import java.util.Date JavaDoc;
37 import java.util.Comparator JavaDoc;
38 import java.io.IOException JavaDoc;
39
40 import org.apache.lucene.document.*;
41 import org.apache.lucene.search.*;
42 import org.apache.lucene.queryParser.*;
43 import org.apache.lucene.analysis.SimpleAnalyzer;
44
45 import com.knowgate.debug.DebugFile;
46 import com.knowgate.misc.Gadgets;
47
48 /**
49  * Search into a Lucene full text index for bugs
50  * @author Sergio Montoro Ten
51  * @version 3.0
52  */

53 public class BugSearcher {
54
55   public BugSearcher() {
56   }
57
58   /**
59    * Compose a Lucene query based on given parameters
60    * @param sLuceneIndexPath String Base path for Lucene indexes excluding WorkArea and table name
61    * @param sWorkArea String GUID of WorkArea to be searched, cannot be null
62    * @param sProject String GUID f project to which bug belongs
63    * @param sReportedBy String
64    * @param sWrittenBy String
65    * @param sTitle String
66    * @param sFromDate String
67    * @param sToDate String
68    * @param sType String
69    * @param sPriority String
70    * @param sSeverity String
71    * @param sStatus String
72    * @param sText String
73    * @param sComments String
74    * @param iLimit int
75    * @param oSortBy Comparator
76    * @return BugRecord[]
77    * @throws ParseException
78    * @throws IOException
79    * @throws NullPointerException
80    */

81   public static BugRecord[] search (String JavaDoc sLuceneIndexPath,
82                                      String JavaDoc sWorkArea, String JavaDoc sProjectGUID,
83                                      String JavaDoc sReportedBy, String JavaDoc sWrittenBy,
84                                      String JavaDoc sTitle,
85                                      String JavaDoc sFromDate, String JavaDoc sToDate,
86                                      String JavaDoc sType, String JavaDoc sPriority, String JavaDoc sSeverity,
87                                      String JavaDoc sStatus, String JavaDoc sText,
88                                      String JavaDoc sComments,
89                                      int iLimit, Comparator JavaDoc oSortBy)
90     throws ParseException, IOException JavaDoc, NullPointerException JavaDoc {
91
92   if (null==sLuceneIndexPath)
93     throw new NullPointerException JavaDoc("BugSearcher.search() luceindex parameter cannot be null");
94
95     if (null==sWorkArea)
96       throw new NullPointerException JavaDoc("BugSearcher.search() workarea parameter cannot be null");
97
98     if (DebugFile.trace) {
99       DebugFile.writeln("Begin BugSearcher.search("+sLuceneIndexPath+","+
100                         sWorkArea+","+sProjectGUID+","+sReportedBy+","+sWrittenBy+","+
101                         sTitle+","+sFromDate+","+sToDate+","+sType+","+sPriority+","+
102                         sSeverity+","+","+sStatus+","+sText+String.valueOf(iLimit)+")");
103       DebugFile.incIdent();
104     }
105
106     BugRecord[] aRetArr;
107
108     String JavaDoc sQry = "workarea:"+sWorkArea;
109
110     if (null!=sProjectGUID)
111       if (sProjectGUID.length()>0) sQry += " AND container:\""+sProjectGUID+"\"";
112
113     if (null!=sWrittenBy)
114       if (sWrittenBy.length()>0)
115         sQry += " AND writer:\""+sWrittenBy+"\"";
116
117     if (null!=sReportedBy)
118       if (sReportedBy.length()>0)
119         sQry += " AND author:\""+Gadgets.ASCIIEncode(sReportedBy)+"\"";
120
121     if (null!=sTitle)
122       if (sTitle.length()>0)
123         sQry += " AND title:\""+Gadgets.ASCIIEncode(sTitle)+"\"";
124
125     if (null!=sType)
126       if (sType.length()>0)
127         sQry += " AND type:\""+sType+"\"";
128
129     if (null!=sStatus)
130       if (sStatus.length()>0)
131         sQry += " AND status:\""+sStatus+"\"";
132
133     if (null!=sPriority)
134       if (sPriority.length()>0)
135         sQry += " AND priority:\""+sPriority+"\"";
136
137     if (null!=sSeverity)
138       if (sSeverity.length()>0)
139         sQry += " AND severity:\""+sSeverity+"\"";
140
141     if (sFromDate!=null && sToDate!=null)
142       sQry += " AND created:["+Gadgets.removeChar(sFromDate,'-')+" TO "+Gadgets.removeChar(sToDate,'-')+"]";
143     else if (sFromDate!=null)
144       sQry += " AND created:["+Gadgets.removeChar(sFromDate,'-')+" TO 21991231]";
145     else if (sToDate!=null)
146       sQry += " AND created:[19790101 TO "+Gadgets.removeChar(sToDate,'-')+"]";
147
148     if (null!=sText)
149       if (sText.length()>0)
150         sQry += " AND text:\""+Gadgets.ASCIIEncode(sText)+"\"";
151
152     if (null!=sComments)
153       if (sComments.length()>0)
154         sQry += " AND comments:\""+Gadgets.ASCIIEncode(sComments)+"\"";
155
156     QueryParser oQpr = new QueryParser("text", new SimpleAnalyzer());
157     if (DebugFile.trace) DebugFile.writeln("QueryParser.parse("+sQry+")");
158     Query oQry = oQpr.parse(sQry);
159
160     if (DebugFile.trace) DebugFile.writeln("new IndexSearcher("+sLuceneIndexPath+")");
161     IndexSearcher oSearch = new IndexSearcher(sLuceneIndexPath);
162     Document oDoc;
163
164     if (iLimit>0) {
165       TopDocs oTopSet = oSearch.search(oQry, null, iLimit);
166       if (oTopSet.scoreDocs!=null) {
167         ScoreDoc[] oTopDoc = oTopSet.scoreDocs;
168         int iDocCount = oTopDoc.length;
169         aRetArr = new BugRecord[iDocCount];
170         for (int d=0; d<iDocCount; d++) {
171           oDoc = oSearch.doc(oTopDoc[d].doc);
172           String JavaDoc[] aAbstract = Gadgets.split(oSearch.doc(oTopDoc[d].doc).get("abstract"), '¨');
173           aRetArr[d] = new BugRecord(Integer.parseInt(oDoc.get("number")),
174                        oDoc.get("guid"), oDoc.get("container"), oDoc.get("title"),
175                        oDoc.get("author"), oDoc.get("created"), oDoc.get("type"),
176                        oDoc.get("status"), oDoc.get("priority"),
177                        oDoc.get("severity"), oDoc.get("abstract"));
178         } // next
179
} else {
180         aRetArr = null;
181       }
182     } else {
183       Hits oHitSet = oSearch.search(oQry);
184       int iHitCount = oHitSet.length();
185       if (iHitCount>0) {
186         aRetArr = new BugRecord[iHitCount];
187         for (int h=0; h<iHitCount; h++) {
188           oDoc = oHitSet.doc(h);
189           aRetArr[h] = new BugRecord(Integer.parseInt(oDoc.get("number")),
190                        oDoc.get("guid"), oDoc.get("container"), oDoc.get("title"),
191                        oDoc.get("author"), oDoc.get("created"), oDoc.get("type"),
192                        oDoc.get("status"), oDoc.get("priority"),
193                        oDoc.get("severity"), oDoc.get("abstract"));
194         } // next
195
} else {
196         aRetArr = null;
197       }
198     } // fi (iLimit>0)
199

200     if (oSortBy!=null) {
201       Arrays.sort(aRetArr, oSortBy);
202     }
203
204     if (DebugFile.trace) {
205       DebugFile.decIdent();
206       if (null==aRetArr)
207         DebugFile.writeln("End BugSearcher.search() : no records found");
208       else
209         DebugFile.writeln("End BugSearcher.search() : "+String.valueOf(aRetArr.length));
210     }
211     return aRetArr;
212   } // search
213
}
214
Popular Tags