很早就对lucene这个基于java的apache开源全文搜索框架有过初步的学习了,但是由于学习等各种原因,迟迟都没有发表一些博文,今天就贴出一些博主的一些学习笔记,希望对lucene有兴趣的博友指点指点。
对于学习lucene这个全文搜索框架,第一步,要学会建立起索引:
package lucene; import java.io.File; import java.io.FileReader; 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.document.Field; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; public class Lucene { /** * 建立索引 * @throws IOException */ public void index() throws IOException { //1、创建directory Directory directory=new RAMDirectory(); //创建内存索引 Directory directory2=FSDirectory.open(new File("E:/lucene/index01")); //索引创建于硬盘中 //2、创建indexWriter //创建IndexWriterConfig,从中文名可以看出,这个是IndexWriter的配置类, 有两个参数,第一个是版本号,第二个是分词器 IndexWriterConfig iwc =new IndexWriterConfig(Version.LUCENE_43,new StandardAnalyzer(Version.LUCENE_43)); IndexWriter iw=new IndexWriter(directory2,iwc); //3、创建document对象 Document document; //4、为document对象添加Field域 //创建文件夹,文件夹里面有三个文档 File f=new File("E:/lucene/luceneexamplefile"); //遍历文件夹里面的三个文档,为每个文档添加索引域,即建立当前这篇文档的索引 for(File file:f.listFiles()) { document= new Document(); document.add(new Field("content",new FileReader(file))); document.add(new Field("filename",file.getName(),Field.Store.YES,Field.Index.NOT_ANALYZED)); document.add(new Field("filepath",file.getAbsolutePath(),Field.Store.YES,Field.Index.NOT_ANALYZED)); //5、通过indexWriter将文档添加到索引中 //因为iw是indexWriter,它里面封装了索引创建的目标地方和分词器等等的配置, //而且提供了一个封装方法,只要调用这个方法,把这篇文档传进去,即可建立起当前文档的索引 iw.addDocument(document); } //类似于数据库一样,用完要关闭 iw.close(); } }
上面代码是建立索引的步骤以及代码实现,下面写个测试类:
public class LuceneTest { @Test public void testIndex() throws IOException { Lucene lucene=new Lucene(); lucene.index(); } }
这样就可以生成索引了,执行结果如下:
就这样,索引就建立成功了。
相关推荐
Lucene的目的是为软件开发人员提供一个简单易用的工具包,以方便的在目标系统中实现全文检索的功能,或者是以此为基础建立起完整的全文检索引擎。Lucene是一套用于全文检索和搜寻的开源程式库,由Apache软件基金会...
lucene4.3增删改查的的一个工具类,对新手来说是一份不可多得的入门资料。
全文检索lucene 4.3 所用到的3个jar包,包含lucene-queryparser-4.3.0.jar、 lucene-core-4.3.0.jar、lucene-analyzers-common-4.3.0.jar。
lucene4.3 按坐标距离排序,里面写了个简单的例子。运行就行
lucene4.3源代码 censed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information ...
|---luceneIndex 索引文件夹,下面放置Lucene4.3版本的索引文件,存放了XXX条来自社交网站的“发言”数据。 |---XunTa XunTa项目源代码,可导入Eclipse(javaEE版)并运行。 |---readme.txt 您正在看的该说明...
lucene 对 xml建立索引 建立索引就是怎么简单 呵呵
使用lucene需要检索时,需要导入jar包,下载全资源文件,进去找就可以
在Eclipse环境中运用java,Lucene建索引及查询关键字
Lucene实现索引和查询的实例讲解Lucene实现索引和查询的实例讲解
本源码演示了Lucene结合Sql建立索引,把Sql中的数据通过建立索引用Lucene来检索 【该源码由51aspx提供】 源码 " onerror="this.src='/images/ifnoimg.gif'" src="/uploads/allimg/090904/1039152O5-0.jpg...
lucene 做索引查询流程,来自《lucene in action》
Lucene创建索引步骤: 1、创建Directory(索引位置) 2、创建IndexWrite(写入索引) 3、创建Document对象 4、为Document添加Field(相当于添加属性:类似于表与字段的关系) 5、通过IndexWriter添加文档到索引中
iTextPDFExtractor.java ------ ...--PDFBox创建PDF文件的Lucene索引 PDFBoxPathIndex.java ------- --PDFBox创建指定目录PDF文档索引 POIOfficeExtractor.java ----- -- POI处理Excel和Word文档代码
Lucene索引器实例Lucene索引器实例Lucene索引器实例Lucene索引器实例
基于lucene 2.4简单的一个索引和搜索实例
基于lucene的索引与搜索 基于lucene的索引与搜索
深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制深入 Lucene 索引机制
Lucene结合Sql建立索引Demo源码 Lucene(这里用到的是Lucene.net版本也成为DotLucene)是一个信息检索的函数库(Library),利用它你可以为你的应用加上索引和搜索的功能. Lucene的使用者不需要深入了解有关全文检索的...
lucene建立数据库索引的问题,在这里,把其中的一些问题补充说明,希望对大家有新的帮助,希望大家相互提出问题,相互学习,共同进步!!