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

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

问题描述

我有一组文件,如

{
    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 ?

推荐答案

编辑:下面的位图可能是一个有趣的阅读,答案本身有点过时。这些功能中的一些功能在2.x左右。另外,Slawek在另一个答案中指出,在这种情况下,条款查询是一种简单的方法来干预搜索。重新确定最终的最佳做法。 -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

你可能想要一个 Bool查询(或更可能过滤器以及另一个查询),其中包含应该子句。

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

bool查询有三个主要属性:必须应该 must_not 。它们中的每一个都接受另一个查询或一组查询。条款名称是非常不言自明的;在您的情况下,应该子句可以指定列表过滤器,匹配任何一个将返回您要查找的文档。

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.

从文档中:


在不含必须子句,一个或多个应该子句必须与文档匹配。可以使用 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来支持过滤器,除非您有理由使用和/或/不(存在此类原因)。 Elasticsearch博客有更多关于每个不同实现的信息,以及何时可能喜欢Bool over / / / /,反之亦然。

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.

弹性搜索博客:关于弹性搜索筛选器的所有内容

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

现在,所有 方式,术语查询是所有上述的DRYer版本。它对于引擎盖下的查询类型是正确的,它的行为与 bool + 应该使用 minimum_should_match 选项,总体来说有点简单。

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天全站免登陆