首页 > 解决方案 > 通过两个条件过滤数组的弹性搜索查询

问题描述

我正在尝试创建一个 Elastic Search 查询来修改收到的数据,但我对 Elastic 了解不多,也不知道如何实现。

我有一系列“任务”,我需要接收按某些条件过滤的那些任务。现在查询过滤: - 必须为特定的“组”分配或创建 - 应该为特定的“用户”分配或创建

组和用户由发出请求的用户确定。

{
    "_source": {
        "includes": [
            "taskId",
            "tenantId-createdAt-circuitId",
            "assignedToGroup",
            "assignedToUser",
            "assignedToRole",
            "circuitId",
            "createdAt",
            "createdByRole",
            "currentStatus",
            "taskType",
            "tenantId",
            "history",
            "payload",
            "slaExpiresAt",
            "taskDefinitionName",
            "taskDefinitionId",
            "input"
        ]
    },
    "sort": [
        {
            "createdAt": {
                "order": "desc"
            }
        }
    ],
    "size": 20,
    "from": 0,
    "query": {
        "bool": {
            "filter": {
                "bool": {
                    "must": [
                        {
                            "match": {
                                "tenantId.keyword": "tenant1"
                            }
                        },
                        {
                            "range": {
                                "createdAt": {
                                    "gte": "now-1y/d",
                                    "lte": "now+1d/d"
                                }
                            }
                        }
                    ],
                    "should": [
                        {
                            "match": {
                                "createdByGroup.keyword": "SUPERVISORES"
                            }
                        },
                        {
                            "match": {
                                "assignedToGroup": "SUPERVISORES"
                            }
                        }
                    ]
                }
            },
            "should": [
                {
                    "match": {
                        "assignedToUser.keyword": "supervisorsucursal1@btekstudio1.onmicrosoft.com"
                    }
                },
                {
                    "match": {
                        "createdByUser.keyword": "supervisorsucursal1@btekstudio1.onmicrosoft.com"
                    }
                }
            ]
        }
    }
}

我需要开始处理“角色”条件,因此最终查询需要接收: - 为用户组分配或创建的任务(并且还需要接收该组上没有分配任何角色的任务) - 分配的任务到用户的角色 - 为用户创建的分配的任务

这是我的映射https://pastebin.com/yE6PGLDQ 这是一个示例任务https://pastebin.com/p0942qfv

标签: elasticsearch

解决方案


推荐阅读