没有合适的资源?快使用搜索试试~ 我知道了~
首页经典的lucene实例代码及详细解析以及lucene结构流程介绍
资源详情
资源评论
资源推荐

本文摘要:
本文并给出一个经典的 lucene 全文收索例子代码。该例子功能是从磁盘
文档建立索引,搜索该文档中的哪个 TXT 文件包含所搜索内容。最后再大致介
绍 Lucene 的结构模块,应用流程希望对网友能有帮助。
创建索引代码:
package luceneL;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.Reader;
import java.util.Date;
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.index.CorruptIndexException;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.store.LockObtainFailedException;
public class createIndex {
public static boolean createDocumentIndex(){
boolean bool=false;
//被索引的目录文件夹
File dirpath=new File("E:\documentTest");
//索引文件存放的目录文件夹
File indexpath=new File("E:\Index");
//分词,分词有 StandardAnalyzer 和 SimpleAnalyzer 两种
//lucene
是将一句句话,一段话 Field,分成一个个词 Term 进行索引搜索的。
Analyzer analyzer=new StandardAnalyzer();
try {
//向 E:\Index 保存建立的索引 Index 内容
//用到 IndexWriter 类,这里需要传入的三个参数为:
//(索引目录文件夹,分词)
IndexWriter index=new IndexWriter(indexpath,analyzer,true);

File[] txtfiles=dirpath.listFiles();
long startTime=new Date().getTime();
for(int i=0;i<txtfiles.length;i++){
if(txtfiles[i].isFile()&&txtfiles[i].getName().endsWith(".txt")){
System.out.println("文
件"+txtfiles[i].getCanonicalPath()+"正在索引中。。。");
//Read 将 txt
内容存进内存
Reader read=new FileReader(txtfiles[i]);
//创建 Document 的实例
Document doc=new Document();
//将 field 存进索引的 Document
//Document 添加读取的文章内容(缓存在内存中的文章内容 read)
doc.add(new Field("content",read));
//Document 添加文章对应路径信息等
//
doc.add(new
Field("path",txtfiles[i].getAbsolutePath(),Field.Store.YES,Field.Inde
x.NO));
//index 加 Document,索引创建成功
index.addDocument(doc);
}
}
//索引优化 optimize(),合并磁盘上的索引文件,以便减少文件的数量,从而也
减少搜索索引的时间
index.optimize();
//注意关闭 IndexWriter,立即将索引文件写入到目录磁盘中,生成索引文件
index.close();
long endTime=new Date().getTime();
System.out.println("共花了"+(endTime-startTime)+"毫秒将文档增加到
索引中"+indexpath.getPath());
bool=true;
} catch (CorruptIndexException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (LockObtainFailedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bool;
}

public static void main(String[] a){
createIndex.createDocumentIndex();
}
}//class createIndex
在创建索引代码中,我们注意到类 IndexWriter 的构造函数需要三个参数,
第一个参数指定了所创建的索引要存放的位置,他可以是一个 File 对象,也可
以是一个 FSDirectory 对象或者 RAMDirectory 对象。第二个参数指定了
Analyzer 类的一个实现,也就是指定这个索引是用哪个分词器对文挡内容进
行分词。第三个参数是一个布尔型的变量,如果为 true 的话就代表创建一个
新的索引,为 false 的话就代表在原来索引的基础上进行操作。接着程序遍历
了目录下面的所有文本文档,并为每一个文本文档创建了一个 Document 对
象。然后把文本文档的两个属性:路径和内容加入到了两个 Field 对象中,接
着在把这两个 Field 对象加入到 Document 对象中,最后把这个文档用
IndexWriter 类的 add 方法加入到索引中去。这样我们便完成了索引的创建。
接下来我们进入在建立好的索引上进行搜索的部分。
搜索代码:
package luceneL;
import java.io.File;
import java.io.IOException;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.CorruptIndexException;
import org.apache.lucene.index.Term;
剩余13页未读,继续阅读
















安全验证
文档复制为VIP权益,开通VIP直接复制

评论1