gpt4 book ai didi

java - ElasticSearch 中 BoolQuery 的 "filter"的用途是什么?

转载 作者:行者123 更新时间:2023-12-02 09:44:08 43 4
gpt4 key购买 nike

我阅读了 BoolQuery 的文档根据它,这就是目的,

filter

The clause (query) must appear in matching documents. However unlike must the score of the query will be ignored. Filter clauses are executed in filter context, meaning that scoring is ignored and clauses are considered for caching.

同样来自 BoolQueryBuilder 类:

   /**
* Adds a query that <b>must</b> appear in the matching documents but will
* not contribute to scoring. No {@code null} value allowed.
*/
public BoolQueryBuilder filter(QueryBuilder queryBuilder) {
if (queryBuilder == null) {
throw new IllegalArgumentException("inner bool query clause cannot be null");
}
filterClauses.add(queryBuilder);
return this;
}

但是我无法理解这个。我什么时候应该使用过滤器 vs(应该或必须)

这是我正在处理的示例:

I want to filter out some records based on the following assumptions :

Fetch All

1) Records where deleted=0 and isPrivate=true

AND

2) Records where (isPrivate=false or [isPrivate=true and
createdBy=loggedInUser])

这是给出相同结果的 2 个查询,我想知道 filter 查询表示什么

仅使用“必须”和“应该”子句,无需过滤即可得到结果。

"query": {
"bool": {
"must": [
{
"term": {
"deleted": {
"value": "0",
"boost": 1
}
}
},
{
"match": {
"isPrivate": {
"query": true
}
}
},
{
"bool": {
"should": [
{
"term": {
"isPrivate": {
"value": "false",
"boost": 1
}
}
},
{
"bool": {
"must": [
{
"term": {
"createdBy": {
"value": "1742991596",
"boost": 1
}
}
},
{
"term": {
"isPrivate": {
"value": "true",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
]
}
}
]
}
},

使用过滤器进行查询

"query": {
"bool": {
"adjust_pure_negative": true,
"boost": 1,
"filter": [
{
"bool": {
"must": [
{
"term": {
"deleted": {
"value": "0",
"boost": 1
}
}
},
{
"match": {
"isPrivate": {
"query": true
}
}
}
],
"should": [
{
"term": {
"isPrivate": {
"value": "false",
"boost": 1
}
}
},
{
"bool": {
"must": [
{
"term": {
"createdBy": {
"value": "1742991596",
"boost": 1
}
}
},
{
"term": {
"isPrivate": {
"value": "true",
"boost": 1
}
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
}
]
}
}

最佳答案

就您而言,您绝对应该使用 bool/filter 因为您没有任何有助于评分的约束,所有约束都是是/否匹配,并且通过使用 filter 您可以从过滤器缓存中受益(使用 Must 时则不会)

因此,一定要使用过滤器选项,但稍作修改(您实际上根本不需要必须,并且您的 boolean 逻辑未正确转换为 boolean 查询):

{
"query": {
"bool": {
"minimum_should_match": 1,
"filter": [
{
"term": {
"deleted": {
"value": "0",
"boost": 1
}
}
},
{
"bool": {
"minimum_should_match": 1,
"should": [
{
"term": {
"isPrivate": {
"value": "false",
"boost": 1
}
}
},
{
"bool": {
"filter": [
{
"term": {
"createdBy": {
"value": "1742991596",
"boost": 1
}
}
},
{
"term": {
"isPrivate": {
"value": "true",
"boost": 1
}
}
}
]
}
}
]
}
}
]
}
}
}

总结一下:

  • 应该 = OR 条件
  • 必须 = AND 条件(需要评分时)
  • filter = AND 条件(当不需要评分和/或当您希望从过滤器缓存中受益时)
  • 奖励:must_not = NOT 条件

关于java - ElasticSearch 中 BoolQuery 的 "filter"的用途是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56801127/

43 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com