Lucene 4.7 BooleanQuery学习

首先,官方API这样解释BooleanQuery:
A Query that maches documents matching boolean combinations of other queries, e.g. TermQuerys, PhraseQuerys or other BooleanQuerys.

我们可以理解为BooleanQuery是一个可以组合其他单个Query对象或者嵌套由多个单个Query组成的BooleanQuery对象。说白了,就是lucene靠它来提供复合查询


下面让我们具体看下它有哪些属性:

private ArrayList< BooleaClause > clauses;

这就是放多个Query的地方啦,我们可以用过add方法往这里面添加查询对象,那这里可能会有一个疑问,像mysql里面多个条件之间是通过 and 或者 or的关系组成起来的,那lucene里面是怎么实现的呢。这就得靠BooleanClause中的Occur枚举对象啦。

Occur枚举总共有三种类型,分别是:
/*Use this opertor for clauses that must appear in the matching documnts/
MUST

/* Use this operator for clauses that should appear in the matching documents. For a BooleanQuery with no MUST clauses one or more SHOULD clauses must match a document for the BooleanQuery to match. /
SHOULD

/* Use this operatr fo clauses that must not appear in the matching documents. Note that it is not possible to search for queries that only consist of a MUST_NOT clause. /
MUST_NOT

从官方给出的描述来看:
MUST相当于and, 就是查询的内容一定要出现在document中。
而SHOULD相当于or,在没有MUST的前提下,文档中至少要出现一个或者多个Occur为SHOULD的 BooleanClause对象,并且,这里要指出的是,我们可以通过setMinimunNumberShouldMatch方法去设置至少要匹配到的条件为SHOULD的个数。
最后MUST_NOT则相当于对某一个条件取非。应该是用来对文档中出现某个值进行过滤。这里要注意,如果整个BooleanQuery对象只有一个Occur为MUST_NOT的查询条件,将得不到想要的结果。

还有两个属性,稍微记一下。
private static int maxClauseCount = 1024;
这个属性是用来限制最多条件数目的,当超出这个值时,会抛出一个TooManyClauses异常

private final boolean disableCoord;
这个属性看官方的文档,意思应该是用来设置是否开启得分排序。默认构造函数为false,实际运用中还没有遇到过。

暂时先记这么多,理解到此了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值