1 4 package com.openedit.modules.search; 5 6 import java.io.File ; 7 import java.text.SimpleDateFormat ; 8 import java.util.Date ; 9 import java.util.GregorianCalendar ; 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 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 fieldSearchDirectory; 36 protected SimpleDateFormat fieldFormat = new SimpleDateFormat ("yyyy_MM_dd_HH_mm_ss"); 37 protected String fieldIndexId = "A" + System.currentTimeMillis(); 38 39 public abstract void reIndexAll() throws Exception ; 40 public Hits search(String inQuery) throws Exception 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 ex) 58 { 59 log.error(ex); 60 throw ex; 61 } 62 63 } 64 protected File buildIndexDir(String inName) 65 { 66 File indexDir = new File (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 File index = buildIndexDir("A"); 100 if( !IndexReader.indexExists(index)) 101 { 102 log.error("No valid index found in A"); 103 reIndexAll(); 104 } 105 107 Date now = new Date (); 108 String today = fieldFormat.format(now); 109 File tmp = buildIndexDir("temp/" + today ); 110 FileUtils utils = new FileUtils(); 111 utils.copyFiles(index, tmp); 112 IndexSearcher a = new IndexSearcher(tmp.getAbsolutePath() ); 113 114 File [] children = tmp.getParentFile().listFiles(); 115 for (int i = 0; i < children.length; i++) 116 { 117 String name = children[i].getName(); 118 if( name.startsWith("20")) 119 { 120 Date old = fieldFormat.parse(children[i].getName()); 121 GregorianCalendar cal = new GregorianCalendar (); 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 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 getSearchDirectory() 153 { 154 return fieldSearchDirectory; 155 } 156 public void setSearchDirectory(File inSearchDirectory) 157 { 158 fieldSearchDirectory = inSearchDirectory; 159 } 160 161 public String getIndexId() 162 { 163 return fieldIndexId; 164 } 165 166 public void setIndexId(String inIndexCount) 167 { 168 fieldIndexId = inIndexCount; 169 } 170 protected File buildLiveIndexDir() 171 { 172 return buildIndexDir("A"); 173 } 174 public void clearIndex() 175 { 176 setLiveSearcher(null); 177 } 178 179 } 180 | Popular Tags |