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. c# CTS 基础数据类型笔记

    C#中的基础数据类型并没有内置于c#语言中,而内置于.net freamework. C#有15个预定义类型,其中13个是值类型,两个是引用类型(string和object) 一.值类型 值类型 数据 ...

  2. 007_苹果Mac系统锁屏不待机效果设置方法介绍

    Mac如何设置锁屏不断网?Mac如何设置锁屏不待机?这是一个非常麻烦的设置,有时候一锁屏幕电脑就跟着待机了,这非常的麻烦,所以今天小编就用图文教程的方式教大家Mac如何设置锁屏不断网Mac如何设置锁屏 ...

  3. 【前端开发】限制input输入保留两位小数

    <input type="text" name='amount' id="cash_num" placeholder="请输入金额" ...

  4. Spring MVC参数注入注意事项

    1.类参数名不能出现‘name’ 2.需提供默认的无参构造

  5. Matlab读取txt中用空格分隔的数据文件到矩阵

    转载...哪儿 忘记了 由于要做的项目中涉及到数据处理,初涉及到matlab.今天需要把一组只用空格分开的数据读取到一个三维矩阵,然后对这个矩阵进行处理. 思路是:首先用importdata读入txt ...

  6. JMeter接口&性能测试

    JMeter接口测试 目前最新版本发展到5.0版本,需要Java7以上版本环境,下载解压目录后,进入\apache-jmeter-5.0\bin\,双击ApacheJMeter.jar文件启动JMem ...

  7. Java OOM学习

    转载自原文: 什么是java OOM?如何分析及解决oom问题? 什么是OOM? OOM,全称"Out Of Memory",翻译成中文就是"内存用完了",表现 ...

  8. (六)MyBatis杂项

    第一节:处理CLOB.BLOB类型数据 第二节:传入多个输入参数 第三节:MyBatis分页 1,逻辑分页 2,物理分页 MyBatis默认情况下,MyBatis启用一级缓存,即同一个SqlSessi ...

  9. Elasticsearch doc_value认识

    一.doc_value是什么 绝大多数的fields在默认情况下是indexed,因此字段数据是可被搜索的.倒排索引中按照一定顺序存放着terms供搜索,当命中搜索时,返回包含term的documen ...

  10. 关于更新SQLserver统计信息的存储过程

    1.建立t_mon_table_stat 用于存过需要更新统计信息的表 2.查找需要更新统计信息的表 insert into t_mon_table_stat SELECT DISTINCT SP.r ...