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();
}
}

最新文章

  1. 安装KVM及虚拟机
  2. python——有一种线程池叫做自己写的线程池
  3. BZOJ4107 : [Wf2015]Asteroids
  4. 在Android开发中使用Ant 一:环境的搭建及入门
  5. ubuntu16.04下安装openssh-server报依赖错误的解决方法
  6. 分享一组Rpg Marker人物行走,游戏素材图片,共20张图片
  7. 【转】学习JAVA的步骤
  8. 问题-某个程序改了ICO图标后编译后还是显示老图标?
  9. c# linq的一些运用
  10. C#高级编程随笔
  11. 关于Mybaits映射一点心得
  12. printf不定参数
  13. ThinkingInJava 学习 之 0000002 操作符
  14. java动手动脑3
  15. 吴裕雄 python深度学习与实践(5)
  16. 【源码阅读】VS调试mimikatz-改造法国神器mimikatz执行就获取明文密码
  17. Java中判断String不为空的问题性能比较
  18. MINA2.0用户手册中文版
  19. PyMysql复习
  20. ubuntu被delete的文件位置

热门文章

  1. 12 TCP服务器 进程 线程 非阻塞
  2. jetbraints激活码
  3. mysql ON DUPLICATE KEY UPDATE、REPLACE INTO
  4. 【APUE】Chapter15 Interprocess Communication
  5. Spring常用注解用法总结
  6. 安装mathtype出问题卸载后 office2016打开mathtype弹错误窗口
  7. JavaSE复习(三)异常与多线程
  8. 软工实践Beta冲刺(5/7)
  9. Mac下离线安装SDK
  10. 【Linux】如何设置Linux开机 ,默认进入图形界面或命令行界面?