16、遍历所有关键字的api方式
来源:互联网 发布:飞鱼网络电视官网 编辑:程序博客网 时间:2024/05/06 03:51
之前的一些讲述中,我们介绍了查询索引的功能,但一般都是通过域和关键字进行的查询,那如果我们不知道域或想要查询出所有的关键字怎么办呢,下面我就给出两个解决方案,代码很简单
通过指定域方式获取该域中所有的关键字
//遍历指定域中的所有关键字的方式@Testpublic void searchIndex() throws Exception { //打开索引库 //指定索引库存放的位置 Directory directory = FSDirectory.open(new File("D:\\LucentTest\\luceneIndex2")); //创建一个IndexReader对象 IndexReader indexReader = DirectoryReader.open(directory); //创建TermsEnum对象 所有关键字存放的类 TermsEnum enumeration = null; IndexSearcher searcher = new IndexSearcher(indexReader); IndexReader reader = searcher.getIndexReader();//得到搜索器的索引阅读器 Fields fields = MultiFields.getFields(reader); Terms terms = fields.terms("name"); BytesRef byteRef = null; enumeration = terms.iterator(enumeration); while((byteRef = enumeration.next()) != null) { String term = new String(byteRef.bytes, byteRef.offset, byteRef.length); System.out.println("term is : " + term); }}
只有索引目录获取所有域所有关键字的方法
//获取全部关键字的方法@Testpublic void sqlIndex() throws Exception{ //打开索引库 //指定索引库存放的位置 Directory indexDirectory = FSDirectory.open(new File("D:\\LucentTest\\luceneIndex2")); //创建一个IndexReader对象 用来打开索引 IndexReader indexReader = DirectoryReader.open(indexDirectory); //取出所有域的信息 Fields fields = MultiFields.getFields(indexReader); //对索引域进行遍历 Iterator<String> fieldsIterator = fields.iterator(); while(fieldsIterator.hasNext()){ String field = fieldsIterator.next(); Terms terms = fields.terms(field); TermsEnum termsEnums = terms.iterator(null); BytesRef byteRef = null; System.out.println("field : "+ field); while((byteRef = termsEnums.next()) != null) { String term = new String(byteRef.bytes, byteRef.offset, byteRef.length); System.out.println("term is : " + term); } }}
其实在这两个方法中,我们要关心的就是遍历的部分,及 MultiFields.getFields(indexReader);和TermsEnum这个类的用法, TermsEnum 是存放所有索引关键字抽象类,看代码我们就能看的出来。
1 0
- 16、遍历所有关键字的api方式
- LinkedList 的所有遍历方式
- jQuery 对 json对象的所有的遍历方式
- 使用递归遍历的方式生成所有的九宫格数独
- 遍历数组的所有元素的三种方式
- Winform遍历窗口的所有控件(两种方式)
- java所有的关键字
- win32 api遍历目录下所有文件
- 非递归方式遍历所有组合
- JEB2 script 中通过api获取指定类的TypeHierarchy以遍历其所有派生类
- 递归调用的方式遍历SD卡上的所有图片
- 递归调用的方式遍历SD卡上的所有图片
- java中所有的关键字
- python 查看所有的关键字
- 递归方式遍历文件夹下的所有文件及子文件夹 [php]
- C#以排列组合中的“组合”方式遍历某个文件夹下的所有文件
- 遍历父物体下所有子物体的几种方式
- 遍历父物体下所有子物体的几种方式
- 消除iOS中多余的警告
- MySQL事务隔离级别详解
- 随笔——共同富裕
- pdo是如何防止 sql注入的
- php 两种方法获取url内容 file_get_contents和curl
- 16、遍历所有关键字的api方式
- 【cpp】 函数如何返回数组
- PHP+Ajax点击加载更多内容 -这个效果好,速度快,只能点击更多加载,不能滚动自动加载
- 20161228:for pyhton网络数据采集02
- Java 与 Arduino uno 使用 RXTX串口通信
- freemaker介绍及常见用法
- Eclipse+Maven创建webapp项目
- 51nod 1255 字典序最小的子序列 神奇的栈用法
- PL/SQL--变量