package com.bt;import org.apache.lucene.document.Document;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.Hits;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.store.Directory;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import java.io.File;
import java.util.Date;/**
 * This code was originally written for Erik's Lucene intro java.net article
 */
public class Searcher { public static void main(String[] args1) throws Exception {
String[] args = { "E:\\test1", "txt" };
if (args.length != 2) {
throw new Exception("Usage: java " + Searcher.class.getName()
+ " <index dir> <query>");
} File indexDir = new File(args[0]);
String q = args[1]; if (!indexDir.exists() || !indexDir.isDirectory()) {
throw new Exception(indexDir
+ " does not exist or is not a directory.");
}
System.out.println("--->");
search(indexDir, q);
System.out.println("--->");
} public static void search(File indexDir, String q) throws Exception {
Directory fsDir = FSDirectory.getDirectory(indexDir, false);
IndexSearcher is = new IndexSearcher(fsDir); Query query = QueryParser.parse(q, "contents", new StandardAnalyzer());
long start = new Date().getTime();
Hits hits = is.search(query);
long end = new Date().getTime(); System.err.println("Found " + hits.length() + " document(s) (in "
+ (end - start) + " milliseconds) that matched query '" + q
+ "':"); for (int i = 0; i < hits.length(); i++) {
Document doc = hits.doc(i);
System.out.println(doc.get("filename"));
}
}
}这个是 lucene in action 一段小程序 一运行出错 Exception in thread "main" java.io.FileNotFoundException: E:\test1\segments (系统找不到指定的文件。)
at java.io.RandomAccessFile.open(Native Method)
at java.io.RandomAccessFile.<init>(Unknown Source)
at org.apache.lucene.store.FSInputStream$Descriptor.<init>(FSDirectory.java:376)
at org.apache.lucene.store.FSInputStream.<init>(FSDirectory.java:405)
at org.apache.lucene.store.FSDirectory.openFile(FSDirectory.java:268)
at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:40)
at org.apache.lucene.index.IndexReader$1.doBody(IndexReader.java:116)---> at org.apache.lucene.store.Lock$With.run(Lock.java:109)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:111)
at org.apache.lucene.index.IndexReader.open(IndexReader.java:106)
at org.apache.lucene.search.IndexSearcher.<init>(IndexSearcher.java:43)
at com.bt.Searcher.search(Searcher.java:40)
at com.bt.Searcher.main(Searcher.java:34)

