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中的某一列频繁的查找,且数据量比较大时,建立索引是非常有必 ...
随机推荐
- [BZOJ]2589: Spoj 10707 Count on a tree II
Time Limit: 20 Sec Memory Limit: 400 MB Description 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v),你需要回答u xor last ...
- [Russian Code Cup 2017 - Finals [Unofficial Mirror]]简要题解
来自FallDream的博客,未经允许,请勿转载,谢谢. Div1难度+ACM赛制 和几个大佬组队逛了逛 A.给一个大小为n的集合ai(1<=ai<=1000000),要求你构造一个大小 ...
- hdu5601 BestCoder Round #67 (div.2)
N*M bulbs Accepts: 94 Submissions: 717 Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 655 ...
- mooc-python语言语法week3-6
week3 1.类型的概念:程序编程不允许有歧义的数据类型存在,所以对数据进行了划分,python语言类型分为,数字类型.字符串类型.元组类型.列表类型.文件类型.字典类型. i:数字类型: pyth ...
- python中str常用操作
1. 字符串的操作 字符串的连接操作 符号: + 格式:str1 + str2 例如:str1 = 'I Love' str2 = 'You!' print(str1 + str2) >> ...
- 关于 "java中常量定义在interface中好还是定义在class中好" 的一些思考
原文链接 http://blog.csdn.net/voo00oov/article/details/50433672 java中interface中定义变量都是"public static ...
- eclipse中安装freemarker插件及ftl使用freemarker编辑器
http://www.07net01.com/2015/08/895212.html eclipse中安装freemarker插件及ftl使用freemarker编辑器 在线安装的方法是:Help – ...
- Linux 查看CPU温度
安装 lm-sensors sudo apt-get install lm-sensors # 安装yes | sudo sensors-detect # 侦测所有感测器 sensors # 查看温度 ...
- VS2010 win7 64位安装后新建项目生成时错误:LINK : fatal error LNK1123: 转换到 COFF 期间失败: 文件无效或损坏
解决方案:VS2010在经历一些更新后,建立Win32 Console Project时会出“error LNK1123” 错误,解决方案为将 项目|项目属性|配置属性|清单工具|输入和输出|嵌入清单 ...
- FJUT第四周寒假作业之第一集,临时特工?(深度优先搜索)
原网址:http://210.34.193.66:8080/vj/Contest.jsp?cid=163#P2 第一集,临时特工? TimeLimit:1000MS MemoryLimit:128M ...