我们的表具有嵌套的 map 角色,如下所示:
{group: 123, roles: {boss: department1, manager: department2}}
现在,我想找到在部门1中起作用的123组中的所有文档。如何根据嵌套 map 的值字段进行过滤?
就像是:
{
"query": {
"bool": {
"must": [
{
"match": {
"group": "123"
}
},
{
"nested": {
"path": "roles",
"query": {
"match": {
"roles.???": "department1"
}
}
}
}
]
}
}
}
我知道如何通过嵌套的对象键进行过滤,但是找不到“对象值”进行过滤。
最佳答案
受this answer启发,您可以编写解决方案的脚本:
{
"min_score": 0.1,
"query": {
"function_score": {
"query": {
"match": {
"group": "123"
}
},
"functions": [
{
"script_score": {
"script": {
"source": """
for (def role : params["_source"]["roles"].values()) {
if (role == params.role) {
return 1
}
}
return 0;
""",
"params": {
"role": "department1"
}
}
}
}
]
}
}
}
不过要注意脚本的低效率-可能会变得很慢。相反,我建议您重新考虑您的结构-保持嵌套,但是
{
"group": 123,
"roles": [
{
"position": "manager",
"department": "department1"
},
{
"position": "boss",
"department": "department2"
}
]
}
关于elasticsearch - 如何通过对象值字段过滤Elasticsearch,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63309131/