Lucene_索引(域)的查询
package cn.tz.lucene; import java.io.File; import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.queryparser.classic.MultiFieldQueryParser;
import org.apache.lucene.queryparser.classic.QueryParser;
import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.NumericRangeQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.ScoreDoc;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TopDocs;
import org.apache.lucene.search.BooleanClause.Occur;
import org.apache.lucene.store.FSDirectory;
import org.apache.lucene.util.Version;
import org.junit.Test;
import org.wltea.analyzer.lucene.IKAnalyzer; public class IndexSearchTest { @Test
public void testIndexSearch() throws Exception{
//创建分词器
//Analyzer analyzer=new StandardAnalyzer();
Analyzer analyzer=new IKAnalyzer();
FSDirectory directory=FSDirectory.open(new File("d:\\lucene"));
//创建索引和文档的读对象
IndexReader reader=IndexReader.open(directory);
//创建索引的搜索对象
IndexSearcher indexSearcher=new IndexSearcher(reader);
//创建查询对象
//第一个参数:默认搜索域,没有指定搜索域时才使用的
QueryParser queryParser= new QueryParser("fileName",analyzer);
//格式: 域名:搜索关键词
//Query query = queryParser.parse("fileName:apache");
Query query = queryParser.parse("fileName:not exit");
//第一个参数:查询语句对象 第二个参数:显示的数据条数
TopDocs topDocs = indexSearcher.search(query,5); System.out.println("***** 一共有"+topDocs.totalHits+" 条记录 *****");
//从搜索的结果中获取结果集
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
//获取文档id
int docId = scoreDoc.doc; //通过文档id从硬盘中读取对应得文件
Document doc = reader.document(docId);
System.out.println("fileName:"+doc.get("fileName"));
System.out.println("fileSize:"+doc.get("fileSize"));
System.out.println("==================================");
}
reader.close();
} /**
* 使用TermQuery不需要分词器:它是分词后进行查询
* @throws Exception
*/
@Test
public void testTermQuery() throws Exception{
Analyzer analyzer=new IKAnalyzer();
FSDirectory dir=FSDirectory.open(new File("d:\\lucene"));
//读对象
IndexReader reader=IndexReader.open(dir); //查询对象
Term term=new Term("fileName","apache");
Query query=new TermQuery(term);
//搜索对象
IndexSearcher searcher=new IndexSearcher(reader);
TopDocs topDocs = searcher.search(query, 10);
System.out.println("总条数: "+topDocs.totalHits);
//从查询结果中获取结果集
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
//获取文档ID
int docID = scoreDoc.doc;
//根据文档ID获取文档
Document document = reader.document(docID);
System.out.println("文件名: "+document.get("fileName"));
System.out.println("文件大小 : "+document.get("fileSize"));
System.out.println("======================================");
}
reader.close();
}
/**
* NumericRangeQuery:
* 用于数字范围的查询
* 注意:只针对数字类型的Field域才可以进行检索
* 例如:LongFeild,FloatFeild...
* @throws Exception
*/
@Test
public void testNumericRangeQuery() throws Exception{
Analyzer analyzer=new IKAnalyzer();
//数据源
FSDirectory dir=FSDirectory.open(new File("d:\\lucene"));
IndexReader reader=IndexReader.open(dir);
IndexSearcher search=new IndexSearcher(reader);
//创建query对象
//参数:域名 最小值 最大值 是否包含最小值 是否包含最大值
NumericRangeQuery query=NumericRangeQuery.newLongRange("fileSize",100L,1000L,true,true); TopDocs topDocs = search.search(query, 10);
System.out.println("文件数量: "+topDocs.totalHits);
//将查询结果转为结果集
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
//获取文档ID
int docID = scoreDoc.doc;
//根据文档ID获取文档
Document doc = reader.document(docID);
System.out.println("文件名称: "+doc.get("fileName"));
System.out.println("文件大小: "+doc.get("fileSize"));
System.out.println("=========================");
}
reader.close();
} /**
* BooleanQuery:用于多个条件(组合)查询
*
*/
@Test
public void testBooleanQuery() throws Exception{
FSDirectory dir=FSDirectory.open(new File("d:\\lucene"));
IndexReader reader=IndexReader.open(dir);
IndexSearcher searcher=new IndexSearcher(reader); TermQuery termQuery=new TermQuery(new Term("fileName","apache"));
NumericRangeQuery numericRangeQuery=NumericRangeQuery.newLongRange("fileSize",100L,1000L,true,true);
BooleanQuery booleanQuery=new BooleanQuery();
//Occur:
//MUST:and
//MUST_NOT:not
//Should:or
//查询文件名字包含有apache,文件大小在100-1000bit之内的
booleanQuery.add(termQuery, Occur.MUST);
booleanQuery.add(numericRangeQuery,Occur.MUST);
TopDocs topDocs=searcher.search(booleanQuery, 10); System.out.println("文件数量 : "+topDocs.totalHits);
ScoreDoc[] scoreDocs = topDocs.scoreDocs;
for(ScoreDoc scoreDoc:scoreDocs){
int docId = scoreDoc.doc;
Document document = reader.document(docId);
System.out.println("文件名称: "+document.get("fileName"));
System.out.println("文件大小: "+document.get("fileSize"));
System.out.println("=============================="); }
} /**
* MultiFieldQueryParser:从多个域进行查询
*
*/
@Test
public void testMultiFieldQueryParser() throws Exception{
Analyzer analyzer=new IKAnalyzer();
FSDirectory directory=FSDirectory.open(new File("d:\\lucene"));
IndexReader reader=IndexReader.open(directory);
IndexSearcher searcher=new IndexSearcher(reader);
//需求:查询文件名称和文件内容中包含有"apache"的内容
//从fileName、fileContent域中进行查询
String[] fields={"fileName","fileContent"};
MultiFieldQueryParser multiQueryParser=new MultiFieldQueryParser(fields, analyzer);
Query query = multiQueryParser.parse("apache");
TopDocs topDocs=searcher.search(query, 5);
System.out.println("总记录数: "+topDocs.totalHits);
//根据查询结果返回结果集,并遍历
for(ScoreDoc scoreDoc:topDocs.scoreDocs){
int docId = scoreDoc.doc;
Document doc = reader.document(docId);
System.out.println("文档名称:"+doc.get("fileName"));
System.out.println("文档大小:"+doc.get("fileSize"));
System.out.println("===============================");
}
}
}
Lucene_索引(域)的查询的更多相关文章
- MySQL索引和优化查询
索引和优化查询 恰当的索引可以加快查询速度,可以分为四种类型:主键.唯一索引.全文索引.普通索引. 主键:唯一且没有null值. create table pk_test(f1 int not nul ...
- MySQL 千万 级数据量根据(索引)优化 查询 速度
一.索引的作用 索引通俗来讲就相当于书的目录,当我们根据条件查询的时候,没有索引,便需要全表扫描,数据量少还可以,一旦数据量超过百万甚至千万,一条查询sql执行往往需要几十秒甚至更多,5秒以上就已经让 ...
- mysql索引原理及查询速度优化
一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...
- Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!
一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...
- MySQL学习-MySQL内置功能_索引与慢查询
1.索引基础 1.1 介绍 (1.)为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂 ...
- SQL Server索引视图以(物化视图)及索引视图与查询重写
本位出处:http://www.cnblogs.com/wy123/p/6041122.html 经常听Oracle的同学说起来物化视图,物化视图的作用之一就是可以实现查询重写,听起来有一种高大上的感 ...
- Atitit 如何利用先有索引项进行查询性能优化
Atitit 如何利用先有索引项进行查询性能优化 1.1. 再分析的话就是我们所写的查询条件,其实大部分情况也无非以下几种:1 1.2. 范围查找 动态索引查找1 1.2.1. 索引联合 所谓的索引联 ...
- MS SQL SERVER索引优化相关查询
查找缺失索引 -- ============================================= -- Description: 查询当前数据库中缺失的索引,知道你进行优化的 ...
- Entity Framework Code First+SQL Server,改变聚集索引,提高查询性能
.net Entity Framework(调研的是Entity Framework 4.0) code first方式生成数据库时,不能修改数据库表的索引,而SQLServer默认会把数据表的主键设 ...
随机推荐
- HTTP之二 http 301 和 302的区别
1.什么是301转向?什么是301重定向? 301转向(或叫301重定向,301跳转)是当用户或搜索引擎向网站服务器发出浏览请求时,服务器返回的HTTP数据流中头信息(header)中的状态码的一种, ...
- 3->集群架构主机克隆教程
centos7系统集群主机克隆: 有道笔记链接地址
- mybatis待研究
1. mapper 中_parameter 的含义,是 参数? 为什么?
- Vue项目实现excel导出
1.package.json里面安装三个插件 npm install xlsx --save npm install script-loader –save-dev npm install ...
- 聚类:(K-means)算法
1.归类: 聚类(clustering) 属于非监督学习 (unsupervised learning) 无类别标记(class label) 2.举例: 3. K-means 算法: ...
- 题解-python-CodeForces 1A
A. Theatre Square time limit per test 2 seconds memory limit per test 64 megabytes input standard in ...
- BFC 从了解到放弃
最近工作中我突然产生了一个想法,就如我们人类面临的终极问题一般,我从哪里来?我到哪里去?在撸代码进行CSS布局的时候,我会去想,我为什么这么做?,为什么浮动的元素要用overflow?,为什么要用cl ...
- 提高eclipse使用效率(二)—— 提高Android开发效率的小技巧
XML文件的代码提示 adt中也有xml文件的代码提示,为了让提示来的更加猛烈,我们还要设置一下 打开eclipse - Window - Preferences,在右边的目录树中切换到XML - X ...
- 反向投影(BackProjection)
如果一幅图像的区域中显示的是一种结构纹理或者一个独特的物体,那么这个区域的直方图可以看作一个概率函数,他给的是某个像素属于该纹理或物体的概率. 所谓反向投影就是首先计算某一特征的直方图模型,然后使用模 ...
- day9--paramiko模块
志不坚者智不达 paramiko:在Linux链接其他机器,每台Linux机器都有一个SSHClient:Python自己也写了一个SSHClient,那么Python写paramiko创建SSHCl ...