最近在学Lucene,参考书籍为《Lucene in action 中文版》,这里的代码例子也是参考里面的【有些出入,不过不少很大】,欢迎各位大神们拍砖;至于一些介绍什么的大家可以参考一下前面说的参考书;程序用到的包我是在官网那里下的,也把他的参考文档下了【英文版】,看文档给写代码提供了不少帮助;
1、Lucene重要的类:
(1)Document;(2)Field;(3)Store;(4)Index;
(5)IndexWriter;(6)Directory;(7)IndexReader;(8)Query;等
2、向索引添加文档;添加文档的方法有两个:
(1)addDocument(Document);(2)addDocument(Document,Analyzer);
3、更新索引中的文档:
IndexReader提供了两个方法用于更新索引中的文档:
(1)updateDocument(Term,Document);
(2)updateDocument(Term,Document,Analyzer);
4、删除索引中的文档
5、下面是示例代码【人比较懒只写了添加和更新两个操作】:
package com.tan.code; import java.io.File; import java.io.IOException; import org.apache.lucene.analysis.cjk.CJKAnalyzer; 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.index.Term; import org.apache.lucene.search.IndexSearcher; import org.apache.lucene.search.Query; import org.apache.lucene.search.ScoreDoc; import org.apache.lucene.search.TermQuery; import org.apache.lucene.search.TopDocs; import org.apache.lucene.store.Directory; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.store.SimpleFSDirectory; import org.apache.lucene.util.Version; public class IndexingTest { // 準備數據 protected String[] ids = { "1", "2" }; protected String[] unindexed = { "Netherlands", "Italy" }; protected String[] unstored = { "Amsterdam has lots of bridges", "Venice has lots of cancals" }; protected String[] text = { "Amsterda", "Venice" }; private String indexpath = "C:/index"; File file = new File(indexpath); private Directory directory; @SuppressWarnings("deprecation") public void setUp() throws IOException { directory = new SimpleFSDirectory(file); // 索引位置在内存 // directory = new RAMDirectory(); // 创建IndexWriter对象 IndexWriter indexWriter = getIndexWriter(); for (int i = 0; i < ids.length; i++) { Document document = new Document(); document.add(new Field("id", ids[i], Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("country", unindexed[i], Field.Store.NO, Field.Index.NOT_ANALYZED)); document.add(new Field("contents", unstored[i], Field.Store.NO, Field.Index.NOT_ANALYZED)); document.add(new Field("city", text[i], Field.Store.YES, Field.Index.NOT_ANALYZED)); indexWriter.addDocument(document); } // IndexReader[] ir = { IndexReader.open(directory) }; // indexWriter.addIndexes(ir); // indexWriter.optimize(); // indexWriter.addIndexes(); indexWriter.close(); } private IndexWriter getIndexWriter() throws IOException { return new IndexWriter(directory, new IndexWriterConfig( Version.LUCENE_44, new CJKAnalyzer(Version.LUCENE_44))); // return new IndexWriter(directory,new WhitespaceAnalyzer(), // IndexWriter.MaxFieldLength.UNLIMITED); } public int getHitsCount(String filedname, String searchString) throws IOException { directory = new SimpleFSDirectory(file); IndexReader indexReader = IndexReader.open(directory); // IndexSearcher searcher = new IndexSearcher(directory); IndexSearcher indexSearcher = new IndexSearcher(indexReader); Term term = new Term(filedname, searchString); // 建立Term查尋 Query query = new TermQuery(term); TopDocs topDocs = indexSearcher.search(query, null, 10); ScoreDoc[] scoreDocs = topDocs.scoreDocs; return scoreDocs.length; } @SuppressWarnings("deprecation") public void updateIndex() throws IOException { directory = new SimpleFSDirectory(file); IndexWriter indexWriter = getIndexWriter(); Document document = new Document(); document.add(new Field("id", "1", Field.Store.YES, Field.Index.NOT_ANALYZED)); document.add(new Field("country", "China", Field.Store.NO, Field.Index.NOT_ANALYZED)); document.add(new Field("contents", "Beijing has lots of people", Field.Store.NO, Field.Index.NOT_ANALYZED)); document.add(new Field("city", "Beijing", Field.Store.YES, Field.Index.NOT_ANALYZED)); indexWriter.updateDocument(new Term("id", "1"), document); indexWriter.close(); } }
6、测试代码:
package com.tan.test; import static org.junit.Assert.*; import java.io.IOException; import org.junit.Test; import com.tan.code.IndexingTest; public class MyTest { @Test public void test() throws IOException { //fail("Not yet implemented"); IndexingTest indexingTest=new IndexingTest(); //indexingTest.setUp(); //System.out.println(indexingTest.getHitsCount("city", "Amsterda")); //updatetest indexingTest.updateIndex(); //确认旧文档已经删除 assertEquals(0, indexingTest.getHitsCount("city", "Amsterdam")); //确认新文档被索引 assertEquals(1, indexingTest.getHitsCount("city", "Beijing")); } }
7、源代码已经上传到了我的资源里面,有兴趣可以去下载,下载链接:http://download.csdn.net/detail/mr_tank_/6017929
【敬请批评指正】
相关推荐
Lucene索引的基本操作,包括添加和更新操作及测试代码;
IndexWriter :提供对索引的写入操作 Directory:描述了索引存放的位置 Analyzer:对文本进行分析,提取词汇(token),剔除无用的信息 Document:虚拟的文档 Field:每个Document包含一个或多个不同命名的Field,每...
本文主要是研究了全文检索技术的基本原理以及Lucene的架构和工作原理,并介绍了基于Lucene的实时全文检索引擎的设计实现过程。并提供了一个基于Web的简单实现。最后通过实验的方式,对实现的实时全文检索引擎的性能...
Lucene读取文件,读取数据,创建索引及搜索索引操作,包含数据库。项目是基本Springboot+MyBatis实现。
分析(Analysis),在Lucene当中指的是将域(Field)文本转换为最基本的索引表示单元——项(Term)的过程。在搜索过程中,这些项用于决定什么样的文档能匹配查询条件。例如,如果这句话“For example, if this ...
es也是用Java开发并使用Lucene作为其核心来实现所有索引和搜索功能,但是它的目的是通过简单的RESTful API来隐藏Lucene的复杂性,从而让全文搜索变得简单。es基本是开箱即用(解压就可以用!),非常简单。Solr安装...
分片是底层的基本读写单元,分片的目的是分割巨大索引,让读写可以并行操作,分片是数据的容器,文档保存在分片内,不会跨分片存储。分片又被分配到集群内的各个节点里。当集群规模扩大或缩小时,ES 会自动在各节点...
Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎,无论在开源还是专有领域,Lucene可以被认为是...本文涉及Elasticsearch的一些基本概念以及操作。还重点总结了安装过程中遇到的一些问题以及解决方案。
该项目基于 Lucene.Net,旨在为标准 Sitecore 索引中使用的 Lucene.Net 提供流畅的风格 API。 其主要目的是使 Lucene 不那么冗长,以完成大多数任务,同时保留 Lucene 的功能和速度。 它的设计目标是驱动现有的 ...
操作系统 OS ( winxp 或 win2000 ) 编译程序 javac , eclipse(IDE) 测试支持软件 java , eclipse(IDE) 2.3基本设计概念和处理流程 总体框架如下所示: 2.4人工处理过程 如通过启动spider搜索的网页,及启动index进行...
3.5.1 基本索引操作 36 3.5.2 批量索引操作 37 3.6 如何进行搜索 39 3.6.1 搜索语法 39 3.6.2 排序 42 3.6.3 字段增加权重 42 3.6.4 Solr分词器、过滤器、分析器 42 3.6.5 Solr高亮使用 46 4 SolrJ的用法 46 4.1 ...
要查看索引操作,请查看TestIndexing.java和Shakespeare.java 。目标全局搜索大量数据。表现更短的查询导致更快的响应。 也就是说*foo*总是比*foosterific*返回得更快。Java使用 Java 7。除了一些涉及 java.util....
Elasticsearch+Kibana基本应用.Elasticsearch(ES)是一个基于Lucene构建的开源、分布式、RESTful风格接口的全文索引引擎。它还是一个分布式文档数据库,其中每个字段均是被索引且可以被搜索,容易扩展和量级数据处理...
2008年5月份,因学习《信息检索》课程,本人利用java的开源搜索引擎库lucene,以及结合ajax技术google-suggest功能,模仿baidu、google的基本风格做了个“Baioogle-SearchEngine(百歌搜索引擎)”程序。 (源代码见...
ElasticSearch是一个基于Lucene的搜索服务器。它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口。Elasticsearch是用Java开发的,并作为Apache许可条款下的开放源码发布,是当前流行的企业级搜索...
在文本框中输入要搜索的关键字,然后单击“搜一下”按钮后,系统将根据索引文件的内容自动进行搜索,将搜索的结果在页面中显示出来,并且将显示的结果中将要搜索的内容进行描红,Lucene搜索引擎运行效果如图1.3所示...
Solr是一个独立的企业级搜索应用服务器,它对外提供类似于Web-service的API接口。用户可以通过http请求,向搜索引擎服务器提交一定格式的XML文件,生成索引...其中Lucene全文检索的基本原理,跟大牛讲的web搜索课程里的
1,新增后台参数设置功能,无需修改页面完成Title等基本设置信息; 2,新增首次使用前台的时候提示,提升操作体验; 3,新增后台操作指引,方便首次使用用户; 4,修复首次使用搜索类型读取索引记录数异常; 5,后台...
基本概念 索引创建与删除 文档简单的CURD操作 深入了解 自定义配置 集群内的原理 映射和分析 排序与相关性 实际使用中的Q&A 检索包含中文的关键字时返回结果不准确 中文分词器 字典 中文参数预处理 索引重建 Reindex...