Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询
Lucene在6.0版本之后彻底废除了Filter的使用,采用BooleanQuery来实现Filter的功能,核心代码如下:
TermQuery termQuery = new TermQuery(new Term("content","长"));
TermQuery termQuery1 = new TermQuery(new Term("content","格"));
BooleanQuery.Builder builder = new BooleanQuery.Builder();//构造booleanQuery
builder.add(termQuery, BooleanClause.Occur.FILTER); //BooleanClause.Occur.FILTER 设置为过滤,这个termQuery包含的就相当于Filter
builder.add(termQuery1, BooleanClause.Occur.FILTER);//这个同上,也就是可以添加多个Filter
(对Lucene6之前的Filter不够熟悉的请看这个:http://www.cnblogs.com/forfuture1978/archive/2010/05/19/1738805.html)
下面给出具体代码,大家可以试一试:
package Query; import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.*;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.FSDirectory; import java.io.IOException;
import java.nio.file.Paths; /**
* Created by jet on 2017/7/31.
*/
public class BooleanQueryTest {
public static void main(String[] args) throws IOException { index();
search();
} public static void search() throws IOException {
Directory dir = FSDirectory.open(Paths.get("booleanIndex"));
IndexSearcher searcher = new IndexSearcher(DirectoryReader.open(dir)); TermQuery termQuery = new TermQuery(new Term("content","长"));
TermQuery termQuery1 = new TermQuery(new Term("content","格")); long start = System.currentTimeMillis();
//construct a boolean query to test filter function.
BooleanQuery.Builder builder = new BooleanQuery.Builder();
//booleanQuery 中使用filter查询也可以让该部分的查询结果不参与打分
builder.add(termQuery, BooleanClause.Occur.FILTER);
builder.add(termQuery1, BooleanClause.Occur.FILTER);
BooleanQuery booleanQuery = builder.build(); TopDocs hits = searcher.search(booleanQuery,10);
long end = System.currentTimeMillis();
System.out.println("totalhits: " + hits.totalHits);
System.out.println("How long it takes to search: "+(end-start));
for (ScoreDoc scoredoc:hits.scoreDocs
) {
Document doc = searcher.doc(scoredoc.doc);
System.out.println("score: "+ scoredoc.score);
System.out.println("getFields: "+doc.get("content"));
System.out.println(doc.get("FileName")); }
} public static void index() throws IOException {
String str ="权声明:本文为博主原创文章,未经博主允许不得转载。\n" ;
String str1= "作为一个合格的程序员,往往是能够很好的管理自己的程序的,尤其是对于长期处于纯研发中心,甚至软件园的程序员朋友们,无论是上班还是下班,甚至课外活动,碰到的都是具有相同思维的人群,进而容易觉得好像大部分人都是相同的思维方式。因而当程序员成为管理者后,往往习惯于将程序员当做程序一样进行管理。\n" +
"\n" + "然而程序员不是程序,是有血有肉的人,所以也呈现出很多与程序不同";
String str2="首先,程序是有确定的输入和输出的,一般对于特定的输入,一定会得到特定的输出,一般规定了输入输出,也就确定了接口,对其中的具体实现不用关心,对于成熟的公共模块,也不用担心其维护,可放心的使用,达到一劳永逸的效 ";
String str3= " 而程序员不是,不是任何对程序员的输入,都会带来特定的输出的。";
String str4= "无论是源代码维护,文档,持续集成,设计模式,架构等,很多都深入程序员的思想中,甚至成为日常生活中的一种思维方式。";
Directory dir = FSDirectory.open(Paths.get("booleanIndex"));
// Directory dir = new RAMDirectory();
StandardAnalyzer analyzer = new StandardAnalyzer();
IndexWriterConfig config = new IndexWriterConfig(analyzer);
config.setOpenMode(IndexWriterConfig.OpenMode.CREATE_OR_APPEND);
IndexWriter writer = new IndexWriter(dir,config);
Document document = new Document();
Document document1 = new Document();
Document document2 = new Document();
Document document3 = new Document();
Document document4 = new Document();
document.add(new Field("content",str, TextField.TYPE_STORED));
document1.add(new Field("content",str1, TextField.TYPE_STORED));
document2.add(new Field("content",str2, TextField.TYPE_STORED));
document3.add(new Field("content",str3, TextField.TYPE_STORED));
document4.add(new Field("content",str4, TextField.TYPE_STORED));
writer.addDocument(document);
writer.addDocument(document1);
writer.addDocument(document2);
writer.addDocument(document3);
writer.addDocument(document4);
writer.close();
}
}
Lucene6去掉了Filter但是可以用BooleanQuery实现Filter查询的更多相关文章
- javaEE中错误提示 Exception starting filter BackServletFilter java.lang.ClassNotFoundException: tmall.filter.BackServletFilter提示这个错误啊
最近在学习javaEE的部署,不借助eclipse中的部署方式,而是通过修改server.xml文件的方式部署 添加Context路径 <Context path="/tm" ...
- Bloom Filter 算法简介 (增加 Counting Bloom Filter 内容)
Bloom Filter的中文翻译叫做布隆过滤器,是1970年由布隆提出的.它实际上是一个很长的二进制向量和一系列随机映射函数.布隆过滤器可以用于检索一个元素是否在一个集合中.它的优点是空间效率和查询 ...
- java web filter 学习(2)
本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...
- asp.net MVC之 自定义过滤器(Filter) - shuaixf
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...
- asp.net MVC之 自定义过滤器(Filter)
一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...
- Servlet的生命周期及filter,servletRequest和servletResponse
序,Web应用中,Servlet和Filter是很重要的两个概念,一定要理解透彻. 一.Servlet类 继承自HttpServlet,HttpServlet是一个抽象类,主要包含的方法有init,s ...
- java web filter 之一 基础实现
本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...
- Java中Filter、Servlet、Listener的学习
1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个reques ...
- 转 Java中Filter、Servlet、Listener的学习
1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个requ ...
随机推荐
- luogu4172 [WC2006]水管局长
就是用 lct 维护最小生成树 ref #include <algorithm> #include <iostream> #include <cstdio> #in ...
- php长整型完整输出
今天调用webservice时返回一个字段是int64 长整型 原始的数值应该是 190000002101056096 而php返回时转成 1.9000000210106E+17 当传入另一个接口就报 ...
- c#根据ip获取城市地址
用的API是百度.新浪.淘宝: 1.首先是一个检测获取的值是不是中文的方法,因为有的ip只能识别出来某省,而城市名称则为空返回的json里会出现null或undefined. public stati ...
- 洛谷P2346四子连棋
题目描述 在一个4*4的棋盘上摆放了14颗棋子,其中有7颗白色棋子,7颗黑色棋子,有两个空白地带,任何一颗黑白棋子都可以向上下左右四个方向移动到相邻的空格,这叫行棋一步. 黑白双方交替走棋,任意一方可 ...
- Hessian 2.0 序列化协议 - Hessian 2.0 Serialization Protocol 翻译
Hessian是一种轻量.快速的web协议,在微服务场景下经常被使用. Hessian协议实际上包含两种含义: 1. Web网络通信远程调用服务,具体可以参考:http://hessian.cauch ...
- UVA 11882 Biggest Number(搜索+剪枝)
You have a maze with obstacles and non-zero digits in it: You can start from any square, walk in the ...
- 数据结构7——DP优化
斜率优化/单调队列优化/四边形优化
- Linux的常用目录学习笔记
首先,先查看一下Linuxi的一级目录结构: ls: /:表示根目录,文件系统的入口,最高一级目录. bin和sbin:命令保存目录,bin是普通用户能,sbin是root用户用的:/bin存放着系统 ...
- Apache服务器的Options 的 Indexes FollowSymLinks详解
禁止显示Apache目录列表 - Indexes FollowSymLinks 如何修改目录的配置以禁止显示 Apache 目录列表. 缺省情况下如果你在浏览器输入地址: http://localho ...
- javaScript运算符学习笔记
1.赋值运算符 javaScript运算符可以分为简单赋值和复合赋值运算.简单赋值运算是将赋值运算符(=)右边的表达式的值保存到赋值运算符左边的变量中,复合赋值运算则是混合了其他操作(算术运算操作,位 ...