比如说C盘文件夹s下有三个txt文件,分别为1.txt(内容为:中华人民共和国人民万岁) ,2.txt(内容为:中国人民),3。txt(内容为:人民的国家民众做主)。如何通过lucene建立索引后根据关键词查询,比如说查询"人民",得到共有四次符合,查询“民”共有五次符合,最后能给个能完整运行的代码,谢谢大虾们,偶刚接触这个东西,不怎么懂,期盼高人求助。
解决方案 »
- File类也许用的到
- 如何判断telnet执行结束
- 帮忙解决jsp连接 mysql出现的错误提示??加分
- Java EE 5 SDK 和J2EE 1.4 SDK是什么关系??
- 100分求mq发送和接收文件的java代码或者实例
- 初学GUI,遇到的一个问题(附例程)
- 菜菜的问题:Java中的MessageBox
- 有什么好的办法?
- 请问我在一个applet中动态的写了很多的JLabel(每个JLabel都没有具体的名称),如何在点击某一个JLabel时出发鼠标事件,得到该JLabel的文本
- 为什么JAVA里的main()必须是static的呢?谢谢指教!
- JCounter 方法是什么意思??
- 有关JCF的List collection产生的数据处理的问题
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"));
}
}
}