过滤数组包含任何给定值的项目 [英] Filter items which array contains any of given values

查看:16
本文介绍了过滤数组包含任何给定值的项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组文档,例如

{
    tags:['a','b','c']
    // ... a bunch properties
}

如标题所述:有没有办法使用 Nest 过滤包含任何给定标签的所有文档?

As stated in the title: Is there a way to filter all documents containing any of given tags using Nest ?

例如,上面的记录将匹配 ['c','d']

For instance, the record above would match ['c','d']

或者我应该手动构建多个OR"?

Or should I build multiple "OR"s manually ?

推荐答案

下面的 bitset 内容可能很有趣,但答案本身有点过时.其中一些功能在 2.x 中发生了变化.Slawek 在另一个答案中还指出,在这种情况下,terms 查询是一种简化搜索的简单方法.最后重构以获取当前最佳实践.—nz

The bitset stuff below is maybe an interesting read, but the answer itself is a bit dated. Some of this functionality is changing around in 2.x. Also Slawek points out in another answer that the terms query is an easy way to DRY up the search in this case. Refactored at the end for current best practices. —nz

您可能需要一个 布尔查询(或者更有可能在另一个查询旁边过滤), 带有 should 子句.

You'll probably want a Bool Query (or more likely Filter alongside another query), with a should clause.

bool 查询具有三个主要属性:mustshouldmust_not.这些中的每一个都接受另一个查询或查询数组.条款名称是不言自明的;在您的情况下, should 子句可以指定一个列表过滤器,与其中任何一个匹配将返回您正在查找的文档.

The bool query has three main properties: must, should, and must_not. Each of these accepts another query, or array of queries. The clause names are fairly self-explanatory; in your case, the should clause may specify a list filters, a match against any one of which will return the document you're looking for.

来自文档:

在没有must 子句的布尔查询中,一个或多个should 子句必须匹配一个文档.可以使用 minimum_should_match 参数设置要匹配的最小应该子句数.

In a boolean query with no must clauses, one or more should clauses must match a document. The minimum number of should clauses to match can be set using the minimum_should_match parameter.

以下是 Bool 查询在孤立情况下可能是什么样子的示例:

Here's an example of what that Bool query might look like in isolation:

{
  "bool": {
    "should": [
      { "term": { "tag": "c" }},
      { "term": { "tag": "d" }}
    ]
  }
}

这里是 Bool 查询的另一个示例,作为更通用的过滤器 过滤查询:

And here's another example of that Bool query as a filter within a more general-purpose Filtered Query:

{
  "filtered": {
    "query": {
      "match": { "title": "hello world" }
    },
    "filter": {
      "bool": {
        "should": [
          { "term": { "tag": "c" }},
          { "term": { "tag": "d" }}
        ]
      }
    }
  }
}

您是将 Bool 用作查询(例如,影响匹配的分数)还是用作过滤器(例如,减少被评分或后过滤的命中)是主观的,具体取决于您的要求.

Whether you use Bool as a query (e.g., to influence the score of matches), or as a filter (e.g., to reduce the hits that are then being scored or post-filtered) is subjective, depending on your requirements.

通常最好使用 Bool 而不是 或过滤器,除非您有使用 And/Or/Not 的理由(这样的理由确实存在).Elasticsearch 博客提供了有关每种实现的不同实现的更多信息,以及您可能更喜欢 Bool 而非 And/Or/Not 以及反之亦然的示例.

It is generally preferable to use Bool in favor of an Or Filter, unless you have a reason to use And/Or/Not (such reasons do exist). The Elasticsearch blog has more information about the different implementations of each, and good examples of when you might prefer Bool over And/Or/Not, and vice-versa.

Elasticsearch 博客:关于 Elasticsearch 过滤器位集的所有信息

Elasticsearch blog: All About Elasticsearch Filter Bitsets

使用重构的查询进行更新...

现在,除去所有那些terms 查询是上述所有内容的 DRYer 版本.它在引擎盖下的查询类型方面做了正确的事情,它的行为与使用 minimum_should_match 选项的 bool + should 相同,总体来说更简洁一些.

Now, with all of that out of the way, the terms query is a DRYer version of all of the above. It does the right thing with respect to the type of query under the hood, it behaves the same as the bool + should using the minimum_should_match options, and overall is a bit more terse.

最后一个查询重构了一点:

Here's that last query refactored a bit:

{
  "filtered": {
    "query": {
      "match": { "title": "hello world" }
    },
    "filter": {
      "terms": {
        "tag": [ "c", "d" ],
        "minimum_should_match": 1
      }
    }
  }
}

这篇关于过滤数组包含任何给定值的项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