Java,lucene,searcher indexer问题怎么办呢?

时间:2011-08-31 20:07:10

标签: java lucene

我必须用lucene和java制作一些东西,但我不知道如何开始。 我必须做一个必须从浏览器接收的servlet,然后进行搜索,最后制作带有结果的页面。 浏览器应该可以在名称,名称和页面内部进行搜索。浏览器应该按照这个方向搜索html文件/ var / www / manual /。 作为帮助,我已经有两个文件:Indexer.java和Searcher.java。

索引

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexWriter;

/**
 * This code was originally written for
 * Erik's Lucene intro java.net article
 */
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")) {
//      } else if (f.getName().endsWith(".html.en")) {
        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(new Field("contents", new FileReader(f)));
    doc.add(new Field("filename", f.getCanonicalPath(), Field.Store.YES, Field.Index.UN_TOKENIZED));
    //doc.add(new Field("filename", new StringReader(f.getCanonicalPath())));
    writer.addDocument(doc);
  }


}

搜索器

import java.io.File;
import java.io.FileReader;
import java.io.StringReader;
import java.util.Date;

import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Hits;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;

/**
 * This code was originally written for
 * Erik's Lucene intro java.net article
 */
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());   DEPRECATED
    QueryParser qp = new QueryParser("contents", new StandardAnalyzer());
    Query query = qp.parse(q);
    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-demos中的HTMLDocument.java作为索引html文档。

有人可以帮我解决这个问题吗? 谢谢你的任何建议。

1 个答案:

答案 0 :(得分:1)

我不知道Lucene是否是您项目的要求,但如果您对Lucene的全文搜索功能感兴趣,那么您可能会发现更容易从Solr开始(http://lucene.apache。 org / solr /),一个基于Lucene的搜索引擎。 Solr是由与Lucene相同的人开发的,因此您可以确保一切都以正确的方式完成,并且可能比您编写的代码更快。

否则,Lucene网站上有一个很好的“入门”指南,它将帮助您了解如何使用Lucene(什么是目录,如何读取和编写索引?)和最佳实践(重用IndexWriter实例等) 。):