比如说C盘文件夹s下有三个txt文件,分别为1.txt(内容为:中华人民共和国人民万岁) ,2.txt(内容为:中国人民),3。txt(内容为:人民的国家民众做主)。如何通过lucene建立索引后根据关键词查询,比如说查询"人民",得到共有四次符合,查询“民”共有五次符合,最后能给个能完整运行的代码,谢谢大虾们,偶刚接触这个东西,不怎么懂,期盼高人求助。

解决方案 »

  1.   


    public class Indexer {  public static void main(String[] args) throws Exception {
        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);
      }
    }
    public class Searcher {  public static void main(String[] args) throws Exception {
        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.");
        }    search(indexDir, q);
      }  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"));
        }
      }
    }
      

  2.   

    lucene主要是分词,看你用什么分词器了,分词器的不同,决定你的结果的不通,然后例子网上很多,你可以在网上找下