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中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...
随机推荐
- [SCOI2005]骑士精神
题目描述 输入输出格式 输入格式: 第一行有一个正整数T(T<=10),表示一共有N组数据.接下来有T个5×5的矩阵,0表示白色骑士,1表示黑色骑士,*表示空位.两组数据之间没有空行. 输出格式 ...
- [AHOI2009]中国象棋
题目描述 这次小可可想解决的难题和中国象棋有关,在一个N行M列的棋盘上,让你放若干个炮(可以是0个),使得没有一个炮可以攻击到另一个炮,请问有多少种放置方法.大家肯定很清楚,在中国象棋中炮的行走方式是 ...
- [BZOJ]4650: [Noi2016]优秀的拆分
Time Limit: 30 Sec Memory Limit: 512 MB Description 如果一个字符串可以被拆分为 AABBAABB 的形式,其中 AA 和 BB 是任意非空字符串, ...
- 【bzoj4443 scoi2015】小凸玩矩阵
题目描述 小凸和小方是好朋友,小方给了小凸一个 nn × mm (n \leq m)(n≤m) 的矩阵 AA ,并且要求小凸从矩阵中选出 nn 个数,其中任意两个数都不能在同一行或者同一列.现在小凸想 ...
- SPOJ 7258 Lexicographical Substring Search
Little Daniel loves to play with strings! He always finds different ways to have fun with strings! K ...
- [HEOI 2016] seq
题解: 发现多决策且明显无后效性,果断dp,那么转移方程F[i]=F[j]+1 设R[I]为改变之后的最大值,L[i]为改变之后的最小值 由于只能改变一个元素 所以转移的条件是 (j<i &am ...
- Codeforces Round #430 A. Kirill And The Game
Kirill plays a new computer game. He came to the potion store where he can buy any potion. Each poti ...
- Delphi7 ADO面板上的控件简介
? ADO Connection的主要方法:1) Begin Trans 开始启动一个新的事务,必须保证数据连接处于激活状态.2) Cancel 关闭于数据库的连接.3) Commit T ...
- python之with的使用
python之with使用 with工作原理 紧跟with后面的语句被求值后,返回对象的__e ...
- 数据结构之Treap
1. 概述 同splay tree一样,treap也是一个平衡二叉树,不过Treap会记录一个额外的数据,即优先级.Treap在以关键码构成二叉搜索树的同时,还按优先级来满足堆的性质.因而,Treap ...