Hi, I'm new to Lucene, so I thought I'd write a test to understand Lucene. However, I'm running into issues with searching after adding documents into an index. The test fails at assertContains in testAddAndSearch() and testDelete(). I'm sure it's something trivial. Can someone please help?
Thanks in advance, Frank import java.io.File; import java.io.IOException; import java.util.Random; import junit.framework.TestCase; import org.apache.log4j.Logger; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.document.Document; import org.apache.lucene.document.Field; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.Term; import org.apache.lucene.search.Hits; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.TermQuery; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import amazon.listingsearch.util.FileUtil; public class LuceneTest extends TestCase { @SuppressWarnings("unused") private static final Logger log = Logger.getLogger(LuceneTest.class); private static final int INDEX_LIMIT = 100; private static final boolean CREATE_NEW = true; private File indexDir; private Directory directory; private Random random = new Random(); protected void setUp() throws Exception { String tmpDirPath = System.getProperty("java.io.tmpdir", "/tmp"); this.indexDir = new File(tmpDirPath, "index"); this.directory = FSDirectory.getDirectory(indexDir, CREATE_NEW); log.debug("The index directory is " + indexDir); } protected void tearDown() throws Exception { this.directory.close(); FileUtil.recursiveDelete(this.indexDir); } private static final String KEY_FIELD_NAME = "key"; private static final String VALUE_FIELD_NAME = "value"; private static final String KEY_FIELD_VALUE_PREFIX = ""; private static final String VALUE_FIELD_VALUE_PREFIX = "value"; public void testAddAndSearch() throws Exception { populateIndex(); for (int i = 0; i < INDEX_LIMIT; i++) { String keyValue = KEY_FIELD_VALUE_PREFIX + i; Term term = new Term(KEY_FIELD_NAME, keyValue); assertContains(term); } } public void testDelete() throws Exception { populateIndex(); String docToBeDeleted = KEY_FIELD_VALUE_PREFIX + random.nextInt(INDEX_LIMIT); Term term = new Term(KEY_FIELD_NAME, docToBeDeleted); assertContains(term); delete(term); assertNotContains(term); } protected void addOrUpdate(Document doc) throws IOException { IndexWriter indexWriter = null; Analyzer analyzer = new StandardAnalyzer(); try { indexWriter = new IndexWriter(directory, analyzer, CREATE_NEW); indexWriter.addDocument(doc); } finally { indexWriter.close(); } } protected int delete(Term term) throws IOException { IndexReader indexReader = null; try { indexReader = IndexReader.open(directory); return indexReader.deleteDocuments(term); } finally { indexReader.close(); } } protected int searchOccurrences(Term term) throws IOException { IndexSearcher indexSearcher = null; try { indexSearcher = new IndexSearcher(directory); TermQuery query = new TermQuery(term); Hits hits = indexSearcher.search(query); return hits.length(); } finally { indexSearcher.close(); } } private void populateIndex() throws IOException { for (int i = 0; i < INDEX_LIMIT; i++) { Document doc = new Document(); String keyValue = KEY_FIELD_VALUE_PREFIX + i; Field keyField = new Field(KEY_FIELD_NAME, keyValue, Field.Store.NO, Field.Index.UN_TOKENIZED); doc.add(keyField); String valueValue = VALUE_FIELD_VALUE_PREFIX + i; Field valueField = new Field(VALUE_FIELD_NAME, valueValue, Field.Store.YES, Field.Index.TOKENIZED); doc.add(valueField); addOrUpdate(doc); } } private void assertContains(Term term) throws IOException { assertEquals("The index should contains " + term, 1, searchOccurrences(term)); } private void assertNotContains(Term term) throws IOException { assertEquals("The index should not contain " + term, 0, searchOccurrences(term)); } }