Hello, I have found that the spellchecker behaves a bit strange. My spell indexer class below doesn't work if I use the spellfield string set in the constructor directly, but it does work if I use the intern() value. The problem resides in the hasNext() method of LuceneIterator where an object comparison between two field strings occur. Now I got everything to work here, but I do wonder why it didn't work in my first version. public boolean hasNext() { has_next_called = true; try { // if there is still words if (!termEnum.next()) { actualTerm = null; return false; } // if the next word are in the field actualTerm = termEnum.term(); String fieldt = actualTerm.field(); if (fieldt != field) { actualTerm = null; return false; } return true; } catch (IOException ex) { ex.printStackTrace(); return false; } }
public class SpellIndexer { private Directory indexMainDirectory; private Directory indexSpellDirectory; private String spellField; public SpellIndexer(String spellField, String indexMainDirectoryStr, String indexSpellDirectoryStr) throws IOException{ this.spellField = spellField; this.indexMainDirectory = FSDirectory.getDirectory(indexMainDirectoryStr,false); this.indexSpellDirectory = FSDirectory.getDirectory(indexSpellDirectoryStr,true); } public boolean rebuildSpellIndex(File indexDir) throws IOException { Directory indexSpellDir = indexDir == null ? indexSpellDirectory : FSDirectory.getDirectory(indexDir,true); IndexReader indexReader = null; boolean success = false; try { indexReader = IndexReader.open(indexMainDirectory); Dictionary dictionary = new LuceneDictionary(indexReader, spellField.intern()); // <---------------- Doesn't work with spellField directly SpellChecker spellChecker = new SpellChecker(indexSpellDir); spellChecker.indexDictionary(dictionary); success = true; } finally { if (indexReader != null) { indexReader.close(); } } return success; } }