Lucene3.6 简单的练习案例
导入3.6的核心包就可以了
下载链接:lucene3.6下载地址
使用过程:
建立索引 分词 存储
----------------------------
关键字 分词 搜索
package com.wzh.ml.one;
import java.io.File;
import java.io.FileReader;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
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.FSDirectory;
import org.apache.lucene.util.Version;
public class HelloWorld {
private static final String PATH_OF_FILE = "F:\\MyLucene\\data";
private static final String PATH_OF_INDEX = "F:\\MyLucene\\index";
public void createIndex(){
Directory directory = null;
IndexWriter indexWriter = null;
Document doc = null;
try {
//存放索引的目录
directory = FSDirectory.open(new File(PATH_OF_INDEX));
//由于Lucene2.9之后,其索引的格式就不会再兼容Lucene的所有版本了,所以在创建索引前,要指定其所匹配的Lucene版本号
//索引的建立器 分词 存储等
indexWriter = new IndexWriter(directory, new IndexWriterConfig(Version.LUCENE_36, new StandardAnalyzer(Version.LUCENE_36)));
for(File f : new File(PATH_OF_FILE).listFiles()){
doc = new Document(); //存储要保存的字段
doc.add(new Field("content", new FileReader(f)));
//存储 分词
doc.add(new Field("fileName", f.getName(),Field.Store.YES,Field.Index.ANALYZED));
//存储 不分词
doc.add(new Field("filePath", f.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED));
indexWriter.addDocument(doc);
}
} catch (Exception e) {
e.printStackTrace();
}finally{
if(null != indexWriter){
try {
indexWriter.close();
} catch (Exception e) {
System.out.println("关闭IndexWriter出现异常,堆栈信息如下:");
e.printStackTrace();
}
}
}
}
public void searchFile(){
IndexReader indexReader = null;
try {
//首先要选择一个存放索引的文件夹
indexReader = IndexReader.open(FSDirectory.open(new File(PATH_OF_INDEX)));
IndexSearcher indexSearcher = new IndexSearcher(indexReader);
//创建基于Parser搜索的Query,创建时需指定其"搜索的版本,默认搜索的域,分词器"....这里的域指的是创建索引时Field的名字
QueryParser queryParser = new QueryParser(Version.LUCENE_36,"content",new StandardAnalyzer(Version.LUCENE_36));
Query query = queryParser.parse("你是笨蛋!!!!!!!!!!!!"); //指定==>搜索域为content(即上一行文字指定的"content")中包含"哈哈"的文档
TopDocs tds = indexSearcher.search(query, 10); //第二个参数指定搜索后显示的条数,若查到5条则显示为5条,查到15条则只显示10条
ScoreDoc[] sds = tds.scoreDocs; //TopDocs中存放的并不是我们的文档,而是文档的ScoreDoc对象
for(ScoreDoc sd : sds){ //ScoreDoc对象相当于每个文档的ID号,我们就可以通过ScoreDoc来遍历文档
Document doc = indexSearcher.doc(sd.doc); //sd.doc得到的是文档的序号
System.out.println(doc.get("fileName") + "[" + doc.get("filePath") + "]"); //输出该文档所存储的信息
}
} catch (Exception e) {
e.printStackTrace();
}
}
}