KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2   Copyright (C) 2003-2005 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 e-mails
50  * @author Sergio Montoro Ten
51  * @version 3.0
52  */

53 public class MailSearcher {
54
55   public MailSearcher() {
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 sFolderName String
63    * @param sSender String
64    * @param sRecipient String
65    * @param sSubject String
66    * @param sFromDate String
67    * @param sToDate String
68    * @param sText String
69    * @param iLimit int
70    * @param oSortBy Comparator
71    * @return MailRecord[]
72    * @throws ParseException
73    * @throws IOException
74    * @throws NullPointerException
75    */

76   public static MailRecord[] search (String JavaDoc sLuceneIndexPath,
77                                      String JavaDoc sWorkArea, String JavaDoc sFolderName,
78                                      String JavaDoc sSender, String JavaDoc sRecipient,
79                                      String JavaDoc sSubject, String JavaDoc sFromDate,
80                                      String JavaDoc sToDate, String JavaDoc sText, int iLimit,
81                                      Comparator JavaDoc oSortBy)
82     throws ParseException, IOException JavaDoc, NullPointerException JavaDoc {
83
84   if (null==sLuceneIndexPath)
85     throw new NullPointerException JavaDoc("MailSearcher.search() luceindex parameter cannot be null");
86
87     if (null==sWorkArea)
88       throw new NullPointerException JavaDoc("MailSearcher.search() workarea parameter cannot be null");
89
90     if (DebugFile.trace) {
91       DebugFile.writeln("Begin MailSearcher.search("+sLuceneIndexPath+","+
92                         sWorkArea+","+sFolderName+","+sSender+","+sRecipient+","+
93                         sSubject+","+sFromDate+","+sToDate+","+sText+","+
94                         String.valueOf(iLimit)+")");
95       DebugFile.incIdent();
96     }
97
98     MailRecord[] aRetArr;
99
100     String JavaDoc sQry = "workarea:"+sWorkArea;
101
102     if (null!=sFolderName) sQry += " AND container:\""+sFolderName+"\"";
103
104     if (null!=sSender) sQry += " AND author:\""+Gadgets.ASCIIEncode(sSender)+"\"";
105
106     if (null!=sRecipient) sQry += " AND recipients:\""+Gadgets.ASCIIEncode(sRecipient)+"\"";
107
108     if (null!=sSubject) sQry += " AND title:\""+Gadgets.ASCIIEncode(sSubject)+"\"";
109
110     if (sFromDate!=null && sToDate!=null)
111       sQry += " AND created:["+Gadgets.removeChar(sFromDate,'-')+" TO "+Gadgets.removeChar(sToDate,'-')+"]";
112     else if (sFromDate!=null)
113       sQry += " AND created:["+Gadgets.removeChar(sFromDate,'-')+" TO 21991231]";
114     else if (sToDate!=null)
115       sQry += " AND created:[19790101 TO "+Gadgets.removeChar(sToDate,'-')+"]";
116
117     if (null!=sText) sQry += " AND text:\""+Gadgets.ASCIIEncode(sText)+"\"";
118
119     QueryParser oQpr = new QueryParser("text", new SimpleAnalyzer());
120     if (DebugFile.trace) DebugFile.writeln("QueryParser.parse("+sQry+")");
121     Query oQry = oQpr.parse(sQry);
122
123     if (DebugFile.trace) DebugFile.writeln("new IndexSearcher("+sLuceneIndexPath+")");
124     IndexSearcher oSearch = new IndexSearcher(sLuceneIndexPath);
125
126     if (iLimit>0) {
127       TopDocs oTopSet = oSearch.search(oQry, null, iLimit);
128       if (oTopSet.scoreDocs!=null) {
129         ScoreDoc[] oTopDoc = oTopSet.scoreDocs;
130         int iDocCount = oTopDoc.length;
131         aRetArr = new MailRecord[iDocCount];
132         for (int d=0; d<iDocCount; d++) {
133           String JavaDoc[] aAbstract = Gadgets.split(oSearch.doc(oTopDoc[d].doc).get("abstract"), '¨');
134           aRetArr[d] = new MailRecord(aAbstract[0], aAbstract[1], aAbstract[2],
135                                       aAbstract[3], aAbstract[4], aAbstract[5],
136                                       sFolderName);
137         } // next
138
} else {
139         aRetArr = null;
140       }
141     } else {
142       Hits oHitSet = oSearch.search(oQry);
143       int iHitCount = oHitSet.length();
144       if (iHitCount>0) {
145         aRetArr = new MailRecord[iHitCount];
146         for (int h=0; h<iHitCount; h++) {
147           String JavaDoc[] aAbstract = Gadgets.split(oHitSet.doc(h).get("abstract"), '¨');
148           aRetArr[h] = new MailRecord(aAbstract[0], aAbstract[1], aAbstract[2],
149                                       aAbstract[3], aAbstract[4], aAbstract[5],
150                                       sFolderName);
151         } // next
152
} else {
153         aRetArr = null;
154       }
155     } // fi (iLimit>0)
156

157     if (oSortBy!=null) {
158       Arrays.sort(aRetArr, oSortBy);
159     }
160
161     if (DebugFile.trace) {
162       DebugFile.decIdent();
163       if (null==aRetArr)
164         DebugFile.writeln("End MailSearcer.search() : no records found");
165       else
166         DebugFile.writeln("End MailSearcer.search() : "+String.valueOf(aRetArr.length));
167     }
168     return aRetArr;
169   } // search
170
}
171
Popular Tags