KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Created on Oct 19, 2004
3  */

4 package com.openedit.modules.search;
5
6 import java.io.File JavaDoc;
7 import java.text.SimpleDateFormat JavaDoc;
8 import java.util.Date JavaDoc;
9 import java.util.GregorianCalendar JavaDoc;
10
11 import org.apache.commons.logging.Log;
12 import org.apache.commons.logging.LogFactory;
13 import org.apache.lucene.analysis.Analyzer;
14 import org.apache.lucene.analysis.standard.StandardAnalyzer;
15 import org.apache.lucene.index.IndexReader;
16 import org.apache.lucene.queryParser.QueryParser;
17 import org.apache.lucene.search.BooleanQuery;
18 import org.apache.lucene.search.Hits;
19 import org.apache.lucene.search.IndexSearcher;
20 import org.apache.lucene.search.Query;
21 import org.apache.lucene.search.Searcher;
22
23 import com.openedit.OpenEditRuntimeException;
24 import com.openedit.util.FileUtils;
25
26 /**
27  * @author cburkey
28  *
29  */

30 public abstract class BaseLuceneSearch
31 {
32     private static final Log log = LogFactory.getLog(BaseLuceneSearch.class);
33     protected Analyzer fieldAnalyzer;
34     protected Searcher fieldLiveSearcher;
35     protected File JavaDoc fieldSearchDirectory;
36     protected SimpleDateFormat JavaDoc fieldFormat = new SimpleDateFormat JavaDoc("yyyy_MM_dd_HH_mm_ss");
37     protected String JavaDoc fieldIndexId = "A" + System.currentTimeMillis();
38
39     public abstract void reIndexAll() throws Exception JavaDoc;
40     public Hits search(String JavaDoc inQuery) throws Exception JavaDoc
41     {
42         if ((inQuery == null) || (inQuery.length() == -1))
43         {
44             return null;
45         }
46         try
47         {
48             QueryParser parser = new QueryParser("description", getAnalyzer());
49             parser.setDefaultOperator(QueryParser.AND_OPERATOR);
50             Query query1 = parser.parse(inQuery);
51     
52             Hits hits = getLiveSearcher().search(query1);
53     
54             log.debug( "Searching for: " + query1.toString("contents") + " " + hits.length() + " total matching documents");
55             return hits;
56         }
57         catch (Exception JavaDoc ex)
58         {
59             log.error(ex);
60             throw ex;
61         }
62     
63     }
64     protected File JavaDoc buildIndexDir(String JavaDoc inName)
65     {
66         File JavaDoc indexDir = new File JavaDoc(getSearchDirectory() + "/search/index/" + inName);
67         if ( !indexDir.exists() )
68         {
69             indexDir.mkdirs();
70         }
71         return indexDir;
72     }
73     protected void setLiveSearcher(Searcher inSearch)
74     {
75         fieldLiveSearcher = inSearch;
76     }
77     protected Searcher getLiveSearcher()
78     {
79         if ( fieldLiveSearcher == null)
80         {
81             synchronized( this )
82             {
83                 if ( fieldLiveSearcher == null)
84                 {
85                     BooleanQuery.setMaxClauseCount(100000);
86                     setIndexId("A" + System.currentTimeMillis());
87                     fieldLiveSearcher = copyIndexA();
88                 }
89             }
90         }
91         return fieldLiveSearcher;
92     }
93
94     protected Searcher copyIndexA()
95     {
96         try
97         {
98             //TODO: Look for any old index and delete it
99
File JavaDoc index = buildIndexDir("A");
100             if( !IndexReader.indexExists(index))
101             {
102                 log.error("No valid index found in A");
103                 reIndexAll();
104             }
105             //IndexReader.unlock(arg0)lock(index);
106

107             Date JavaDoc now = new Date JavaDoc();
108             String JavaDoc today = fieldFormat.format(now);
109             File JavaDoc tmp = buildIndexDir("temp/" + today );
110             FileUtils utils = new FileUtils();
111             utils.copyFiles(index, tmp);
112             IndexSearcher a = new IndexSearcher(tmp.getAbsolutePath() );
113             
114             File JavaDoc[] children = tmp.getParentFile().listFiles();
115             for (int i = 0; i < children.length; i++)
116             {
117                 String JavaDoc name = children[i].getName();
118                 if( name.startsWith("20"))
119                 {
120                     Date JavaDoc old = fieldFormat.parse(children[i].getName());
121                     GregorianCalendar JavaDoc cal = new GregorianCalendar JavaDoc();
122                     cal.setTime(old);
123                     cal.add(GregorianCalendar.DAY_OF_YEAR,1);
124                     old = cal.getTime();
125                     if( old.before(now))
126                     {
127                         log.info("Clear old temp index");
128                         utils.deleteAll(children[i]);
129                     }
130                 }
131             }
132             return a;
133         }
134         catch ( Exception JavaDoc ex)
135         {
136             throw new OpenEditRuntimeException(ex);
137         }
138     }
139     public void setAnalyzer(Analyzer inAnalyzer)
140     {
141         fieldAnalyzer = inAnalyzer;
142     }
143
144     public Analyzer getAnalyzer()
145     {
146         if (fieldAnalyzer == null) {
147             fieldAnalyzer = new StandardAnalyzer();
148         }
149         return fieldAnalyzer;
150     }
151
152     public File JavaDoc getSearchDirectory()
153     {
154         return fieldSearchDirectory;
155     }
156     public void setSearchDirectory(File JavaDoc inSearchDirectory)
157     {
158         fieldSearchDirectory = inSearchDirectory;
159     }
160
161     public String JavaDoc getIndexId()
162     {
163         return fieldIndexId;
164     }
165
166     public void setIndexId(String JavaDoc inIndexCount)
167     {
168         fieldIndexId = inIndexCount;
169     }
170     protected File JavaDoc buildLiveIndexDir()
171     {
172         return buildIndexDir("A");
173     }
174     public void clearIndex()
175     {
176         setLiveSearcher(null);
177     }
178
179 }
180
Popular Tags