Java、lucene、searcher索引器问题如何解决?
我必须用lucene和java做一些东西,但我不知道如何开始。 我必须做servlet,它必须从浏览器接收,然后进行搜索,最后用搜索结果生成页面。 浏览器应该可以选择在名称中搜索或在名称和页面内部搜索。浏览器应按此方向搜索html文件/var/www/manual/。 作为助手,我已经有两个文件:Indexer。java和Searcher。爪哇
索引器
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"));
}
}
}
建议之一是使用HTMLDocument。lucene演示中的java用于索引html文档
有人能帮我解决这个问题吗? 谢谢你的建议
# 1 楼答案
我不知道你的项目是否需要Lucene,但如果你对Lucene的全文搜索功能感兴趣,那么你可能会发现从Solr开始更容易(http://lucene.apache.org/solr/),一个基于Lucene的搜索引擎。Solr和Lucene是由同一个人开发的,因此您可以确保一切都以正确的方式完成,并且可能比您编写的代码更快
另外,Lucene的网站上有一个很好的“入门”指南,可以帮助你了解如何使用Lucene(什么是目录,如何读写索引?)以及最佳实践(重用IndexWriter实例等):