1.lucene创建索引和搜索,主要用到一下几个类,IndexWriter,Document,Analyzer;IndexSearcher,QueryParser,Query,TopDocs,
2.通过FSDirectory和RAMDirectory的并用,可以提高速度。先把磁盘上的索引文件载入内存,然后在内存操作,免去了IO操作,可以提高效率,最后退出时,要把内存操作的结果保存在磁盘上。
3.fsIndexWriter.optimize();优化索引文件,把多个cfs文件合并成一个
4.建立索引和进行搜索时应该使用同一个分词器。
ps:用到的jar包:/LuceneDemo/lib/je-analysis-1.5.3.jar(中文分词器)
/LuceneDemo/lib/lucene-analyzers-2.4.0.jar(lucenne自带的)
/LuceneDemo/lib/lucene-core-2.4.0.jar
/LuceneDemo/lib/lucene-highlighter-2.4.0.jar
package com.bjsxt.helloworld;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriter.MaxFieldLength;
import org.apache.lucene.queryParser.MultiFieldQueryParser;
import org.apache.lucene.queryParser.QueryParser;
import org.apache.lucene.search.Filter;
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.store.RAMDirectory;
import com.bjsxt.utils.File2DocumentUtil;
public class HelloWorld {
/**
* @param args
*/
String filePath = "D:\\flexWorkespace\\LuceneDemo\\luceneDataSource\\IndexWriter addDocument's a javadoc .txt";
String indexPath = "D:\\flexWorkespace\\LuceneDemo\\indexPath";
Analyzer analyzer = new StandardAnalyzer();
public static void main(String[] args) throws Exception {
new HelloWorld().createIndexByDir();
new HelloWorld().searchByDir("document");
}
public void createIndex() throws Exception {
// File file = new File(filePath);
Document doc = File2DocumentUtil.file2Document(filePath);
// IndexWriter 是用来操作(增删改)索引库的
IndexWriter iw = new IndexWriter(indexPath, analyzer, true,
MaxFieldLength.LIMITED);
iw.addDocument(doc);
iw.close();
}
public void createIndexByDir()throws Exception {
//1.创建时候载入文件系统里的索引
Directory fsDir = FSDirectory.getDirectory(indexPath);
Directory ramDir = new RAMDirectory(fsDir);
//new ramIndexWriter时不需要重新创建
IndexWriter ramIndexWriter = new IndexWriter(ramDir, analyzer, MaxFieldLength.LIMITED);
//添加document
Document doc = File2DocumentUtil.file2Document(filePath);
ramIndexWriter.addDocument(doc);
ramIndexWriter.close();
//2.退出时保存内存里的索引
//new fsIndexWriter时需要重新创建,即删除原来的索引文件
IndexWriter fsIndexWriter = new IndexWriter(fsDir, analyzer, true, MaxFieldLength.LIMITED);
// Directory[] dir = {ramDir};
fsIndexWriter.addIndexesNoOptimize(new Directory[]{ramDir});
//把内存里的东西提交后才优化
fsIndexWriter.commit();
// 优化索引文件,把多个cfs文件合并成一个
fsIndexWriter.optimize();
fsIndexWriter.close();
}
public void search(String queryStr) throws Exception {
String[] fields = { "name", "content" };
QueryParser queryParser = new MultiFieldQueryParser(fields, analyzer);
Query query = null;
Filter filter = null;
query = queryParser.parse(queryStr);
IndexSearcher indexSearcher = null;
indexSearcher = new IndexSearcher(indexPath);
TopDocs topDocs = indexSearcher.search(query, filter, 1000);
System.out.println("总共有" + topDocs.totalHits + "条记录:");
for (ScoreDoc scoreDoc : topDocs.scoreDocs) {
int docNum = scoreDoc.doc;
Document doc = indexSearcher.doc(docNum);
File2DocumentUtil.printDocumentInfo(doc);
}
}
}
工具类:
import org.apache.lucene.document.Field.Index;
import org.apache.lucene.document.Field.Store;
public class File2DocumentUtil {
public static Document file2Document(String filePath) throws Exception {
File file = new File(filePath);
Document doc = new Document();
doc.add(new Field("name", file.getName(), Store.YES, Index.ANALYZED));
doc.add(new Field("content", readFileContent(file), Store.YES,
Index.ANALYZED));
doc.add(new Field("size", String.valueOf(file.length()), Store.YES,
Index.NOT_ANALYZED));
doc.add(new Field("path", file.getAbsolutePath(), Store.YES, Index.NO));
return doc;
}
private static String readFileContent(File file) throws Exception {
// TODO Auto-generated method stub
StringBuffer content = new StringBuffer();
BufferedReader br = new BufferedReader(new FileReader(file));
for (String line = null; (line = br.readLine()) != null;) {
content.append(line).append("\n");
}
return content.toString();
}
public static void printDocumentInfo(Document doc) {
System.out.println("------------------------------");
System.out.println("name:" + doc.get("name"));
System.out.println("content:" + doc.get("content"));
System.out.println("path:" + doc.get("path"));
System.out.println("size:" + doc.get("size"));
}
}
分享到:
相关推荐
Lucene5写的全文搜索的demo,包括创建索引和搜索
Lucene创建与搜索索引。个人做的流程总结。
Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的知识,仅仅学会使用库中的一个类,...
iTextPDFExtractor.java ------ ...--PDFBox创建PDF文件的Lucene索引 PDFBoxPathIndex.java ------- --PDFBox创建指定目录PDF文档索引 POIOfficeExtractor.java ----- -- POI处理Excel和Word文档代码
lucene 4.7.2支持java 6 ,之后的版本需要java 7以上,创建、删除、修改索引,搜索支持通用对象(可以根据对象类型搜索),可以范围搜索、排序、高亮,希望有所帮助
Lucene.NET2.9.2 的应用Demo,从提取自淘车网的搜索项目,主要分两部分,创建索引和查询索引. 效果展示:http://www.taoche.com
创建索引 一、创建词法分析器 二、创建索引存储目录 三、创建索引写入器 四、将内容存储到索引 关键字查询 一、创建索引存储目录读取器 二、创建索引搜索器 三、解析查询 四、获取结果
介绍了Lucene的基础知识,包括Lucene的历史和发展情况、使用Lucene创建索引和执行搜索的基本方法以及中文分词的应用,最后做了两个应用项目。第2部分:数据解析。介绍解析不同格式数据(如Word、PDF等)的方法,包括...
通过lucene创建索引,然后进行各种搜索
lucene 实现的源代码,主要是java语言编写的。里面还含有索引创建,搜索等功能,简单易用功能强大,希望能对学习lucene和java的朋友有所帮助
Lucene 创建索引与搜索的简单应用,包含了测试所需要的jar 包。
资源名称:Lucene实战视频教程资源目录:【】01_lucenc简介和创建索引初步【】02_lucene简介和搜索初步【】03_lucene索引_创建_域选项【】04_lucene索引_的删除和更新【】05_lucene索引_加权操作和Luke的简单演示...
基于struts2实现的lucene搜索引擎 包括创建索引,搜索索引,高亮显示 有详细的注释,适合初学者学习
lucene原理:被检索文档创建索引,在索引的基础上搜索。
a) 创建索引 b) 查询索引 3、配置开发环境 4、创建索引库 5、查询索引库 6、分析器的分析过程 a) 测试分析器的分词效果 b) 第三方中文分析器 7、索引库的维护 a) 添加文档 b) 删除文档 c) 修改文档 Lucene的高级查询...
基于HanLP和Lucene的全文索引和搜索库。 安装 您可以在使用Cube Repo 原料药 创建索引目录 WordAnalytics analytics = new WordAnalytics ( new File ( " datadirectory " )); 新增文件 Document document ...
完整代码,基于Lucene的分词,根据搜索引擎的目标和基本内容,将实现功能模块主要划分为创建中文分析器(创建索引)、读取索引文件查询记录、根据输入的内容进行分词、根据关键字进行全文检索、将结果按JSON格式输出...
Lucene 创建索引与搜索的简单应用,包含了测试所需要的jar 包。
lucene为数据库搜索建立增量索引.txt lucene数据库索引.txt 新闻系统全文检索的思绪.txt lucene学习笔记 1 .txt lucene学习笔记 2.txt lucene学习笔记 3 .txt lucene入门实战.txt Lucene 的学习 .txt ...
// 创建索引 // luceneUtil.index(); // 搜索 luceneUtil.search(); // 删除索引 // luceneUtil.delete(); // 检查索引文件 // luceneUtil.check(); // 恢复删除的索引 // luceneUtil.unDelete(); ...