4.Lucene3.案例介绍,创建索引,查询等操作验证
案例:
|
Article.java |
|
package cn.toto.lucene.quickstart; publicclassArticle privateintid; private private /** * */ publicint returnid; } /** * */ publicvoid this.id } /** * */ public returntitle; } /** * */ publicvoid this.title } /** * */ public returncontent; } /** * */ publicvoid this.content } } |
|
工具类Configuration.java |
|
package cn.toto.lucene.util; import java.io.File; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.analysis.standard.StandardAnalyzer; import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.Version; /** * @brief Configuration.java配置对象,提供Lucene需要 索引目录,分词器 * @attention * @author toto * @date 2014-12-7 * @note begin modify by涂作权 */ public class Configuration { private static Directory directory; private static Analyzer analyzer; private static Version version; static { try { //设置磁盘目录,表示的是本地index目录 directory = FSDirectory.open(new File("index")); } catch (Exception e) { e.printStackTrace(); } //表示LUCENE版本 version = Version.LUCENE_36; //表示使用版本 analyzer = new StandardAnalyzer(version); } //提供目录 public static Directory getDirectory() { return directory; } //提供分词器 public static Analyzer getAnalyzer() { return analyzer; } //获取版本 public static Version getVersion() { return version; } } |
|
工具类LuceneUtils.java |
|
package cn.toto.lucene.util; import java.io.IOException; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.search.IndexSearcher; // lucene工具类 public class LuceneUtils { private static IndexWriter indexWriter; static { // try { // IndexWriterConfig indexWriterConfig = new IndexWriterConfig( Configuration.getVersion(), Configuration.getAnalyzer()); indexWriter = new IndexWriter(Configuration.getDirectory(), indexWriterConfig); // Runtime.getRuntime().addShutdownHook(new Thread() { @Override public void run() { try { indexWriter.close(); } catch (CorruptIndexException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }); } catch (IOException e) { e.printStackTrace(); } } // public static IndexWriter getIndexWriter() { return indexWriter; } // public static IndexSearcher getIndexSearcher() throws Exception { return new IndexSearcher(IndexReader.open(Configuration.getDirectory())); } } |
|
LuceneTest.java |
|
package cn.toto.lucene.api; import java.io.File; importjava.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.document.Field.Store; import org.apache.lucene.index.CorruptIndexException; import org.apache.lucene.index.IndexReader; import org.apache.lucene.index.IndexWriter; import org.apache.lucene.index.IndexWriterConfig; import org.apache.lucene.queryParser.QueryParser; 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.LockObtainFailedException; import org.apache.lucene.store.RAMDirectory; import org.apache.lucene.util.Version; import org.junit.Test; import cn.toto.lucene.quickstart.Article; import cn.toto.lucene.util.LuceneUtils; // API详细分析 publicclass @Test //使用LuceneUtils解决 @SuppressWarnings("unused") publicvoid IndexWriter indexWriter2 = LuceneUtils.getIndexWriter(); IndexWriter indexWriter1 = LuceneUtils.getIndexWriter(); } @Test @SuppressWarnings("all") //使用两个IndexWrtier报错,锁使用问题 publicvoid LockObtainFailedException, //索引目录位置 Directory directory = FSDirectory.open(new //分词器 Analyzer analyzer = //写入索引 IndexWriterConfig indexWriterConfig = Version.LUCENE_36, IndexWriter indexWriter = IndexWriterConfig indexWriterConfig2 = Version.LUCENE_36, IndexWriter indexWriter2 = indexWriterConfig2); } 上面的运行结果如下: @Test //测试Store和 /* * Store.YES * Index.NOT_ANALYZED * Index.NOT_ANALYZED_NO_NORMS */ publicvoid //需要建立索引目标数据 Article article = article.setId(100); article.setTitle("学习全文检索"); article.setContent("lucene是搜索引擎开发技术,lucene并不是一个现成的产品,由Apache提供"); //将索引数据转换 Document document = document.add(new Field.Index.NOT_ANALYZED));//对于id通常不分词 document.add(new Field.Index.ANALYZED_NO_NORMS)); document.add(new Field.Index.ANALYZED)); //建立索引库 //索引目录位置 Directory directory = FSDirectory.open(new //分词器 Analyzer analyzer = //写入索引 IndexWriterConfig indexWriterConfig = IndexWriter indexWriter = //将document数据写入索引库 indexWriter.addDocument(document); indexWriter.close(); } 上面的单元测试的结果 @Test //查询索引库 publicvoid //建立Query对象 String queryStrng = Analyzer analyzer = QueryParser queryParser = analyzer); Query query = queryParser.parse(queryStrng); //根据Query查找 Directory directory = FSDirectory.open(new IndexSearcher indexSearcher = IndexReader.open(directory)); //执行查询获得满足结果前多少条记录 TopDocs topDocs = indexSearcher.search(query, 100);//查询满足结果前100条数据 System.out.println("满足结果记录条数:" //获得每个结果 ScoreDoc[] scoreDocs = topDocs.scoreDocs; for //打印得分 System.out.println("得分:" //获得Document下标 int Document document = indexSearcher.doc(docID); System.out.println("id:" System.out.println("title:" System.out.println("content:" } indexSearcher.close(); } 上面的运行单元测试之后的结果如下: @SuppressWarnings("unused") @Test //测试路径写法 publicvoid //磁盘路径 FSDirectory.open(new FSDirectory.open(new //类路径 FSDirectory.open(new //内存路径 Directory directory = } } |
4.Lucene3.案例介绍,创建索引,查询等操作验证的更多相关文章
- java mongodb 基础系列---查询,排序,limit,$in,$or,输出为list,创建索引,$ne 非操作
官方api教程:http://docs.mongodb.org/ecosystem/tutorial/getting-started-with-java-driver/#getting-started ...
- 关于怎么C#控制台窗口中怎么创建连接查询数据库操作
首先需要新建一张表,为了测试随建了一张学生表 新建号一张表之后就可以对数据库进行操作了 列举了常用的增删改查 操作 static void Main(string[] args) { s ...
- Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!
一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...
- MySQL索引详解(优缺点,何时需要/不需要创建索引,索引及sql语句的优化)
一.什么是索引? 索引是对数据库表中的一列或多列值进行排序的一种结构,使用索引可以快速访问数据库表中的特定信息. 二.索引的作用? 索引相当于图书上的目录,可以根据目录上的页码快速找到所需的内容,提 ...
- Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程
2.Lucene3.6.2包介绍,第一个Lucene案例介绍,查看索引信息的工具lukeall介绍,Luke查看的索引库内容,索引查找过程 2014-12-07 23:39 2623人阅读 评论(0) ...
- SQL Server 查询性能优化——创建索引原则(二)
三:索引的建立原则 一般来说,建立索引要看数据使用的场景,换句话来说哪些访问数据的SQL语句是常用的,而这些语句是否因为缺少索引(也有可能是索引过多)变的效率低下.但绝不是所有的SQL语句都要建立索引 ...
- SQL Server 查询性能优化——创建索引原则
索引是什么?索引是提高查询性能的一个重要工具,索引就是把查询语句所需要的少量数据添加到索引分页中,这样访问数据时只要访问少数索引的分页就可以.但是索引对于提高查询性能也不是万能的,也不是建立越多的索引 ...
- mysql索引原理及创建与查询
索引介绍 一:为什么要有索引 索引是用来优化查询效率(速度)的 没有索引的话,对于大数据的表,就只能每次都遍历一遍,数据量越大,耗时越多有索引的话,可以提升好几个数量级的速度 一般的应用系统,读写比例 ...
- ArcGIS Engine 创建索引(属性索引)——提高查询效率
转自原文 ArcGIS Engine 创建索引(属性索引)——提高查询效率 众所周知,建立索引可以提高查询的效率,当对FeatureClass中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...
随机推荐
- [LCA模版] Distance Queries
题目描述 约翰的奶牛们拒绝跑他的马拉松,因为她们悠闲的生活不能承受他选择的长长的赛道.因此他决心找一条更合理的赛道.此题的输入于第一题相同,紧接着下一行输入一个整数K,以后K行为K个"距离问 ...
- bzoj4514
4514: [Sdoi2016]数字配对 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1870 Solved: 712[Submit][Statu ...
- C++内存机制中内存溢出、内存泄露、内存越界和栈溢出的区别和联系
当我们在用C++做底层驱动的时候,经常会遇到内存不足的警告,究其原因,往往是因为内存出现溢出,泄露或者越界等原因.那么他们之间有什么联系吗? 内存溢出(out of memory) 是指程序在申请内存 ...
- Mysql--执行计划 Explain
0 介绍 0.1 是什么 使用 Explain 关键字可以模拟优化器执行 Sql 查询语句,从而知道 Mysql 是如何处理 Sql 的. 0.2 用法 Explain + Sql语句 0.3 执行计 ...
- 自然语言处理工具:中文 word2vec 开源项目,教程,数据集
word2vec word2vec/glove/swivel binary file on chinese corpus word2vec: https://code.google.com/p/wor ...
- 腾讯北京SNG一面
写在前面 面试官超nice,以一种聊天的形式跟你交流.上午10点10左右开始,11点40结束.总的来说,基础还可以,但是有些东西的底层学的还是不够深. 问题回忆 自我介绍 怎么平衡科研与项目开发之间的 ...
- SQL之排序
1.按多个列排序 经常需要按不止一个列进行数据排序.例如,如果要显示雇员名单,可能希望按姓和名排序(首先按姓排序,然后在每个姓中再按名排序).如果多个雇员有相同的姓,这样做很有用. 要按多个列排序,简 ...
- 70. Climbing Stairs(easy, 号称 Dynamic Programming 天下第一题)
You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...
- Ajax相关——get请求和post请求的区别
一.完整的URL由以下几部分组成: scheme:通信协议,常用的有:http/ftp. host:主机,服务器(计算机)域名或IP地址 port:端口,整数,可选,省略时使用默认端口,http的默认 ...
- CentOS 7下GitLab搭建及配置
由于公司业务,需要上Git版本控制. * 目前市面上比较有名的Git服务提供商,国外有GitHub.BitBucket.GitLab,国内有码云,Coding. * 现有的服务商,对于免费的套餐都有一 ...