lucene3.0.0 一个子目录及其子目录的文件转换成Document对象 并添加多索引库中进行查询
package com.txt.test2;
import java.io.File;
import java.io.FileReader;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.SimpleFSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
//一个子目录及其子目录的文件转换成Document对象 并添加多索引库中进行查询
public class LuceneTest2 {
private String path = "f:"+File.separator+"cd";
private File storeFile = new File("f:"+File.separator+"indexDir7");
private Analyzer analyzer = new StandardAnalyzer(Version.LUCENE_30);
private IndexWriter writer;
@Test
public void create () throws Exception {
Directory directory = new SimpleFSDirectory(storeFile);
writer = new IndexWriter(directory, analyzer,true, MaxFieldLength.LIMITED);
File file = new File(path);
showFile(file);
writer.close();
}
//获取file目录下的文件及其子目录
public void showFile(File file) throws Exception{
if (file == null) {
return ;
}else if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < files.length; i++) {
//递归
showFile(files[i]);
}
}else {
System.out.println(file.getAbsolutePath());
//建立索引 将文件转换成doc..对象
Document document = new Document();
document.add(new Field("fileName", file.getName(), Store.YES, Index.ANALYZED));
document.add(new Field("filePath", file.getAbsolutePath(), Store.YES, Index.ANALYZED));
//不做存储
document.add(new Field("content", new FileReader(file)));
writer.addDocument(document);
}
}
//查询
@Test
public void serach() throws Exception{
Directory directory = new SimpleFSDirectory(storeFile);
IndexSearcher searcher = new IndexSearcher(directory,true);
QueryParser parser = new QueryParser(Version.LUCENE_30, "filePath", analyzer);
String key = "笔记";
Query query = parser.parse(key);
TopDocs tdDocs = searcher.search(query, 100);
System.out.println("查询的内容是:"+key);
System.out.println("一共命中了多少次:"+tdDocs.totalHits);
System.out.println();
if (tdDocs.scoreDocs != null) {
for (int i = 0; i < tdDocs.scoreDocs.length; i++) {
ScoreDoc sDoc = tdDocs.scoreDocs[i];
System.out.println("文档编号的索引:"+sDoc.doc);
System.out.println("得分:"+sDoc.score);
Document document = searcher.doc(sDoc.doc);
System.out.println("fileName名称是:"+document.get("fileName"));
System.out.println("filePath路径是:"+document.get("filePath"));
System.out.println("content内容是:"+document.get("content"));
}
}else {
System.out.println("没有要查找的内容...");
}
searcher.close();
}
}
分享到:
相关推荐
lucene-3.0.0是lucene目前最新版本
The new version is mostly a cleanup release without any new features. All deprecations targeted to be removed in version 3.0 were removed.
lucene3.0.0 入门DEMO 适合新手使用 先要新建号相关的目录
lucene3.0.0jar包、IKAnalyzer分词器jar包及IKAnayzer中文分词器使用手册
lucene-3.0.0-src.zip
最新的lucene api手册, 感觉看这个比一堆html方便。。。 支持开源
lucene3.0.0的学习资料,里边有lucene的jar包,具体的入门讲解:http://blog.csdn.net/lengyuhong/archive/2010/11/17/6014597.aspx
在Eclipse环境中运用java,Lucene建索引及查询关键字
Lucene创建索引,查询索引的简单使用。
Lucene 并没有规定数据源的格式,而只提供了一个通用的结构(Document 对象)来接受索引的输入,因此输入的数据源可以是:数据库、WORD 文档、PDF 文档、HTML 文档等,只要能够设计相应的解析转换器将数据源构造成 ...
lukeall,Lucene索引文件查看工具可支持Lucene4.7版本的索引查看,直接双击打开,选择索引目录进行使用
Lucene实现索引和查询的实例讲解Lucene实现索引和查询的实例讲解
NULL 博文链接:https://85600367.iteye.com/blog/849554
lucene 做索引查询流程,来自《lucene in action》
为了快速查询,加快查询速度,减少与数据库交互,经常需要用到lucene实现创建索引和查询
lucene 对 xml建立索引 建立索引就是怎么简单 呵呵
该程序代码属于本人2015所写,虽然尚有不足,却实现了对多个文件夹下的数据进行Lucene建立索引和查询功能,并包含了所需的所有jar包,工程直接导入即可运行。
Lucene 删除 合并索引,可以指定几个索引文件合并成一个索引文件。自己写的,有很多不足之处请多指教
这是Lucene3.0索引查看文件 直接运行jar包就可以打开ui界面 陪好路径就可以使用了