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.背景 目前跟信息采集相关的一个项目提出了这样的一个需求:中国银行等 ...
随机推荐
- mobile deeplearning
框架: 腾讯ncnn https://github.com/Tencent/ncnn 百度mobile-deep-learning https://github.com/baidu/mobile-de ...
- 如何下载旧版本的MySQL
可能存在这样的场景,比如一些老系统需要使用MySQL 5.5版本才能运行,其余的不行. 1.登录下载站点 https://dev.mysql.com/downloads/mysql/ 此时的最新版本为 ...
- python3.7之12306抢票脚本实现
悲催的12306,彻底沦为各路抢票软件的服务提供方.元旦伊始,纯粹12306官网及APP抢票,愈一周的时间,仅到手一张凌晨3:55回家的站票.为远离脑残,无奈选择抢票软件,预购年后返沪车票.BTW,研 ...
- python optparser模块
python的内置模块中对于命令行的解析模块共两个getopt 和 optparse .不过getopt过于简单,往往不能满足需求.此时可以使用optparse模块.这个模块相对于getopt更新,功 ...
- BZOJ1014[JSOI2008]火星人——非旋转treap+二分答案+hash
题目描述 火星人最近研究了一种操作:求一个字串两个后缀的公共前缀.比方说,有这样一个字符串:madamimadam,我们将这个字符串的各个字符予以标号:序号: 1 2 3 4 5 6 7 8 9 10 ...
- HDU4864 Task(算竞进阶习题)
贪心 比较巧妙的贪心..先把所有机器和任务按时间是第一关键字,等级为第二关键字排序. 然后用机器去匹配每一个任务. 排序之后,在时间上满足当前任务的机器,必定也在时间上满足后面的机器,所以我们每次把时 ...
- python中的replace()方法的使用
python中的replace()方法的使用 需求是这样的:需要将字符串的某些字符替换成其他字符 str.replace(old,new,max) 第一个参数是要进行更换的旧字符,第二个参数是新的子串 ...
- wstngfw IKEv2服务器配置示例
wstngfw IKEv2服务器配置示例 移动客户端的服务器配置有几个组件: 为***创建一个证书结构 配置IPsec移动客户端设置 为客户端连接创建阶段1和阶段2 添加IPsec防火墙规则 创建** ...
- Linux 多网卡绑定bond
mode=0:负载均衡模式,增加带宽,两块网卡使用的是同一个MAC地址,所以必须配置网卡相连的交换机,这两个端口应采用聚合方式. mode=1:主备模式,一个线断了,另一条自动备援. mode=6:负 ...
- linux下对clamav杀毒软件的安装和配置
下载安装 首先安装zlib库: # yum install zlib zlib-devel //安装可忽略 下载安装clamav源码包 clamav管网:http://www.clamav.net/d ...