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中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...
随机推荐
- BZOJ4423 Bytehattan
Description 比特哈顿镇有n*n个格点,形成了一个网格图.一开始整张图是完整的. 有k次操作,每次会删掉图中的一条边(u,v),你需要回答在删除这条边之后u和v是否仍然连通. Input 第 ...
- 【BZOJ3233】【tyvj1729】文艺平衡树
原题传送门 解题思路:裸平衡树操作,支持区间翻转即可,这里写了无旋treap. 其实平衡树的区间操作就和线段树差不多,你用个标记搞一下就好了,,,,, #include <stdio.h> ...
- bzoj 4919: [Lydsy六月月赛]大根堆
Description 给定一棵n个节点的有根树,编号依次为1到n,其中1号点为根节点.每个点有一个权值v_i. 你需要将这棵树转化成一个大根堆.确切地说,你需要选择尽可能多的节点,满足大根堆的性质: ...
- 计蒜客NOIP2017提高组模拟赛(三)day1
火山喷发 火山喷发对所有附近的生物具有毁灭性的影响.在本题中,我们希望用数值来模拟这一过程. 在环境里有 n 个生物分别具有 A1,A2,⋯,An点生命值,一次火山喷发总计 MM 轮 ...
- bzoj2005 NOI2010 方案统计
2005: [Noi2010]能量采集 Time Limit: 10 Sec Memory Limit: 552 MBSubmit: 4387 Solved: 2619[Submit][Statu ...
- 对中断的理解handle_level_irq【原创】
如下为本人原创,在解决问题的过程中的一点心得,如果有描述不准确的地方还请各位指出,非常感谢 Linux内核版本:linux-4.9.18 曾有一次调试触摸屏的时候遇到如下的问题 /startup/mo ...
- StarSpace是用于高效学习实体向量的通用神经模型
StarSpace是用于高效学习实体向量的通用神经模型,用于解决各种各样的问题: 学习单词,句子或文档级嵌入. 文本分类或任何其他标签任务. 信息检索:实体/文件或对象集合的排序,例如 排名网络文件. ...
- tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例
tf.nn.embedding_lookup TensorFlow embedding_lookup 函数最简单实例 #!/usr/bin/env python # -*- coding: utf-8 ...
- 消息中间件--ActiveMQ&JMS消息服务
### 消息中间件 ### ---------- **消息中间件** 1. 消息中间件的概述 2. 消息中间件的应用场景 * 异步处理 * 应用解耦 * 流量削峰 * 消息通信 --------- ...
- south 命令学习
south 命令学习 概述 在django某个版本之前,django自身提供一个创建数据库的命令-syncdb,它会根据model来创建相应的表,但是这个命令不好的地方在于,如果想要对model进行更 ...