2014-02-23 93 views
1

我目前正在索引大量的文本文件沒有有效的目錄。我使用Apache Lucene 4.6.1來索引文件和搜索選項。我正在使用Luke4.6 jar來測試索引。我從下載了這個罐子。但是我得到錯誤 「無效的目錄」盧克 - 在當前位置

錯誤消息:

org.apache.lucene.index.IndexNotFoundException: no segments* file found in [email protected]/home/hduser/prayaas/index [email protected]: files: [_8.cfe, _0.cfs, _7.cfs, _1.si, _2.cfs, _a.fdx, _4.cfs, _8.cfs, _8.si, _6.cfs, _2.si, _9.si, _a.fdt, _1.cfe, _3.si, _1.cfs, _7.cfe, _3.cfe, _5.si, _5.cfs, _5.cfe, _4.cfe, _0.si, _9.cfs, _6.si, _6.cfe, _9.cfe, _0.cfe, _2.cfe, _4.si, _3.cfs, _7.si] 
    at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:801) 
    at org.apache.lucene.index.SegmentInfos$FindSegmentsFile.run(SegmentInfos.java:694) 
    at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:400) 
    at org.getopt.luke.Luke.openIndex(Luke.java:868) 
    at org.getopt.luke.Luke.openOk(Luke.java:678) 
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) 
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 
    at java.lang.reflect.Method.invoke(Method.java:606) 
    at thinlet.Thinlet.invokeImpl(Thinlet.java:4579) 
    at thinlet.Thinlet.invoke(Thinlet.java:4546) 
    at thinlet.Thinlet.handleMouseEvent(Thinlet.java:3937) 
    at thinlet.Thinlet.processEvent(Thinlet.java:2917) 
    at java.awt.Component.dispatchEventImpl(Component.java:4861) 
    at java.awt.Container.dispatchEventImpl(Container.java:2287) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832) 
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492) 
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422) 
    at java.awt.Container.dispatchEventImpl(Container.java:2273) 
    at java.awt.Window.dispatchEventImpl(Window.java:2719) 
    at java.awt.Component.dispatchEvent(Component.java:4687) 
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:735) 
    at java.awt.EventQueue.access$200(EventQueue.java:103) 
    at java.awt.EventQueue$3.run(EventQueue.java:694) 
    at java.awt.EventQueue$3.run(EventQueue.java:692) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87) 
    at java.awt.EventQueue$4.run(EventQueue.java:708) 
    at java.awt.EventQueue$4.run(EventQueue.java:706) 
    at java.security.AccessController.doPrivileged(Native Method) 
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76) 
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:705) 
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242) 
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161) 
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146) 
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138) 
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91) 

我的代碼:

public class LuceneTest 
{ 
public static final String INDEX_DIR_PATH = "/home/hduser/prayaas/index"; 
public static final String FILES_DIR_PATH = "/home/hduser/prayaas/input"; 
private IndexWriter writer; 


public void readFile() 
{ 
    try 
    { 
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/home/hduser/prayaas/test.txt"))); 
     String line; 
     while((line = br.readLine()) != null) 
     { 
      String[] fields = line.split("\t"); 
      for(int i=0;i<fields.length;i++) 
       System.out.print(i+" "+fields[i]+"\t"); 
      System.out.println(); 
     } 
     br.close(); 
    } 
    catch (FileNotFoundException e) 
    { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 

public void initWriter() /* To Initialize IndexWriter class */ 
{ 
    try 
    { 
     Directory dir = FSDirectory.open(new File(INDEX_DIR_PATH)); 
     Analyzer a = new StandardAnalyzer(Version.LUCENE_46); 
     writer  = new IndexWriter(dir, new IndexWriterConfig(Version.LUCENE_46, a).setOpenMode(OpenMode.CREATE)); 
    } 
    catch (IOException e) 
    { 
     System.out.println("Error in Creating Index Writer. Please specify the folder Correctly for storing index"); 
     e.printStackTrace(); 
    } 
} 

public void indexFile(File file) 
{ 
    try 
    { 
     BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file))); 
     String line; 
     while((line=br.readLine()) != null) 
     { 
      String[] fields  = line.split("\t"); 
      String[] fieldNames = {"userid","usertype","age","gender","country","pva","sqa","minspent","visits"}; 
      Document doc = new Document(); 

      for(int i=0;i<5;i++) 
       if(i != 2) 
        doc.add(new StringField(fieldNames[i], fields[i],Store.YES)); 
       else 
        doc.add(new IntField(fieldNames[i], Integer.parseInt(fields[i]),Store.YES)); 

      for(int i=7;i<=8;i++) 
       doc.add(new IntField(fieldNames[i], Integer.parseInt(fields[i]),Store.YES)); 

      for(int i=5;i<7;i++) 
       if(fields.length > 0) 
       { 
        String[] special = fields[i].split("\\|"); 

        for(int j=0;j<special.length;j++) 
        { 
         String[] temp = special[j].split(";"); 
         if(temp.length > 1) 
         { 
          doc.add(new StringField(fieldNames[i], temp[0],Store.YES)); 
          doc.add(new IntField(temp[0], Integer.parseInt(temp[1]),Store.YES)); 
         } 
        } 
       } 

      writer.addDocument(doc); 

     } 

     br.close(); 
    } 
    catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

public void indexDirectory(String path) 
{ 
    File dataDir = new File(path); 
    File[] files = dataDir.listFiles(); 

    for(File file : files) 
    { 
     if(file.isDirectory()) 
      indexDirectory(file.getAbsolutePath()); 
     else 
      indexFile(file); 
    } 
} 
public void searchIndex() 
{ 
    //IndexReader reader = IndexReader.open(new File(INDEX_DIR_PATH)); 
} 

public static void main(String[] args) 
{ 
    LuceneTest lt = new LuceneTest(); 
    lt.initWriter(); 
    lt.indexDirectory(FILES_DIR_PATH); 
    System.out.println("Success"); 
} 
} 

回答

0

只是在我的代碼中的小bug。

我沒有關閉的IndexWriter對象