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查询的更多相关文章

  1. javaEE中错误提示 Exception starting filter BackServletFilter java.lang.ClassNotFoundException: tmall.filter.BackServletFilter提示这个错误啊

    最近在学习javaEE的部署,不借助eclipse中的部署方式,而是通过修改server.xml文件的方式部署 添加Context路径 <Context path="/tm" ...

  2. Bloom Filter 算法简介 (增加 Counting Bloom Filter 内容)

    Bloom Filter的中文翻译叫做布隆过滤器,是1970年由布隆提出的.它实际上是一个很长的二进制向量和一系列随机映射函数.布隆过滤器可以用于检索一个元素是否在一个集合中.它的优点是空间效率和查询 ...

  3. java web filter 学习(2)

    本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...

  4. asp.net MVC之 自定义过滤器(Filter) - shuaixf

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration :缓存的时间, 以 ...

  5. asp.net MVC之 自定义过滤器(Filter)

    一.系统过滤器使用说明 1.OutputCache过滤器 OutputCache过滤器用于缓存你查询结果,这样可以提高用户体验,也可以减少查询次数.它有以下属性: Duration:缓存的时间,以秒为 ...

  6. Servlet的生命周期及filter,servletRequest和servletResponse

    序,Web应用中,Servlet和Filter是很重要的两个概念,一定要理解透彻. 一.Servlet类 继承自HttpServlet,HttpServlet是一个抽象类,主要包含的方法有init,s ...

  7. java web filter 之一 基础实现

    本文主要对filter的基本使用进行了讲解,其中涉及到了 filter是什么 一个filter处理一个jsp 多个filter处理一个jsp filter是什么 Filter 是java下的一种过滤器 ...

  8. Java中Filter、Servlet、Listener的学习

    1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个reques ...

  9. 转 Java中Filter、Servlet、Listener的学习

      1.Filter的功能filter功能,它使用户可以改变一个 request和修改一个response. Filter 不是一个servlet,它不能产生一个response,它能够在一个requ ...

随机推荐

  1. git初始化仓库相关

    当我们需要新建一个git项目会遇到的问题 全局设置 git config --global user.name "名字" git config --global user.emai ...

  2. 1826: [JSOI2010]缓存交换

    1826: [JSOI2010]缓存交换 https://www.lydsy.com/JudgeOnline/problem.php?id=1826 分析: 简单的贪心,然后调啊调...最近怎么了,码 ...

  3. C# List集合去重操作注意点

    今天调试代码时发现list的distinct方法在对引用类型操作时并没有去重,后来查阅资料发现list去重操作对象集合时比较的是对象的一个个引用地址, 因为集合里的对象都是一个个单独的实例,所以并不会 ...

  4. spring 给静态变量注入值

    一般在spring中,给static变量加上@Autowired注解的时候会报空指针异常错误. 解决: 1.通过xml配置文件配置 这个就不多说了. 2.通过注解 @Component public ...

  5. Qt QML之不显示标题栏、边框

    原文连接:http://blog.csdn.net/u010780613 我使用的Qt版本是Qt 5.3.0,Qt Creator 是3.1.1. QML做界面实在太方便了,动画效果很不错. 创建一个 ...

  6. HDFS伪分布式

    (一).HDFS shell操作 以上已经介绍了如何搭建伪分布式的Hadoop,既然环境已经搭建起来了,那要怎么去操作呢?这就是本节将要介绍的内容: HDFS自带有一些shell命令,通过这些命令我们 ...

  7. Linux开发C语言规范

    -Iinclude:找头文件目录 ,获取头文件的目录 -C:创建.o文件 .h:文件用来声明函数,即写一个函数名. 如 int add(); int div(); int mul(); .c:文件定义 ...

  8. 第一课:SVN代码管理

    SVN:是一个跨平台的开源的版本控制系统.svn版本管理工具管理着随时间改变的各种数据.这些数据放置在一个中央资料档案库中.svn会备份并记录每个文件每次的修改更新变动.svn的工作流程:1.在中央库 ...

  9. lintcode-128-哈希函数

    128-哈希函数 在数据结构中,哈希函数是用来将一个字符串(或任何其他类型)转化为小于哈希表大小且大于等于零的整数.一个好的哈希函数可以尽可能少地产生冲突.一种广泛使用的哈希函数算法是使用数值33,假 ...

  10. intelliJ idea 使用技巧&方法

    导入的项目查看svn地址:在项目上右键--subversion--relocate可以看到以前对应的svn地址. 重要的几个快捷键的使用方式: (1)       alt+insert 成员变量封装 ...