LUCENE-1152 Fix for calling indexDictionary after clearIndex call

git-svn-id: https://svn.apache.org/repos/asf/lucene/java/trunk@659013 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Otis Gospodnetic 2008-05-22 06:21:17 +00:00
parent 2175d84d64
commit a379a67875
2 changed files with 10 additions and 32 deletions

View File

@ -95,6 +95,10 @@ Bug fixes
8. LUCENE-1003: Stop RussianAnalyzer from removing numbers. 8. LUCENE-1003: Stop RussianAnalyzer from removing numbers.
(TUSUR OpenTeam, Dmitry Lihachev via Otis Gospodnetic) (TUSUR OpenTeam, Dmitry Lihachev via Otis Gospodnetic)
9. LUCENE-1152: SpellChecker fix around clearIndex and indexDictionary
methods, plus removal of IndexReader reference.
(Naveen Belkale via Otis Gospodnetic)
New features New features
1. LUCENE-1137: Added Token.set/getFlags() accessors for passing more information about a Token through the analysis 1. LUCENE-1137: Added Token.set/getFlags() accessors for passing more information about a Token through the analysis

View File

@ -72,7 +72,6 @@ public class SpellChecker {
private float bStart = 2.0f; private float bStart = 2.0f;
private float bEnd = 1.0f; private float bEnd = 1.0f;
private IndexReader reader;
private IndexSearcher searcher; private IndexSearcher searcher;
// minimum score for hits generated by the spell checker query // minimum score for hits generated by the spell checker query
@ -284,11 +283,12 @@ public class SpellChecker {
* @throws IOException * @throws IOException
*/ */
public void clearIndex() throws IOException { public void clearIndex() throws IOException {
if (IndexReader.isLocked(spellIndex)){
IndexReader.unlock(spellIndex);
}
IndexWriter writer = new IndexWriter(spellIndex, null, true); IndexWriter writer = new IndexWriter(spellIndex, null, true);
writer.close(); writer.close();
//close the old searcher
searcher.close();
searcher = new IndexSearcher(this.spellIndex);
} }
/** /**
@ -298,10 +298,7 @@ public class SpellChecker {
* @return true iff the word exists in the index * @return true iff the word exists in the index
*/ */
public boolean exist(String word) throws IOException { public boolean exist(String word) throws IOException {
if (reader == null) { return searcher.docFreq(new Term(F_WORD, word)) > 0;
reader = IndexReader.open(spellIndex);
}
return reader.docFreq(new Term(F_WORD, word)) > 0;
} }
/** /**
@ -310,11 +307,7 @@ public class SpellChecker {
* @throws IOException * @throws IOException
*/ */
public void indexDictionary(Dictionary dict) throws IOException { public void indexDictionary(Dictionary dict) throws IOException {
if (IndexReader.isLocked(spellIndex)){ IndexWriter writer = new IndexWriter(spellIndex, true, new WhitespaceAnalyzer());
IndexReader.unlock(spellIndex);
}
IndexWriter writer = new IndexWriter(spellIndex, new WhitespaceAnalyzer(),
!IndexReader.indexExists(spellIndex));
writer.setMergeFactor(300); writer.setMergeFactor(300);
writer.setMaxBufferedDocs(150); writer.setMaxBufferedDocs(150);
@ -338,12 +331,6 @@ public class SpellChecker {
// close writer // close writer
writer.optimize(); writer.optimize();
writer.close(); writer.close();
// close reader so it will be re-opened (and see the new content) when exist()
// is called the next time:
if (reader != null) {
reader.close();
reader = null;
}
// also re-open the spell index to see our own changes when the next suggestion // also re-open the spell index to see our own changes when the next suggestion
// is fetched: // is fetched:
searcher.close(); searcher.close();
@ -395,17 +382,4 @@ public class SpellChecker {
} }
} }
} }
/**
* Closes the internal IndexReader.
*/
protected void finalize() throws Throwable {
try {
if (reader != null) {
reader.close();
}
} finally {
super.finalize();
}
}
} }