解决方案 »

  1.   

    String[] args = { "E:\\test1\\", "txt" };
      

  2.   

    怎么了? 有问题 你在看看 没有问题的不是main函数一顶 参数要写 成args 的 
      

  3.   

    String[] args = { "E:\\test1", "txt" };
    String[] args = { "E:\\test1\\", "txt" }; 
    是不一样的
      

  4.   


    还是这个错 Exception in thread "main" java.io.FileNotFoundException: E:\test1\segments (系统找不到指定的文件。)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at org.apache.lucene.store.FSInputStream$Descriptor.<init>(FSDirectory.java:376)
    at org.apache.lucene.store.FSInputStream.<init>(FSDirectory.java:405)
    at org.apache.lucene.store.FSDirectory.openFile(FSDirectory.java:268)
    at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:40)
    at org.apache.lucene.index.IndexReader$1.doBody(IndexReader.java:116)
    at org.apache.lucene.store.Lock$With.run(Lock.java:109)
    at org.apache.lucene.index.IndexReader.open(IndexReader.java:111)
    at org.apache.lucene.index.IndexReader.open(IndexReader.java:106)
    at org.apache.lucene.search.IndexSearcher.<init>(IndexSearcher.java:43)
    at com.bt.Searcher.search(Searcher.java:40)
    at com.bt.Searcher.main(Searcher.java:34)
      

  5.   

    程序找不到索引文件!!
    Directory fsDir = FSDirectory.getDirectory(indexDir, false);
    indexDir目录是放索引文件的吗???如果创建好的索引文件,有3个(在没有修改,删除,添加索引的话)
      

  6.   


    这个程序本身不能 创建 你说的那三个 吗?
    我没有改 ,目录也没有那个 segments 文件
      

  7.   

    索引文件中缺少segments文件!
      

  8.   

    我现在机子上没有索引文件,在生成后的索引文件是三个文件的,你可以自己随便生成个索引文件。然后用你的有你的程序serach一下,看看能不能执行!如果可以的话,可能是索引文件损坏或者索引没有生成完!你在网上找create index的代码!在生成索引的时候一定要用new StandardAnalyzer分词方式!自己试试把!
      

  9.   


    package com.bt;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 java.io.File;
    import java.io.IOException;
    import java.io.FileReader;
    import java.util.Date;/**
     * This code was originally written for Erik's Lucene intro java.net article
     */
    public class Indexer { public static void main(String[] args1) throws Exception { String[] args = { "E:\\test\\", "E:\\test1\\" };
    if (args.length != 2) {
    throw new Exception("Usage: java " + Indexer.class.getName()
    + " <index dir> <data dir>");
    }
    File indexDir = new File(args[0]);
    File dataDir = new File(args[1]); long start = new Date().getTime();
    int numIndexed = index(indexDir, dataDir);
    long end = new Date().getTime(); System.out.println("Indexing " + numIndexed + " files took "
    + (end - start) + " milliseconds");
    } public static int index(File indexDir, File dataDir) throws IOException { if (!dataDir.exists() || !dataDir.isDirectory()) {
    throw new IOException(dataDir
    + " does not exist or is not a directory");
    } IndexWriter writer = new IndexWriter(indexDir, new StandardAnalyzer(),
    true);
    writer.setUseCompoundFile(false); indexDirectory(writer, dataDir); int numIndexed = writer.docCount();
    writer.optimize();
    writer.close();
    return numIndexed;
    } private static void indexDirectory(IndexWriter writer, File dir)
    throws IOException { File[] files = dir.listFiles(); for (int i = 0; i < files.length; i++) {
    File f = files[i];
    if (f.isDirectory()) {
    indexDirectory(writer, f); // recurse
    } else if (f.getName().endsWith(".txt")) {
    indexFile(writer, f);
    }
    }
    } private static void indexFile(IndexWriter writer, File f)
    throws IOException { if (f.isHidden() || !f.exists() || !f.canRead()) {
    return;
    } System.out.println("Indexing " + f.getCanonicalPath()); Document doc = new Document();
    doc.add(Field.Text("contents", new FileReader(f)));
    doc.add(Field.Keyword("filename", f.getCanonicalPath()));
    writer.addDocument(doc);
    }
    }运行后 Indexing C:\test1\txt.txt
    Indexing C:\test1\复件 (2) 3.txt
    Indexing C:\test1\复件 (3) 3.txt
    Indexing C:\test1\复件 (4) 3.txt
    Indexing C:\test1\复件 (5) 3.txt
    Indexing C:\test1\复件 (6) 3.txt
    Indexing C:\test1\复件 (7) 3.txt
    Indexing C:\test1\复件 (8) 3.txt
    Indexing C:\test1\复件 3.txt
    Indexing 9 files took 171 milliseconds
    然后我 执行 Searcher。java 找 txt的 还是找不到,没有报错了Found 0 document(s) (in 10 milliseconds) that matched query 'txt':
      

  10.   

    兄弟,你这个问题解决的怎么样了?我也出现了一样的问题,索引已经创建成功了,但是就是:
    java.io.FileNotFoundException: D:\houseSearch\segments (系统找不到指定的文件。)
    at java.io.RandomAccessFile.open(Native Method)
    at java.io.RandomAccessFile.<init>(Unknown Source)
    at org.apache.lucene.store.FSIndexInput$Descriptor.<init>(FSDirectory.java:430)
    at org.apache.lucene.store.FSIndexInput.<init>(FSDirectory.java:439)
    at org.apache.lucene.store.FSDirectory.openInput(FSDirectory.java:329)
    at org.apache.lucene.index.SegmentInfos.read(SegmentInfos.java:45)
    at org.apache.lucene.index.IndexReader$1.doBody(IndexReader.java:146)
    at org.apache.lucene.store.Lock$With.run(Lock.java:99)
    at org.apache.lucene.index.IndexReader.open(IndexReader.java:141)
    at org.apache.lucene.index.IndexReader.open(IndexReader.java:125)
    at org.apache.lucene.search.IndexSearcher.<init>(IndexSearcher.java:42)
    at com.house.search.SearchDao.DoSearch(SearchDao.java:40)
    at org.apache.jsp.allHouse_jsp._jspService(allHouse_jsp.java:63)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:386)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:864)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1665)
    at java.lang.Thread.run(Unknown Source)
    ===========================================================================
    但是在java文件下运行时正确的
    在JSP下是找不到segment文件,奇怪了