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 ...
随机推荐
- How to add a webpart to your website
I have download a webpart that can play media on the website from the internet.Then how to add ...
- jmeter结合autoit操作windows程序
需求: 模拟操作下图软件的控件,如拨号和挂机. 1. 下载安装好autoit后,打开finder tool,使用查找工具定位到要模拟操作的控件上,如图: 2.在finder tool中的control ...
- Android4.0系统以上程序不出现菜单键的问题解决
去掉targetSdkVersion 或改为targetSdkVersion =13或更小.. 不改targetSdkVersion的办法:在onCreate() 里setContentView()之 ...
- Linux系统学习笔记(1)
学习地址:http://www.runoob.com/linux/linux-tutorial.html 1.学习Windows和Linux哪个比较难? 前期是Windows容易学习,后期是Linux ...
- Vm Ubuntu 文件共享问题
其实也是差不多的,就是需要重新安装一次工具 ,自己安装有问题,自己在手动安装一次就好了 下面是一个我的参考文章 http://blog.csdn.net/zz962/article/details/7 ...
- jira+mysql+破解+中文+compose
1.制作docker-compose.yml 2.安装 $ docker stack deploy -c docker-compose.yml mshk_jira
- DFS(6)——hdu1342Lotto
一.题目回顾 题目链接:Lotto Sample Input 7 1 2 3 4 5 6 7 8 1 2 3 5 8 13 21 34 0 Sample Output 1 2 3 4 5 6 1 2 ...
- 软件工程项目组Z.XML会议记录 2013/10/22
软件工程项目组Z.XML会议记录 [例会时间]2013年10月22日星期二21:00-22:30 [例会形式]小组讨论 [例会地点]三号公寓楼会客厅 [例会主持]李孟 [会议记录]周敏轩 会议整体流程 ...
- java rmi浅谈
首先比较下RPC和RMI的差别: 首先java提供了RMI的api,jdk1.5之后虚拟机自动生成两个类:存根类stub和骨架类skelton. stub是给客户端的,当客户端调用远程对象的一个方法时 ...
- 【iOS开发】initWithNibName、initWithCoder、awakeFromNib和 loadNibNamed详解
第一.initWithNibName这个方法是在controller的类在IB中创建,但是通过Xcode实例化controller的时候用的. 第二.initWithCoder 是一个类在IB中创建但 ...