elasticsearch按数组字段的大小过滤 [英] elasticsearch filtering by the size of a field that is an array

查看:70
本文介绍了elasticsearch按数组字段的大小过滤的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何过滤具有数组字段且元素多于 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?

faces 是解决方案吗?如果是,怎么办?

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

关于第二个问题:你那里真的有一个空数组吗?或者它只是一个没有值的数组字段?您可以使用 missing filter 来获取文档没有特定字段的值:

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 部分,这样脚本就会被 elasticsearch 缓存并重用,因为它总是相同的:

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 > params.param1"
        "params" : {
            "param1" : 10
        }
    }
}

这篇关于elasticsearch按数组字段的大小过滤的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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