1 32 33 package com.knowgate.lucene; 34 35 import java.util.Arrays ; 36 import java.util.Date ; 37 import java.util.Comparator ; 38 import java.io.IOException ; 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 53 public class BugSearcher { 54 55 public BugSearcher() { 56 } 57 58 81 public static BugRecord[] search (String sLuceneIndexPath, 82 String sWorkArea, String sProjectGUID, 83 String sReportedBy, String sWrittenBy, 84 String sTitle, 85 String sFromDate, String sToDate, 86 String sType, String sPriority, String sSeverity, 87 String sStatus, String sText, 88 String sComments, 89 int iLimit, Comparator oSortBy) 90 throws ParseException, IOException , NullPointerException { 91 92 if (null==sLuceneIndexPath) 93 throw new NullPointerException ("BugSearcher.search() luceindex parameter cannot be null"); 94 95 if (null==sWorkArea) 96 throw new NullPointerException ("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 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 [] 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 } } 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 } } else { 196 aRetArr = null; 197 } 198 } 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 } } 214 | Popular Tags |