private synchronized void reIndex() throws IOException {
UseCase ucReIndex = MonitorFactory.startUseCase("reIndex()");
// build a dictionary (from the spell package)
log.debug("Starting to reindex autocomplete index.");
LuceneIndexLocation source = this.autocompleter.getSource();
LuceneIndexLocation autocompleteLocation = this.autocompleter.getAutocompleteLocation();
String autocompletefield = this.autocompleter.getAutocompletefield();
IndexAccessor sia = source.getAccessor();
IndexReader sourceReader = sia.getReader(false);
LuceneDictionary dict = new LuceneDictionary(sourceReader, autocompletefield);
IndexAccessor aia = autocompleteLocation.getAccessor();
// IndexReader reader = aia.getReader(false);
IndexWriter writer = aia.getWriter();
try {
writer.setMergeFactor(300);
writer.setMaxBufferedDocs(150);
// go through every word, storing the original word (incl. n-grams)
// and the number of times it occurs
// CREATE WORD LIST FROM SOURCE INDEX
Map<String, Integer> wordsMap = new HashMap<String, Integer>();
Iterator<String> iter = (Iterator<String>) dict.getWordsIterator();
while (iter.hasNext()) {
String word = iter.next();
int len = word.length();
if (len < 3) {
continue; // too short we bail but "too long" is fine...
}
if (wordsMap.containsKey(word)) {
throw new IllegalStateException("Lucene returned a bad word list");
} else {
// use the number of documents this word appears in
wordsMap.put(word, sourceReader.docFreq(new Term(autocompletefield, word)));
}
}
// DELETE OLD OBJECTS FROM INDEX
writer.deleteAll();
// UPDATE DOCUMENTS IN AUTOCOMPLETE INDEX
for (String word : wordsMap.keySet()) {
// ok index the word
Document doc = new Document();
doc.add(new Field(SOURCE_WORD_FIELD, word, Field.Store.YES, Field.Index.NOT_ANALYZED_NO_NORMS)); // orig term
doc.add(new Field(GRAMMED_WORDS_FIELD, word, Field.Store.YES, Field.Index.ANALYZED)); // grammed
doc.add(new Field(COUNT_FIELD, Integer.toString(wordsMap.get(word)), Field.Store.YES,
Field.Index.NOT_ANALYZED_NO_NORMS)); // count
writer.addDocument(doc);
}
writer.optimize();
autocompleteLocation.createReopenFile();
} finally {
sia.release(sourceReader, false);
// close writer