一、什么是Lucene?
Lucene最初是由Doug Cutting开发的,2000年3月,发布第一个版本,是一个全文检索引擎的架构,提供了完整的查询引擎和索引引擎 ;Lucene得名于Doug妻子的中名,同时这也她外祖母的姓;目前是Apache基金会的一个顶级项目,同时也是学习搜索引擎入门必知必会。
Lucene 是一个 JAVA 搜索类库,它本身并不是一个完整的解决方案,需要额外的开发工作。
优点:成熟的解决方案,有很多的成功案例。apache 顶级项目,正在持续快速的进步。庞大而活跃的开发社区,大量的开发人员。它只是一个类库,有足够的定制和优化空间:经过简单定制,就可以满足绝大部分常见的需求;经过优化,可以支持 10亿+ 量级的搜索。
缺点:需要额外的开发工作。所有的扩展,分布式,可靠性等都需要自己实现;非实时,从建索引到可以搜索中间有一个时间延迟,而当前的“近实时”(Lucene Near Real Time search)搜索方案的可扩展性有待进一步完善。
java数据库索引,对于全文检索一般都由以下3个部分组成:
索引部分
分词部分
搜索部分
在接下来的一系列文章中会详细介绍这三个部分,本文将简单介绍lucene环境搭建以及lucene索引初步。
目前基于Lucene的产品有:
lucene索引、Solr,Nutch,Hbase,Katta,constellio,Summa,Compass,Bobo Search,Index Tank,Elastic Search,Hadoop contrib/index ,LinkedIn ,Eclipse,Cocoon
二、Lucene环境搭建
目录最新版的Lucene为4.10.0(今天是2014-09-22 )版,其官方主页为:http://lucene.apache.org/
如果你会使用Maven,那么可以非常简单的将pom.xml中加入以下内容即可:
org.apache.lucene
lucene-core
文本搜索引擎。4.10.0
org.apache.lucene
lucene-analyzers-common
4.10.0
org.apache.lucene
lucene-queryparser
索引的格式、4.10.0
如果不会使用Maven,则需要手工下载相应的jar包进行开发。
三、索引创建
1、创建Directory
2、创建IndexWriter
3、创建Document对象
java字符串索引、4、为Docuemnt添加Field
5、通过IndexWriter添加文档到Document
packagecom.amos.lucene;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.document.Document;importorg.apache.lucene.document.Field;importorg.apache.lucene.document.FieldType;importorg.apache.lucene.document.TextField;importorg.apache.lucene.index.IndexWriter;importorg.apache.lucene.index.IndexWriterConfig;importorg.apache.lucene.index.IndexableField;importorg.apache.lucene.index.IndexableFieldType;importorg.apache.lucene.store.FSDirectory;importorg.apache.lucene.store.RAMDirectory;importorg.apache.lucene.util.Version;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;/*** Created by amosli on 14-9-17.*/
public classHelloLucene {static String indexDir = "/home/amosli/developtest/lucene";public voidindex() {
IndexWriter indexWriter= null;
FSDirectory directory= null;try{//1、创建Directory
java获取list索引位置?directory = FSDirectory.open(newFile(indexDir));//RAMDirectory directory = new RAMDirectory();//2、创建IndexWriter
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_0, newStandardAnalyzer(Version.LUCENE_4_10_0));
indexWriter= newIndexWriter(directory, indexWriterConfig);
File file= new File("/home/amosli/developtest/testfile");for(File f : file.listFiles()) {
FieldType fieldType= newFieldType();//3、创建Docuemnt对象
Document document = newDocument();//4、为Document添加Field
java索引、document.add(new TextField("content", newFileReader(f)) );
fieldType.setIndexed(true);
fieldType.setStored(true);
document.add(new Field("name", f.getName(),fieldType));
fieldType.setIndexed(false);
fieldType.setStored(true);
java搜索引擎?document.add(new Field("path", f.getAbsolutePath(), fieldType));//5、通过IndexWriter添加文档索引中
indexWriter.addDocument(document);
}
}catch(IOException e) {
e.printStackTrace();
}finally{if (indexWriter != null) {try{
lucene正向索引文档,indexWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
lucene搜索引擎博客、}
}
注:
1、这里使用的是FSDirectory,是为了方便进行测试,将生成的文件写入到本地硬盘中;
2、Document相当于数据库中的一条记录,field相当数据库中表的一列;
3、使用indexWriter当记录添加到文档索引中;
java数组索引怎么获取、4、fieldType可以设置是否需要索引和是否需要存储;
5、记得关闭indexWriter
生成的索引文件,如下图所示:
四、搜索记录
1、创建Directory
2、创建IndexReader
javaweb搜索功能实现,3、根据IndexReader创建IndexSearcher
4、创建搜索的Query
5、根据Searcher搜索并且返回TopDocs
6、根据TopDocs获取ScoreDoc对象
7、根据Seacher和ScoreDoc对象获取具体的Document对象
8、根据Document对象获取需要的值
public voidsearch() {
IndexReader indexReader= null;try{//1、创建Directory
FSDirectory directory = FSDirectory.open(newFile(indexDir));//2、创建IndexReader
indexReader =DirectoryReader.open(directory);//3、根据IndexReader创建IndexSearcher
IndexSearcher indexSearcher = newIndexSearcher(indexReader);//4、创建搜索的Query//创建querypaser来确定要搜索文件的内容,第二个参数表示搜索的域
QueryParser queryParser = new QueryParser("content", newStandardAnalyzer());//创建query,表示搜索域为content中包含java的文档
Query query = queryParser.parse("java");//5、根据Searcher搜索并且返回TopDocs
TopDocs topDocs = indexSearcher.search(query, 100);//6、根据TopDocs获取ScoreDoc对象
ScoreDoc[] sds =topDocs.scoreDocs;//7、根据Seacher和ScoreDoc对象获取具体的Document对象
for(ScoreDoc sdc : sds) {
Document doc=indexSearcher.doc(sdc.doc);//8、根据Document对象获取需要的值
System.out.println("name:" + doc.get("name") + "-----> path:" + doc.get("path"));
}
}catch(IOException e) {
e.printStackTrace();
}catch(ParseException e) {
e.printStackTrace();
}finally{if(indexReader!=null){try{
indexReader.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
输出结果:
所有源码:HelloLucene.java
packagecom.amos.lucene;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.document.Document;importorg.apache.lucene.document.Field;importorg.apache.lucene.document.FieldType;importorg.apache.lucene.document.TextField;import org.apache.lucene.index.*;importorg.apache.lucene.queryparser.classic.ParseException;importorg.apache.lucene.queryparser.classic.QueryParser;importorg.apache.lucene.search.IndexSearcher;importorg.apache.lucene.search.Query;importorg.apache.lucene.search.ScoreDoc;importorg.apache.lucene.search.TopDocs;importorg.apache.lucene.store.FSDirectory;importorg.apache.lucene.store.RAMDirectory;importorg.apache.lucene.util.Version;importjava.io.File;importjava.io.FileReader;importjava.io.IOException;/*** Created by amosli on 14-9-17.*/
public classHelloLucene {static String indexDir = "/home/amosli/developtest/lucene";public voidindex() {
IndexWriter indexWriter= null;
FSDirectory directory= null;try{//1、创建Directory
directory = FSDirectory.open(newFile(indexDir));//RAMDirectory directory = new RAMDirectory();//2、创建IndexWriter
IndexWriterConfig indexWriterConfig = new IndexWriterConfig(Version.LUCENE_4_10_0, newStandardAnalyzer(Version.LUCENE_4_10_0));
indexWriter= newIndexWriter(directory, indexWriterConfig);
File file= new File("/home/amosli/developtest/testfile");for(File f : file.listFiles()) {
FieldType fieldType= newFieldType();//3、创建Docuemnt对象
Document document = newDocument();//4、为Document添加Field
document.add(new TextField("content", newFileReader(f)));
fieldType.setIndexed(true);
fieldType.setStored(true);
document.add(new Field("name", f.getName(), fieldType));
fieldType.setIndexed(false);
fieldType.setStored(true);
document.add(new Field("path", f.getAbsolutePath(), fieldType));//5、通过IndexWriter添加文档索引中
indexWriter.addDocument(document);
}
}catch(IOException e) {
e.printStackTrace();
}finally{if (indexWriter != null) {try{
indexWriter.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}public voidsearch() {
IndexReader indexReader= null;try{//1、创建Directory
FSDirectory directory = FSDirectory.open(newFile(indexDir));//2、创建IndexReader
indexReader =DirectoryReader.open(directory);//3、根据IndexReader创建IndexSearcher
IndexSearcher indexSearcher = newIndexSearcher(indexReader);//4、创建搜索的Query//创建querypaser来确定要搜索文件的内容,第二个参数表示搜索的域
QueryParser queryParser = new QueryParser("content", newStandardAnalyzer());//创建query,表示搜索域为content中包含java的文档
Query query = queryParser.parse("java");//5、根据Searcher搜索并且返回TopDocs
TopDocs topDocs = indexSearcher.search(query, 100);//6、根据TopDocs获取ScoreDoc对象
ScoreDoc[] sds =topDocs.scoreDocs;//7、根据Seacher和ScoreDoc对象获取具体的Document对象
for(ScoreDoc sdc : sds) {
Document doc=indexSearcher.doc(sdc.doc);//8、根据Document对象获取需要的值
System.out.println("name:" + doc.get("name") + "-----> path:" + doc.get("path"));
}
}catch(IOException e) {
e.printStackTrace();
}catch(ParseException e) {
e.printStackTrace();
}finally{if(indexReader!=null){try{
indexReader.close();
}catch(IOException e) {
e.printStackTrace();
}
}
}
}
}
View Code
TestHelloLucene.java
packagecom.amos.lucene;importorg.apache.lucene.analysis.standard.StandardAnalyzer;importorg.apache.lucene.queryparser.classic.QueryParser;importorg.junit.Test;/*** Created by amosli on 14-9-17.*/
public classTestHelloLucene {
@Testpublic voidtestIndex(){
HelloLucene helloLucene= newHelloLucene();
helloLucene.index();
}
@Testpublic voidtestSearch(){
HelloLucene helloLucene= newHelloLucene();
helloLucene.search();
}
}
View Code
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态