匹配所有值在嵌套使用数组elasticsearch [英] Match all values in a nested array using elasticsearch

查看:789
本文介绍了匹配所有值在嵌套使用数组elasticsearch的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用elasticsearch在嵌套数组匹配所有值。对于如。我的搜索数组是 [1,2,3,4,5,6,7,8,9]
我的文档包含数组像

I am trying to use elasticsearch to match all values in a nested array. For eg. my search array is ["1","2","3","4","5","6","7","8","9"] and my document contains an array of arrays like

"arr":[
["1","2","10"],
["4","5"],
["8","9","11"]
]

我需要将所有的值相匹配的嵌套数组中,但只有嵌套阵列中的一个必须是匹配的文件成为匹配。所以,在这个例子中只有第二嵌套数组是一个比赛,因为45都是$搜索阵列p中$ psent(因此我的文件是一个匹配)。我应该使用什么样的查询来实现这一目标?

I need to match all the values inside a nested array but only one of the nested arrays needs to be a match for the document to be a match. So, in this example only the second nested array is a match because "4" and "5" are both present in the search array (therefore my document is a match). What kind of query should I use to achieve this?

推荐答案

您可以用一个简单的Groovy 剧本像这样的侥幸:

You can get away with a simple Groovy script like this one:

def match = false; 
for (sub_array in _source.arr) {
    match = match || (search_array.intersect(sub_array).size() == sub_array.size())
}
return match;

我们的想法是遍历所有改编子阵列和检查,如果与搜索数组的交集的大小与子阵列本身相同。

The idea is to iterate over all arr sub-arrays and check if the intersection with the search array has the same size as the sub-array itself.

脚本中结束语本过滤器,查询应该是这样的:

Wrapping this inside a script filter, the query would look like this:

POST index/type/_search
{
   "query": {
      "filtered": {
         "filter": {
            "script": {
               "script": "def match = false; for (sub_array in _source.arr) {match = match || (search_array.intersect(sub_array).size() == sub_array.size())}; return match;",
               "params": {
                  "search_array": [ "1", "2", "3", "4", "5", "6", "7", "8", "9" ]
               }
            }
         }
      }
   }
}

您还需要确保启用动态脚本为了这个工作,也就是在你的 elasticsearch.yml 文件只是添加脚本。内联:在并重新启动群集

You also need to make sure to enable dynamic scripting in order for this to work, i.e. in your elasticsearch.yml file just add script.inline: on and restart your cluster.

这篇关于匹配所有值在嵌套使用数组elasticsearch的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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