Note that there are some explanatory texts on larger screens.

plurals
  1. POproblem with incremental update in lucene
    primarykey
    data
    text
    <p>I am creating a program that can index many text files in different folder. so that's mean every folder that has text files get indexed and its index are stored in another folder. so this another folder acts like a universal index of all files in my computer. and I am using lucene to achieve this because lucene fully supported incremental update. this is the source code into which I use it for indexing.</p> <pre><code>public class SimpleFileIndexer { public static void main(String[] args) throws Exception { int i=0; while(i&lt;2) { File indexDir = new File("C:/Users/Raden/Documents/myindex"); File dataDir = new File("C:/Users/Raden/Documents/indexthis"); String suffix = "txt"; SimpleFileIndexer indexer = new SimpleFileIndexer(); int numIndex = indexer.index(indexDir, dataDir, suffix); System.out.println("Total files indexed " + numIndex); i++; Thread.sleep(1000); } } private int index(File indexDir, File dataDir, String suffix) throws Exception { RAMDirectory ramDir = new RAMDirectory(); // 1 @SuppressWarnings("deprecation") IndexWriter indexWriter = new IndexWriter( ramDir, // 2 new StandardAnalyzer(Version.LUCENE_CURRENT), true, IndexWriter.MaxFieldLength.UNLIMITED); indexWriter.setUseCompoundFile(false); indexDirectory(indexWriter, dataDir, suffix); int numIndexed = indexWriter.maxDoc(); indexWriter.optimize(); indexWriter.close(); Directory.copy(ramDir, FSDirectory.open(indexDir), false); // 3 return numIndexed; } private void indexDirectory(IndexWriter indexWriter, File dataDir, String suffix) throws IOException { File[] files = dataDir.listFiles(); for (int i = 0; i &lt; files.length; i++) { File f = files[i]; if (f.isDirectory()) { indexDirectory(indexWriter, f, suffix); } else { indexFileWithIndexWriter(indexWriter, f, suffix); } } } private void indexFileWithIndexWriter(IndexWriter indexWriter, File f, String suffix) throws IOException { if (f.isHidden() || f.isDirectory() || !f.canRead() || !f.exists()) { return; } if (suffix!=null &amp;&amp; !f.getName().endsWith(suffix)) { return; } System.out.println("Indexing file " + f.getCanonicalPath()); Document doc = new Document(); doc.add(new Field("contents", new FileReader(f))); doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.ANALYZED)); indexWriter.addDocument(doc); } } </code></pre> <p>and this is the source code that I use for searching the lucene-created index</p> <pre><code>public class SimpleSearcher { public static void main(String[] args) throws Exception { File indexDir = new File("C:/Users/Raden/Documents/myindex"); String query = "revolution"; int hits = 100; SimpleSearcher searcher = new SimpleSearcher(); searcher.searchIndex(indexDir, query, hits); } private void searchIndex(File indexDir, String queryStr, int maxHits) throws Exception { Directory directory = FSDirectory.open(indexDir); IndexSearcher searcher = new IndexSearcher(directory); @SuppressWarnings("deprecation") QueryParser parser = new QueryParser(Version.LUCENE_30, "contents", new StandardAnalyzer(Version.LUCENE_CURRENT)); Query query = parser.parse(queryStr); TopDocs topDocs = searcher.search(query, maxHits); ScoreDoc[] hits = topDocs.scoreDocs; for (int i = 0; i &lt; hits.length; i++) { int docId = hits[i].doc; Document d = searcher.doc(docId); System.out.println(d.get("filename")); } System.out.println("Found " + hits.length); } } </code></pre> <p>the problem I am having now is that the indexing program I created above seem can't do any incremental update. I mean I can search for a text file but only for the file that existed in the last folder to which I already indexed, and the other previous folder that I had already indexed seems to be missing in the search result and didn't get displayed. can you tell me what went wrong in my code? I just wanted to be able to have incremental update feature in my source code. so in essence, my program seems to be overwriting the existing index with the new one instead of merging it. </p> <p>thanks though</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload