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 ="权声明:本文为博主原创文章,未经博主允许不得转载。
" ;
String str1= "作为一个合格的程序员,往往是能够很好的管理自己的程序的,尤其是对于长期处于纯研发中心,甚至软件园的程序员朋友们,无论是上班还是下班,甚至课外活动,碰到的都是具有相同思维的人群,进而容易觉得好像大部分人都是相同的思维方式。因而当程序员成为管理者后,往往习惯于将程序员当做程序一样进行管理。
" +
"
" + "然而程序员不是程序,是有血有肉的人,所以也呈现出很多与程序不同";
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();
}
}