2014-02-11 44 views
1
package lia.meetlucene; 
import org.apache.lucene.index.IndexWriter; 
import org.apache.lucene.analysis.standard.StandardAnalyzer; 
import org.apache.lucene.document.Document; 
import org.apache.lucene.document.Field; 
import org.apache.lucene.store.FSDirectory; 
import org.apache.lucene.store.Directory; 
import org.apache.lucene.util.Version; 

import java.io.File; 
import java.io.FileFilter; 
import java.io.IOException; 
import java.io.FileReader; 

public class Indexer { 

    public static void main(String[] args) throws Exception { 
    if (args.length != 2) { 
     throw new IllegalArgumentException("Usage: java " + Indexer.class.getName() 
     + " <index dir> <data dir>"); 
    } 
    String indexDir = args[0];   //1 
    String dataDir = args[1];   //2 

    long start = System.currentTimeMillis(); 
    Indexer indexer = new Indexer(indexDir); 
    int numIndexed; 
    try { 
     numIndexed = indexer.index(dataDir); 
    } finally { 
     indexer.close(); 
    } 
    long end = System.currentTimeMillis(); 

    System.out.println("Indexing " + numIndexed + " files took " 
     + (end - start) + " milliseconds"); 
    } 

    private IndexWriter writer; 

    public Indexer(String indexDir) throws IOException { 
    Directory dir = FSDirectory.open(new File(indexDir)); 
    writer = new IndexWriter(dir,   //3 
       new StandardAnalyzer(  //3 
        Version.LUCENE_30),//3 
       true,      //3 
          IndexWriter.MaxFieldLength.UNLIMITED); //3 
    } 

    public void close() throws IOException { 
    writer.close();        //4 
    } 

    public int index(String dataDir) 
    throws Exception { 
try{ 
    File[] files = new File(dataDir).listFiles(); 

    for (File f: files) {     
    ************************************************   
     if(f.isDirectory())   // I added this if block which is causing error 
     { 
      index(dataDir); 
     } 
    ************************************************ 
     else if (!f.isDirectory() && 
      !f.isHidden() && 
      f.exists() && 
      f.canRead() 
     ) { 
     indexFile(f); 
     } 
    } 
} 
     catch (IOException e) { 
      e.printStackTrace(); 
     } 
    return writer.numDocs();      //5 
    } 


    protected Document getDocument(File f) throws Exception { 
    Document doc = new Document(); 
    doc.add(new Field("contents", new FileReader(f)));  //7 
    doc.add(new Field("filename", f.getName(),    //8 
       Field.Store.YES, Field.Index.NOT_ANALYZED));//8 
    doc.add(new Field("fullpath", f.getCanonicalPath(),  //9 
       Field.Store.YES, Field.Index.NOT_ANALYZED));//9 
    return doc; 
    } 

    private void indexFile(File f) throws Exception { 
    System.out.println("Indexing " + f.getCanonicalPath()); 
    Document doc = getDocument(f); 
    writer.addDocument(doc);        //10 
    } 
} 

這是給出Lucene的行動書的程序。它僅索引不在子文件夾中的父文件夾中的文件。所以我添加了一個if塊以遞歸方式查找子文件夾中的文件。但是在運行這個程序之後,它正在創建write.lock文件,並且即使在關閉命令提示符後它也會繼續創建索引文件。代碼有什麼問題?錯誤而索引文件

我是新來的LuceneJava,以前我嘗試過使用apache commons io來查找子文件夾,但是我得到的包不存在錯誤(package org.apache.commons.io does not exist error)。

+0

,你能否告訴我們的錯誤和線造成的錯誤? – Salah

+0

用* – samnaction

+0

@JUBA標出該行不顯示錯誤,繼續運行 – samnaction

回答

0

是的,它會繼續運行,因爲你一直都在通過相同的路徑。所以你不會達到close()方法,那爲什麼write.lock保持存在。

在這裏你目前的代碼。

if(f.isDirectory())   
{ 
    index(dataDir); // dataDir is the orginal path 
} 

你必須做的是這樣的:

if(f.isDirectory())   
{ 
    index(f.getAbsolutePath()); 
} 
+0

謝謝,真的有所幫助。還有一個問題,即使在關閉命令提示符後,索引仍然會繼續運行? – samnaction

+0

不,事實上,'lucene'沒有機會解鎖目錄,'lucene'通過調用'writer.close()'關閉'IndexWriter'時解鎖資源,在你的情況下close方法沒有調用,所以'write.lock'文件仍然存在。 – Salah