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.背景 目前跟信息采集相关的一个项目提出了这样的一个需求:中国银行等 ...
随机推荐
- 前端nginx+Java后台ftp处理页面图片上传踩坑
今天,将前端代码部署到服务器nginx上,在测试多图片上传时,报错413请求体空间太大,请求都没到后台,直接被nginx拦截,调整后又报错504. 整体而言,前端存在两处问题: 413 错误 :Req ...
- veu——引入iconfont图标
我这里是阿里的iconfont图标,如何下载请看下面这个博文 https://www.cnblogs.com/wangyang0210/articles/9248324.html 创建文件夹 在ass ...
- 图灵机器人API接口
调用图灵API接口实现人机交互 流程一: 注册 图灵机器人官网: http://www.tuling123.com/ 第一步: 先注册, 然后创建机器人, 拿到一个32位的key 编码方式 UTF-8 ...
- Codeforces543 B. Destroying Roads
传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, ...
- Git的搭建
Git的搭建 第1步:官网下载安装git 第2步:github官网注册账号 第3步:配置git 第4步:github这是秘钥 第5步:上传本地工程到git 主要参考的博客(这三篇博客能让你顺利上传至g ...
- npm 淘宝源
--------- npm: 淘宝源设置:npm config set registry https://registry.npm.taobao.org
- Leetcode 27.移除元素 By Python
给定一个数组 nums 和一个值 val,你需要原地移除所有数值等于 val 的元素,返回移除后数组的新长度. 不要使用额外的数组空间,你必须在原地修改输入数组并在使用 O(1) 额外空间的条件下完成 ...
- Android性能优化案例研究
译 者前言: 这是Google的Android开发工程师Romain Guy刊登在个人Blog上的一篇文章.Romain Guy 作为Android图形渲染和系统优化的专家,是Android 4.1中 ...
- 单片机的基本构成、工作原理 LET′S TRY“嵌入式编程”: 1 of 6
单片机的基本构成.工作原理 LET′S TRY“嵌入式编程”: 1 of 6 本连载讲解作为嵌入式系统开发技术人员所必需具备的基础知识.这些基础知识是硬件和软件技术人员都应该掌握的共通技术知识.有了电 ...
- servlet 上下文
一.应用需求: 如何统计网站在线人数? 使用ServletContext. 二.ServletContext详解: 1.是不同于session和cookie,是可以让所有客户端共同访问的内容,是在服务 ...