Lucene的其他搜索(三)
生成索引:
package com.wp.search; import java.nio.file.Paths; 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.IntField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; public class Indexer { private Integer ids[] = { 1, 2, 3 };
private String citys[] = { "aingdao", "banjing", "changhai" };
private String descs[] = { "Qingdao is b beautiful city.",
"Nanjing is c city of culture.", "Shanghai is d dustling dity." };
// 这里的descs中的Shanghai is d dustling dity这句中我让他们不出现b和c,为等下搜索准备 private Directory dir; /**
* 获取IndexWriter实例
*
* @return
* @throws Exception
*/
private IndexWriter getWriter() throws Exception {
Analyzer analyzer = new StandardAnalyzer(); // 标准分词器
IndexWriterConfig iwc = new IndexWriterConfig(analyzer);// 为索引配置分词器
IndexWriter writer = new IndexWriter(dir, iwc);
return writer;
} /**
* 生成索引
*
* @param indexDir
* @throws Exception
*/
private void index(String indexDir) throws Exception {
dir = FSDirectory.open(Paths.get(indexDir));
IndexWriter writer = getWriter();
for (int i = 0; i < ids.length; i++) {
Document doc = new Document();
doc.add(new IntField("id", ids[i], Field.Store.YES));
doc.add(new StringField("city", citys[i], Field.Store.YES));
doc.add(new TextField("desc", descs[i], Field.Store.YES));
writer.addDocument(doc); // 添加文档
}
writer.close();
} public static void main(String[] args) throws Exception {
new Indexer().index("D:\\lucene\\luceneIndex");
} }
其他搜索:
package com.wp.search; import java.nio.file.Paths; import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.BooleanClause;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.PrefixQuery;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermRangeQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.BytesRef;
import org.junit.After;
import org.junit.Before;
import org.junit.Test; public class SearchTest { private Directory dir;
private IndexReader reader;
private IndexSearcher is; @Before
public void setUp() throws Exception {
dir = FSDirectory.open(Paths.get("D:\\lucene\\luceneIndex"));// FSDirectory为专门处理目录文件的一个类
reader = DirectoryReader.open(dir);
is = new IndexSearcher(reader);
} @After
public void tearDown() throws Exception {
reader.close();
} /**
* 指定项范围搜索 之前我将Shanghai is d dustling dity不出现b和c的原因,更好看结果
*
* @throws Exception
*/
@Test
public void testTermRangeQuery() throws Exception {
TermRangeQuery query = new TermRangeQuery("desc", new BytesRef("b"
.getBytes()), new BytesRef("c".getBytes()), true, true);// 查询满足包含b和c区间的结果
TopDocs hits = is.search(query, 10);// 显示查询结果的最前10条数据
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);// 将查到的内容放在文档中
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
} /**
* 指定数字范围
*
* @throws Exception
*/
@Test
public void testNumericRangeQuery() throws Exception {
NumericRangeQuery<Integer> query = NumericRangeQuery.newIntRange("id",
1, 3, true, true);// 查询在id包含1到3以内的结果(包括2)
TopDocs hits = is.search(query, 10);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
} /**
* 指定字符串开头搜索
*
* @throws Exception
*/
@Test
public void testPrefixQuery() throws Exception {
PrefixQuery query = new PrefixQuery(new Term("city", "a"));// 查询城市以a开头的
TopDocs hits = is.search(query, 10);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
} /**
* 多条件查询
*
* @throws Exception
*/
@Test
public void testBooleanQuery() throws Exception {
NumericRangeQuery<Integer> query1 = NumericRangeQuery.newIntRange("id",
1, 2, true, true);// 查询id在1和2之间的
PrefixQuery query2 = new PrefixQuery(new Term("city", "a"));// 城市以a开头的
BooleanQuery.Builder booleanQuery = new BooleanQuery.Builder();
// booleanQuery.add(query1, BooleanClause.Occur.MUST_NOT);//
// MUST_NOT表示除了
// booleanQuery.add(query1, BooleanClause.Occur.SHOULD);// SHOULD表示或者
booleanQuery.add(query1, BooleanClause.Occur.MUST);// MUST表示并且
booleanQuery.add(query2, BooleanClause.Occur.MUST);
TopDocs hits = is.search(booleanQuery.build(), 10);
for (ScoreDoc scoreDoc : hits.scoreDocs) {
Document doc = is.doc(scoreDoc.doc);
System.out.println(doc.get("id"));
System.out.println(doc.get("city"));
System.out.println(doc.get("desc"));
}
}
}
Lucene的其他搜索(三)的更多相关文章
- Apache Solr采用Java开发、基于Lucene的全文搜索服务器
http://docs.spring.io/spring-data/solr/ 首先介绍一下solr: Apache Solr (读音: SOLer) 是一个开源.高性能.采用Java开发.基于Luc ...
- 基于 Lucene 的桌面文件搜索
开源2010年,自己在学习 Lucene 时开发的一款桌面文件搜索工具,这么多年过去了,代码一直静静存放在自己的硬盘上,与其让其沉睡,不如分享出来. 这款工具带有明显的模仿 Everything 的痕 ...
- Apache Lucene(全文检索引擎)—搜索
目录 返回目录:http://www.cnblogs.com/hanyinglong/p/5464604.html 本项目Demo已上传GitHub,欢迎大家fork下载学习:https://gith ...
- lucene学习笔记:三,Lucene的索引文件格式
Lucene的索引里面存了些什么,如何存放的,也即Lucene的索引文件格式,是读懂Lucene源代码的一把钥匙. 当我们真正进入到Lucene源代码之中的时候,我们会发现: Lucene的索引过程, ...
- lucene的多种搜索2-SpanQuery
SpanQuery按照词在文章中的距离或者查询几个相邻词的查询 SpanQuery包括以下几种: SpanTermQuery:词距查询的基础,结果和TermQuery相似,只不过是增加了查询结果中单词 ...
- 如何使用 Lucene 做网站高亮搜索功能?
现在基本上所有网站都支持搜索功能,现在搜索的工具有很多,比如Solr.Elasticsearch,它们都是基于 Lucene 实现的,各有各的使用场景.Lucene 比较灵活,中小型项目中使用的比较多 ...
- 一种安全云存储方案设计(下)——基于Lucene的云端搜索与密文基础上的模糊查询
一种安全的云存储方案设计(未完整理中) 一篇老文了,现在看看错漏颇多,提到的一些技术已经跟不上了.仅对部分内容重新做了一些修正,增加了一些机器学习的内容,然并卵. 这几年来,云产品层出不穷,但其安全性 ...
- Lucene建立索引搜索入门实例
第一部分:Lucene建立索引 Lucene建立索引主要有以下两步:第一步:建立索引器第二步:添加索引文件准备在f盘建立lucene文件夹,然后 ...
- WebGIS中解决使用Lucene进行兴趣点搜索排序的两种思路
文章版权由作者李晓晖和博客园共有,若转载请于明显处标明出处:http://www.cnblogs.com/naaoveGIS/. 1.背景 目前跟信息采集相关的一个项目提出了这样的一个需求:中国银行等 ...
随机推荐
- sql查询表中重复数据个数
select train_code,count(1) from tb_ask_trainbodyroadtrain group by train_code having count(1) >1
- vim指令
编辑-->输入: i: 在当前光标所在字符的前面,转为输入模式: a: 在当前光标所在字符的后面,转为输入模式: o: 在当前光标所在行的下方,新建一行,并转为输入模式: I:在当前光标所在行的 ...
- iframe与src一个性质 当js中修改了src的值后会重新向后台发送请求 ;为了防止浏览器缓存问题 当我们修改src时候 需要添加不同的值 这样浏览器就不会从缓存中取值 而是重新发起后台请求
- Power Spectral Density
对于一个特定的信号来说,有时域与频域两个表达形式,时域表现的是信号随时间的变化,频域表现的是信号在不同频率上的分量.在信号处理中,通常会对信号进行傅里叶变换得到该信号的频域表示,从而得到信号在频域上的 ...
- MVC 动态菜单
直接上代码: 一,创建菜单 Action public ActionResult GetMenu() { //获取菜单 List<MenuItem> mainMenu = mm.GetMe ...
- 大学jsp实验3include指令的使用
1.include指令的使用 (1)编写一个名为includeCopyRight.jsp的页面,页面的浏览效果如下: 要求“2016”这个值可以实现动态更新.请写出页面代码: <%@ page ...
- Flask 构建微电影视频网站(三)
搭建后台页面 视图函数位于admin文件夹下, app/admin/views.py 管理员登录页面搭建 视图函数 @admin.route('/') def index(): return '后台主 ...
- bzoj 2653: middle (主席树+二分)
2653: middle Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 2522 Solved: 1434[Submit][Status][Disc ...
- PHP linux ZendGuardLoader.so: undefined symbol: executor_globals
/usr/xxx/php xxx/xxx.php 报了这个错. 本人出现此问题的原因: php执行程序路径错了. 解决: linux下执行 which php 命令 查看php真实路 ...
- 【HDU-6148】 Valley Numer(数位dp)
百度之星2017复赛1005 HDU-6148 Valley Numer 题意 不出现上升后直接下降数位的数,不超过n的有几个.前导零不算. 题解 dfs(当前数位的位置len,这位的数num,是否在 ...