简体   繁体   English

ElasticSearch中BoolQuery的“过滤器”的目的是什么?

[英]What is the purpose of BoolQuery's “filter” in ElasticSearch?

I read the documentation of BoolQuery and according to it, this the purpose, 我阅读了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. Filter子句在过滤器上下文中执行,这意味着计分被忽略,并且子句被视为用于缓存。

Also from BoolQueryBuilder class: 同样来自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;
    }

but I can't get my head around, this. 但是我无法理解。 When should I use filter vs (should or must) 什么时候应该使用过滤器vs(应该还是必须)

Here is the example I am working on : 这是我正在处理的示例:

I want to filter out some records based on the following assumptions : 我想根据以下假设过滤掉一些记录:

Fetch All 全部提取

1) Records where deleted=0 and isPrivate=true 1)记录其中deleted=0 isPrivate=true

AND

2) Records where (isPrivate=false or [isPrivate=true and createdBy=loggedInUser]) 2)记录哪里(isPrivate=false or [isPrivate=true and createdBy=loggedInUser])

Here are the 2 queries which give the same result, I want to know what filter query signifies 这是两个给出相同结果的查询,我想知道什么是filter查询

Result without Filter using just must and should clause. 不使用Filter的结果仅使用must和should子句。

"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 with using filter 使用过滤器查询

"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
          }
        }
      ]
    }
  }

In your case, you should definitely use bool/filter since you don't have any constraint that contributes to scoring, all constraints are yes/no matches, and by using filter you can benefit from filter caches (which you don't when using must) 在您的情况下,您应该绝对使用bool/filter因为您没有任何有助于得分的约束,所有约束都是yes / no匹配,并且通过使用filter可以受益于filter缓存(使用时不会这样做)必须)

So definitely go with the filter option, but with a slight modification (you don't really need must at all and your boolean logic is not properly translated to bool queries): 因此,绝对可以使用filter选项,但要稍作修改(您根本不需要,而且布尔逻辑未正确转换为bool查询):

{
  "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
                        }
                      }
                    }
                  ]
                }
              }
            ]
          }
        }
      ]
    }
  }
}

So to sum up: 所以总结一下:

  • should = OR condition should = OR条件
  • must = AND condition (when scoring is desired) must = AND条件(需要评分时)
  • filter = AND condition (when scoring is not desired and/or when you want to benefit from filter caching) filter = AND条件(当不需要评分和/或希望从过滤器缓存中受益时)
  • Bonus: must_not = NOT condition 奖励: must_not = NOT条件

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM