Revision b227bf36
Added by Ben Clark over 15 years ago
cdmlib-persistence/src/main/java/eu/etaxonomy/cdm/persistence/dao/hibernate/AlternativeSpellingSuggestionParser.java | ||
---|---|---|
41 | 41 |
import org.hibernate.search.store.DirectoryProvider; |
42 | 42 |
import org.springframework.beans.factory.annotation.Autowired; |
43 | 43 |
import org.springframework.orm.hibernate3.support.HibernateDaoSupport; |
44 |
import org.springmodules.lucene.index.factory.IndexFactory; |
|
45 |
import org.springmodules.lucene.index.factory.LuceneIndexWriter; |
|
46 | 44 |
|
47 | 45 |
import eu.etaxonomy.cdm.model.common.CdmBase; |
48 | 46 |
import eu.etaxonomy.cdm.persistence.dao.IAlternativeSpellingSuggestionParser; |
49 | 47 |
|
50 | 48 |
|
51 | 49 |
public abstract class AlternativeSpellingSuggestionParser<T extends CdmBase> extends HibernateDaoSupport implements |
52 |
IAlternativeSpellingSuggestionParser {
|
|
50 |
IAlternativeSpellingSuggestionParser { |
|
53 | 51 |
private static Log log = LogFactory.getLog(AlternativeSpellingSuggestionParser.class); |
54 |
|
|
52 |
|
|
55 | 53 |
private String defaultField; |
56 |
protected IndexFactory indexFactory; |
|
57 | 54 |
protected Directory directory; |
58 | 55 |
private Class<T> type; |
59 |
|
|
56 |
private Class<? extends T> indexedClasses[]; |
|
57 |
|
|
60 | 58 |
public AlternativeSpellingSuggestionParser(Class<T> type) { |
61 | 59 |
this.type = type; |
62 | 60 |
} |
63 |
|
|
61 |
|
|
62 |
public void setIndexedClasses(Class<? extends T> indexedClasses[]) { |
|
63 |
this.indexedClasses = indexedClasses; |
|
64 |
} |
|
65 |
|
|
64 | 66 |
public abstract void setDirectory(Directory directory); |
65 |
|
|
66 |
public abstract void setIndexFactory(IndexFactory indexFactory); |
|
67 |
|
|
67 |
|
|
68 | 68 |
@Autowired |
69 | 69 |
public void setHibernateSessionFactory(SessionFactory sessionFactory) { |
70 | 70 |
super.setSessionFactory(sessionFactory); |
71 | 71 |
} |
72 |
|
|
72 |
|
|
73 | 73 |
public void setDefaultField(String defaultField) { |
74 | 74 |
this.defaultField = defaultField; |
75 | 75 |
} |
... | ... | |
84 | 84 |
Query query = querySuggester.parse(queryString); |
85 | 85 |
return querySuggester.hasSuggestedQuery() ? query : null; |
86 | 86 |
} |
87 |
|
|
87 |
|
|
88 | 88 |
private class QuerySuggester extends QueryParser { |
89 | 89 |
private boolean suggestedQuery = false; |
90 | 90 |
public QuerySuggester(String field, Analyzer analyzer) { |
... | ... | |
94 | 94 |
// Copied from org.apache.lucene.queryParser.QueryParser |
95 | 95 |
// replacing construction of TermQuery with call to getTermQuery() |
96 | 96 |
// which finds close matches. |
97 |
TokenStream source = getAnalyzer().tokenStream(field, new StringReader(queryText));
|
|
97 |
TokenStream source = getAnalyzer().tokenStream(field, new StringReader(queryText));
|
|
98 | 98 |
Vector v = new Vector(); |
99 | 99 |
Token t; |
100 | 100 |
|
... | ... | |
127 | 127 |
return q; |
128 | 128 |
} |
129 | 129 |
} |
130 |
|
|
130 |
|
|
131 | 131 |
private Term getTerm(String field, String queryText) throws ParseException { |
132 |
|
|
132 |
|
|
133 | 133 |
try { |
134 | 134 |
SpellChecker spellChecker = new SpellChecker(directory); |
135 | 135 |
if (spellChecker.exist(queryText)) { |
... | ... | |
149 | 149 |
return suggestedQuery; |
150 | 150 |
} |
151 | 151 |
} |
152 |
|
|
152 |
|
|
153 | 153 |
public void refresh() { |
154 |
|
|
155 |
FullTextSession fullTextSession = Search.createFullTextSession(getSession()); |
|
154 |
FullTextSession fullTextSession = Search.getFullTextSession(getSession()); |
|
156 | 155 |
SearchFactory searchFactory = fullTextSession.getSearchFactory(); |
157 |
DirectoryProvider directoryProvider = searchFactory.getDirectoryProviders(type)[0]; |
|
158 |
|
|
159 |
ReaderProvider readerProvider = searchFactory.getReaderProvider(); |
|
160 |
|
|
161 |
IndexReader indexReader = null; |
|
162 |
LuceneIndexWriter indexWriter = null; |
|
163 | 156 |
try { |
164 |
try { |
|
165 |
indexWriter = indexFactory.getIndexWriter(); |
|
166 |
indexReader = readerProvider.openReader(directoryProvider); |
|
167 |
|
|
168 |
log.debug("Creating new dictionary for words in " + defaultField + " docs " + indexReader.numDocs()); |
|
169 |
Dictionary dictionary = new LuceneDictionary(indexReader, defaultField); |
|
170 |
|
|
171 |
if(log.isDebugEnabled()) { |
|
172 |
Iterator iterator = dictionary.getWordsIterator(); |
|
173 |
while(iterator.hasNext()) { |
|
174 |
log.debug("Indexing word " + iterator.next()); |
|
175 |
} |
|
176 |
} |
|
177 |
|
|
178 |
SpellChecker spellChecker = new SpellChecker(directory); |
|
179 |
spellChecker.indexDictionary(dictionary); |
|
180 |
} catch (CorruptIndexException cie) { |
|
181 |
log.error("Spellings index is corrupted", cie); |
|
182 |
} finally { |
|
183 |
if (indexReader != null) { |
|
184 |
readerProvider.closeReader(indexReader); |
|
185 |
} |
|
186 |
if(indexWriter != null) { |
|
187 |
indexWriter.close(); |
|
188 |
} |
|
157 |
SpellChecker spellChecker = new SpellChecker(directory); |
|
158 |
|
|
159 |
for(Class<? extends T> indexedClass : indexedClasses) { |
|
160 |
DirectoryProvider directoryProvider = searchFactory.getDirectoryProviders(indexedClass)[0]; |
|
161 |
ReaderProvider readerProvider = searchFactory.getReaderProvider(); |
|
162 |
IndexReader indexReader = null; |
|
163 |
|
|
164 |
try { |
|
165 |
|
|
166 |
indexReader = readerProvider.openReader(directoryProvider); |
|
167 |
log.debug("Creating new dictionary for words in " + defaultField + " docs " + indexReader.numDocs()); |
|
168 |
|
|
169 |
Dictionary dictionary = new LuceneDictionary(indexReader, defaultField); |
|
170 |
if(log.isDebugEnabled()) { |
|
171 |
Iterator iterator = dictionary.getWordsIterator(); |
|
172 |
while(iterator.hasNext()) { |
|
173 |
log.debug("Indexing word " + iterator.next()); |
|
174 |
} |
|
175 |
} |
|
176 |
|
|
177 |
spellChecker.indexDictionary(dictionary); |
|
178 |
} catch (CorruptIndexException cie) { |
|
179 |
log.error("Spellings index is corrupted", cie); |
|
180 |
} finally { |
|
181 |
if (indexReader != null) { |
|
182 |
readerProvider.closeReader(indexReader); |
|
183 |
} |
|
184 |
} |
|
189 | 185 |
} |
190 |
} catch (IOException ioe) {
|
|
186 |
}catch (IOException ioe) { |
|
191 | 187 |
log.error(ioe); |
192 | 188 |
} |
193 | 189 |
} |
Also available in: Unified diff
Minor problems discovered whilst refactoring CATE:
Corrections to User / Group / Authority impl
Changes due to upgrade of hibernate-search