创建index
/**<p>描述:使用最新版Lucene3.5.0的代码示例。</p><p>功能:搜索指定文件夹下的html文件,创建索引。</p>
* V Lucene 3.5
* 创建索引
*/
public static void createIndex(){
File indexDir = new File(LUCENEINDEX);
File dataDir = new File(LUCENEDATA);
Analyzer luceneAnalyzer = new StandardAnalyzer(Version.LUCENE_35);
File[] dataFiles = indexDir.listFiles();
IndexWriter indexWriter = null;
try {
/**
* indexWriter = new IndexWriter(SimpleFSDirectory.open(dataDir),luceneAnalyzer, true,IndexWriter.MaxFieldLength.LIMITED);
* 在最新版中这种方式已被不使用。
* 现在使用下面的方式创建indexWriter
*/
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_35, luceneAnalyzer);
indexWriter = new IndexWriter(SimpleFSDirectory.open(dataDir), indexWriterConfig);
long startTime = new Date().getTime();
//注意:filed实例在多次添加的时候可以重用,节约构造field实例的时间。
Field f1 = new Field("name", "", Field.Store.YES, Field.Index.NOT_ANALYZED) ;
Field f2 = new Field("path", "", Field.Store.YES, Field.Index.NOT_ANALYZED) ;
List<FilePojo> result = tree(indexDir);
for (FilePojo po : result) {
String name = po.getName();
String path = po.getPath();
try {
System.out.println("Indexing file: " + path);
Document doc = new Document();
f1.setValue(name);
doc.add(f1);
f2.setValue(path);
doc.add(f2);
indexWriter.addDocument(doc);
} catch (IOException e) {
e.printStackTrace();
}
}
//查看IndexWriter里面有多少个索引
System.out.println("numDocs:"+indexWriter.numDocs());
indexWriter.commit();
long endTime = new Date().getTime();
System.out.println("耗时:" + (endTime - startTime));
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (LockObtainFailedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
indexWriter.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
搜索index
/**
* V Lucene 3.5
* 搜索索引
*/
public static void readIndex(){
IndexSearcher indexSearch = null;
try {
//保存索引文件的地方
Directory dir = new SimpleFSDirectory(new File(LUCENEDATA));
/**
* indexSearch = new IndexSearcher(dir);<被弃用>
*/
indexSearch = new IndexSearcher(IndexReader.open(dir));
//创建QueryParser对象,第一个参数表示Lucene的版本,第二个表示搜索Field的字段,第三个表示搜索使用分词器
QueryParser queryParser = new QueryParser(Version.LUCENE_35,
"name", new StandardAnalyzer(Version.LUCENE_35));
String key = "tESt.html";
//生成Query对象
Query query = queryParser.parse(key);
/**
* 搜索结果 TopDocs里面有scoreDocs[]数组,里面保存着索引值
* API: Finds the top n hits for query
*/
TopDocs hits = indexSearch.search(query, 10);
//hits.totalHits表示一共搜到多少个
System.out.println("共搜索到"+hits.totalHits+"个 '"+key+"'");
//循环hits.scoreDocs数据,并使用indexSearch.doc方法把Document还原,再拿出对应的字段的值
for (ScoreDoc sdoc : hits.scoreDocs) {
Document doc = indexSearch.doc(sdoc.doc);
System.out.println(doc.get("path"));
}
indexSearch.close();
} catch (CorruptIndexException e) {
e.printStackTrace();
} catch (IOException e) {
System.out.println("There is no index files...");
} catch (ParseException e) {
e.printStackTrace();
}
}
其他示例:
http://liuzidong.iteye.com/blog/1316940
分享到:
相关推荐
Lucene3.5全部源码,打包jar文件,可以直接打开查看源码,Lucene开发必备
lucene3.5 IKAnalyzer3.2.5 实例中文分词通过,目前在网上找的lucene 和IKAnalyzer 的最新版本测试通过。内含:示例代码,以及最新jar包。 lucene lucene3.5 IKAnalyzer IKAnalyzer3.2.5 jar 中文 分词
luke3.5 可查看lucene3.5索引
lucene 官网的3.5源代码,标准代码,未做任何处理
lucene3.5的创建和增删改查的工程
基于新发布的Lucene3.5 做的一个实例,仅供参考。
lucene3.5高亮
几个经常用到的包,有中文分词器,核心包,高亮包...等等
chm格式的Lucene帮助文档,Lucene3.5
用爬虫爬下来的Lucene3.5 api 可以正常使用
lucene3.5全文检索案例lucene+demo
lucene3.5的API,虽然是英文版,不过还是很有用的
Lucene 3.5&API,最新版
这个为一个lucene3.5所写的程序例子,有助于刚入门的学习lucene的人,更快的的进入到lucene的学习中去。
最新lucene3.5的源码,很好用的~~~
lucene3.5中文分词,详细的注释,适用于初学者。有兴趣的可以看看。
基于新发布的Lucene3.5的学习,对部分模块的研究及学习心得。
介绍lucene3.5的相关技术,包括基本用法、分析器、索引建立与查询,扩展的高亮、分页、以及solr3.5的相关用法
solr_lucene3.5_lukeall-3.5.0.jar.zip
Lucene3.5视频教程(内含分享链接) 一共50集, 包含各部分讲解及源码