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_索引(域)的查询的更多相关文章

  1. MySQL索引和优化查询

    索引和优化查询 恰当的索引可以加快查询速度,可以分为四种类型:主键.唯一索引.全文索引.普通索引. 主键:唯一且没有null值. create table pk_test(f1 int not nul ...

  2. MySQL 千万 级数据量根据(索引)优化 查询 速度

    一.索引的作用 索引通俗来讲就相当于书的目录,当我们根据条件查询的时候,没有索引,便需要全表扫描,数据量少还可以,一旦数据量超过百万甚至千万,一条查询sql执行往往需要几十秒甚至更多,5秒以上就已经让 ...

  3. mysql索引原理及查询速度优化

    一 介绍 为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂的查询操作,因此对查询语句 ...

  4. Lucene7.1.0版本的索引创建与查询以及维护,包括新版本的一些新特性探索!

    一 吐槽 lucene版本更新实在太快了,往往旧版本都还没学会,新的就出来,而且每个版本改动都特别大,尤其是4.7,6,6,7.1.......ε=(´ο`*)))唉,但不可否认,新版本确实要比旧版本 ...

  5. MySQL学习-MySQL内置功能_索引与慢查询

    1.索引基础 1.1 介绍 (1.)为何要有索引? 一般的应用系统,读写比例在10:1左右,而且插入操作和一般的更新操作很少出现性能问题,在生产环境中,我们遇到最多的,也是最容易出问题的,还是一些复杂 ...

  6. SQL Server索引视图以(物化视图)及索引视图与查询重写

    本位出处:http://www.cnblogs.com/wy123/p/6041122.html 经常听Oracle的同学说起来物化视图,物化视图的作用之一就是可以实现查询重写,听起来有一种高大上的感 ...

  7. Atitit 如何利用先有索引项进行查询性能优化

    Atitit 如何利用先有索引项进行查询性能优化 1.1. 再分析的话就是我们所写的查询条件,其实大部分情况也无非以下几种:1 1.2. 范围查找 动态索引查找1 1.2.1. 索引联合 所谓的索引联 ...

  8. MS SQL SERVER索引优化相关查询

        查找缺失索引 -- =============================================   -- Description: 查询当前数据库中缺失的索引,知道你进行优化的 ...

  9. Entity Framework Code First+SQL Server,改变聚集索引,提高查询性能

    .net Entity Framework(调研的是Entity Framework 4.0) code first方式生成数据库时,不能修改数据库表的索引,而SQLServer默认会把数据表的主键设 ...

随机推荐

  1. 移动web特殊样式处理

    一.高清图片 二.一像素边框 还有一种解决办法: border-1px($color) position: relative &:after display: block position: ...

  2. 你真的了解js伪数组吗?深入js伪数组

    关于js伪数组 具有length属性: 按索引方式存储数据: 不具有数组的push().pop()等方法: 你可能知道怎么把伪数组转换为数组,但是你知道这里边的原理吗? 假如页面有一组li元素 < ...

  3. R语言学习笔记:choose、factorial、combn排列组合函数

    一.总结 组合数:choose(n,k) —— 从n个中选出k个 阶乘:factorial(k) —— k! 排列数:choose(n,k) * factorial(k) 幂:^ 余数:%% 整数商: ...

  4. CCF CSP 201612-3 权限查询

    CCF计算机职业资格认证考试题解系列文章为meelo原创,请务必以链接形式注明本文地址 CCF CSP 201612-3 权限查询 问题描述 授权 (authorization) 是各类业务系统不可缺 ...

  5. 【洛谷】P4207 [NOI2005]月下柠檬树

    题解 原来自适应simpson积分是个很简单的东西! 我们尝试分析一下影子,圆的投影还是圆,圆锥的尖投影成一个点,而圆台的棱是圆的公切线,我们把圆心投影出来,发现平面上圆心的距离是两两高度差/tan( ...

  6. 8-12 Erratic Expansion uva12627

    题意:一开始有一个红气球  每小时后一个红气球会变成三个红气球和一个蓝气球  第k小时 a到b行之间有几个红气球 递归找规律题目 一定要注意涉及指数的时候一定要开long long 数组!!!! #i ...

  7. nginx配置web服务器

    一:设置虚拟服务器 1.设置 http { server { listen 127.0.0.1:8080; server_name example.org www.example.org; } } 2 ...

  8. 变量覆盖漏洞学习及在webshell中的运用

    一.发生条件: 函数使用不当($$.extract().parse_str().import_request_variables()等) 开启全局变量 二.基础了解: 1.$$定义 $$代表可变变量, ...

  9. 自定义JSP标签示例

    我们以一个例子来讲解如何自定义JSP标签,假如我们需要在页面中输出当前的时间,按照最简单的JSP脚本,需要在JSP里面写很多Java代码,那么如何来使用自定义标签实现这个功能呢? 首先,我们要先创建一 ...

  10. 四、django rest_framework源码之频率控制剖析

    1 绪言 权限判定之后的下一个环节是访问频率控制,本篇我们分析访问频率控制部分源码. 2 源码分析 访问频率控制在dispatch方法中的initial方法调用check_throttles方法开始. ...