001: /*
002: Copyright (C) 2003-2005 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1ē2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.lucene;
034:
035: import java.util.Arrays;
036: import java.util.Date;
037: import java.util.Comparator;
038: import java.io.IOException;
039:
040: import org.apache.lucene.document.*;
041: import org.apache.lucene.search.*;
042: import org.apache.lucene.queryParser.*;
043: import org.apache.lucene.analysis.SimpleAnalyzer;
044:
045: import com.knowgate.debug.DebugFile;
046: import com.knowgate.misc.Gadgets;
047:
048: /**
049: * Search into a Lucene full text index for e-mails
050: * @author Sergio Montoro Ten
051: * @version 3.0
052: */
053: public class MailSearcher {
054:
055: public MailSearcher() {
056: }
057:
058: /**
059: * Compose a Lucene query based on given parameters
060: * @param sLuceneIndexPath String Base path for Lucene indexes excluding WorkArea and table name
061: * @param sWorkArea String GUID of WorkArea to be searched, cannot be null
062: * @param sFolderName String
063: * @param sSender String
064: * @param sRecipient String
065: * @param sSubject String
066: * @param sFromDate String
067: * @param sToDate String
068: * @param sText String
069: * @param iLimit int
070: * @param oSortBy Comparator
071: * @return MailRecord[]
072: * @throws ParseException
073: * @throws IOException
074: * @throws NullPointerException
075: */
076: public static MailRecord[] search(String sLuceneIndexPath,
077: String sWorkArea, String sFolderName, String sSender,
078: String sRecipient, String sSubject, String sFromDate,
079: String sToDate, String sText, int iLimit, Comparator oSortBy)
080: throws ParseException, IOException, NullPointerException {
081:
082: if (null == sLuceneIndexPath)
083: throw new NullPointerException(
084: "MailSearcher.search() luceindex parameter cannot be null");
085:
086: if (null == sWorkArea)
087: throw new NullPointerException(
088: "MailSearcher.search() workarea parameter cannot be null");
089:
090: if (DebugFile.trace) {
091: DebugFile.writeln("Begin MailSearcher.search("
092: + sLuceneIndexPath + "," + sWorkArea + ","
093: + sFolderName + "," + sSender + "," + sRecipient
094: + "," + sSubject + "," + sFromDate + "," + sToDate
095: + "," + sText + "," + String.valueOf(iLimit) + ")");
096: DebugFile.incIdent();
097: }
098:
099: MailRecord[] aRetArr;
100:
101: String sQry = "workarea:" + sWorkArea;
102:
103: if (null != sFolderName)
104: sQry += " AND container:\"" + sFolderName + "\"";
105:
106: if (null != sSender)
107: sQry += " AND author:\"" + Gadgets.ASCIIEncode(sSender)
108: + "\"";
109:
110: if (null != sRecipient)
111: sQry += " AND recipients:\""
112: + Gadgets.ASCIIEncode(sRecipient) + "\"";
113:
114: if (null != sSubject)
115: sQry += " AND title:\"" + Gadgets.ASCIIEncode(sSubject)
116: + "\"";
117:
118: if (sFromDate != null && sToDate != null)
119: sQry += " AND created:["
120: + Gadgets.removeChar(sFromDate, '-') + " TO "
121: + Gadgets.removeChar(sToDate, '-') + "]";
122: else if (sFromDate != null)
123: sQry += " AND created:["
124: + Gadgets.removeChar(sFromDate, '-')
125: + " TO 21991231]";
126: else if (sToDate != null)
127: sQry += " AND created:[19790101 TO "
128: + Gadgets.removeChar(sToDate, '-') + "]";
129:
130: if (null != sText)
131: sQry += " AND text:\"" + Gadgets.ASCIIEncode(sText) + "\"";
132:
133: QueryParser oQpr = new QueryParser("text", new SimpleAnalyzer());
134: if (DebugFile.trace)
135: DebugFile.writeln("QueryParser.parse(" + sQry + ")");
136: Query oQry = oQpr.parse(sQry);
137:
138: if (DebugFile.trace)
139: DebugFile.writeln("new IndexSearcher(" + sLuceneIndexPath
140: + ")");
141: IndexSearcher oSearch = new IndexSearcher(sLuceneIndexPath);
142:
143: if (iLimit > 0) {
144: TopDocs oTopSet = oSearch.search(oQry, null, iLimit);
145: if (oTopSet.scoreDocs != null) {
146: ScoreDoc[] oTopDoc = oTopSet.scoreDocs;
147: int iDocCount = oTopDoc.length;
148: aRetArr = new MailRecord[iDocCount];
149: for (int d = 0; d < iDocCount; d++) {
150: String[] aAbstract = Gadgets.split(oSearch.doc(
151: oTopDoc[d].doc).get("abstract"), '¨');
152: aRetArr[d] = new MailRecord(aAbstract[0],
153: aAbstract[1], aAbstract[2], aAbstract[3],
154: aAbstract[4], aAbstract[5], sFolderName);
155: } // next
156: } else {
157: aRetArr = null;
158: }
159: } else {
160: Hits oHitSet = oSearch.search(oQry);
161: int iHitCount = oHitSet.length();
162: if (iHitCount > 0) {
163: aRetArr = new MailRecord[iHitCount];
164: for (int h = 0; h < iHitCount; h++) {
165: String[] aAbstract = Gadgets.split(oHitSet.doc(h)
166: .get("abstract"), '¨');
167: aRetArr[h] = new MailRecord(aAbstract[0],
168: aAbstract[1], aAbstract[2], aAbstract[3],
169: aAbstract[4], aAbstract[5], sFolderName);
170: } // next
171: } else {
172: aRetArr = null;
173: }
174: } // fi (iLimit>0)
175:
176: if (oSortBy != null) {
177: Arrays.sort(aRetArr, oSortBy);
178: }
179:
180: if (DebugFile.trace) {
181: DebugFile.decIdent();
182: if (null == aRetArr)
183: DebugFile
184: .writeln("End MailSearcer.search() : no records found");
185: else
186: DebugFile.writeln("End MailSearcer.search() : "
187: + String.valueOf(aRetArr.length));
188: }
189: return aRetArr;
190: } // search
191: }
|