弹性搜索按照一个数组的大小进行过滤 [英] elasticsearch filtering by the size of a field that is an array

查看:97
本文介绍了弹性搜索按照一个数组的大小进行过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤具有一个数组的字段并具有多个N元素的文档?

How can I filter documents that have a field which is an array and has more than N elements?

如何过滤具有字段的文档?空数组?

How can I filter documents that have a field which is an empty array?

是解决方案吗?如果是,那么怎么样?

Is facets the solution? If so, how?

推荐答案

我将看看脚本过滤器。以下过滤器应仅返回 fieldname 字段中至少有10个元素的文档,这是一个数组。请记住,这可能是昂贵的,具体取决于你的索引中有多少文件。

I would have a look at the script filter. The following filter should return only the documents that have at least 10 elements in the fieldname field, which is an array. Keep in mind that this could be expensive depending on how many documents you have in your index.

"filter" : {
    "script" : {
        "script" : "doc['fieldname'].values.length > 10"
    }
}

关于第二个问题:你真的有一个空数组吗?还是只是一个没有价值的数组字段?您可以使用缺少的过滤器获取没有特定值的文档字段:

Regarding the second question: do you really have an empty array there? Or is it just an array field with no value? You can use the missing filter to get documents which have no value for a specific field:

"filter" : {
    "missing" : { "field" : "user" }
}

否则我想你需要再次使用脚本,与上面我建议的一样,只是具有不同的长度作为输入。如果长度不变,我会将其放在 params 部分中,以便脚本将被弹性搜索缓存并重新使用,因为它总是相同的:

Otherwise I guess you need to use scripting again, similarly to what I suggested above, just with a different length as input. If the length is constant I'd put it in the params section so that the script will be cached by elasticsearch and reused, since it's always the same:

"filter" : {
    "script" : {
        "script" : "doc['fieldname'].values.length > param1"
        "params" : {
            "param1" : 10
        }
    }
}

这篇关于弹性搜索按照一个数组的大小进行过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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