1 package org.mdarad.framework.index; 2 3 import java.io.IOException ; 4 import java.util.Collection ; 5 import java.util.HashMap ; 6 import java.util.Iterator ; 7 import java.util.Locale ; 8 import java.util.Map ; 9 10 import org.apache.lucene.analysis.Analyzer; 11 import org.apache.lucene.analysis.standard.StandardAnalyzer; 12 import org.apache.lucene.document.Document; 13 import org.apache.lucene.index.IndexWriter; 14 import org.apache.lucene.queryParser.ParseException; 15 import org.apache.lucene.queryParser.QueryParser; 16 import org.apache.lucene.search.Hits; 17 import org.apache.lucene.search.IndexSearcher; 18 import org.apache.lucene.search.Query; 19 import org.apache.lucene.search.Searcher; 20 import org.apache.lucene.store.Directory; 21 import org.apache.lucene.store.RAMDirectory; 22 import org.mdarad.framework.delegates.BusinessDelegate; 23 import org.mdarad.framework.exception.SystemException; 24 25 abstract public class AbstractIndexDelegate implements BusinessDelegate { 26 public final static String DEFAULT_FIELD_NAME = "org.mdarad.framework.index.AbstractIndexFacade.DEFAULT_FIELD_NAME"; 27 abstract public Map getEntityDocuments(); 28 29 Map directories = null; 30 31 public void buildIndex() throws SystemException { 32 try { 33 Map documents = getEntityDocuments(); 34 35 Map newDirectories = new HashMap (); 36 Iterator documentLocaleIterator = documents.keySet().iterator(); 37 while(documentLocaleIterator.hasNext()) { 38 Locale locale = (Locale ) documentLocaleIterator.next(); 39 Directory directory = new RAMDirectory(); 40 newDirectories.put(locale, directory); 41 Collection documentsForLocale = (Collection ) documents.get(locale); 42 43 IndexWriter indexWriter = new IndexWriter(directory, getAnalyzerer(locale), true); 44 45 Iterator documentIterator = documentsForLocale.iterator(); 46 while(documentIterator.hasNext()) { 47 indexWriter.addDocument((Document) documentIterator.next()); 48 } 49 50 indexWriter.optimize(); 51 indexWriter.close(); 52 } 53 54 directories = newDirectories; 55 } catch (IOException ioe) { 56 throw new SystemException(ioe); 57 } 58 } 59 60 public Hits getSearchResults(String queryString, Locale locale) throws IndexNotFoundException { 61 Hits hits = null; 62 Directory directory = null; 63 64 if(directories == null) throw new IndexNotFoundException("Directory map has not been initialized."); 65 66 Map directories = this.directories; 67 if(directories != null) { 68 directory = (Directory) directories.get(locale); 69 } 70 71 if(directory == null) throw new IndexNotFoundException("Directory map does not contain locale."); 72 73 Searcher searcher = null; 74 75 try { 76 searcher = new IndexSearcher(directory); 77 } catch (IOException ioe) { 78 throw new IndexNotFoundException(ioe); 79 } 80 81 Query query = null; 82 83 try { 84 85 query = QueryParser.parse(queryString, DEFAULT_FIELD_NAME, getAnalyzerer(locale)); 86 87 } catch (ParseException e) { 88 e.printStackTrace(); 90 } 91 92 try { 93 hits = searcher.search(query); 94 } catch (IOException e) { 95 e.printStackTrace(); 97 } 98 99 return hits; 100 } 101 102 static Analyzer defaultAnalyzer = new StandardAnalyzer(); 103 public Analyzer getAnalyzerer(Locale locale) { 104 return defaultAnalyzer; 106 } 107 } 108 | Popular Tags |