You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

ElasticSearch 2.2查询结果不符,如何过滤指定ID数据?

Elasticsearch 2.2 查询结果不符合预期的问题排查与解决

问题描述

我使用以下适用于Elasticsearch 2.2版本的查询语句:

{
    "query": {
        "bool": {
            "must": [
                {
                     "term": {
                        "companyId": 3211002
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "hrId": 1005031
                                }
                            },
                            {
                                "terms": {
                                    "manager": [
                                        1005031
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must": [
                                        {
                                            "exists": {
                                                "field": "manager"
                                            }
                                        },
                                        {
                                            "terms": {
                                                "manager": []
                                            }
                                        }
                                    ]
                                }
                            },
                            {
                                "bool": {
                                    "must_not": {
                                        "exists": {
                                            "field": "manager"
                                        }
                                    }
                                }
                            }
                        ],
                        "minimum_should_match": "1"
                    }
                },
                {
                    "terms": {
                        "status": [
                            "0"
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must_not": {
                                        "exists": {
                                            "field": "approvals"
                                        }
                                    }
                                }
                            },
                            {
                                "nested": {
                                    "query": {
                                        "terms": {
                                            "approvals.approvalStatus": [
                                                1
                                            ]
                                        }
                                    },
                                    "path": "approvals"
                                }
                            }
                        ],
                        "minimum_should_match": "1"
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "createTime": {
                "order": "desc"
            }
        }
    ]
}

但Elasticsearch返回的结果中包含了ID为498的条目:

{
    "hits": {
        "total": 2,
        "max_score": null,
        "hits": [
            {
                "_index": "recruitment",
                "_type": "requirement",
                "_id": "501",
                "_score": null,
                "_source": {
                    "id": 501,
                    "companyId": 3211002,
                    "hrId": 1005031,
                    "formId": 501,
                    "requirementId": "Test0004",
                    "positionTitle": "招聘需求004",
                    "positionProperties": 1,
                    "requirementCount": 3,
                    "requirementType": 1,
                    "reportTo": 1004651,
                    "requirementTeam": 188384773,
                    "requirementStatus": 0,
                    "manager": [
                        1005031
                    ],
                    "status": 0,
                    "createTime": 1685350126881,
                    "hasAttachment": false
                },
                "sort": [
                    1685350126881
                ]
            },
            {
                "_index": "recruitment",
                "_type": "requirement",
                "_id": "498",
                "_score": null,
                "_source": {
                    "id": 498,
                    "companyId": 3211002,
                    "hrId": 1004483,
                    "formId": 498,
                    "requirementId": "Test0002",
                    "positionTitle": "审批测试1",
                    "positionProperties": 2,
                    "requirementCount": 2,
                    "requirementStatus": 0,
                    "status": 0,
                    "createTime": 1685346243403,
                    "hasAttachment": false,
                    "approvals": [
                        {
                            "approvalStatus": 1,
                            "nodes": [
                                {
                                    "approvers": [
                                        {
                                            "approverStatus": 3,
                                            "id": 1005031
                                        }
                                    ],
                                    "nodeStatus": 3
                                },
                                {
                                    "approvers": [
                                        {
                                            "approverStatus": 1,
                                            "id": 1004789
                                        },
                                        {
                                            "approverStatus": 1,
                                            "id": 1004483
                                        }
                                    ],
                                    "nodeStatus": 1
                                }
                            ],
                            "createTime": "2023-05-29T15:44:07+08:00",
                            "hrId": 1004483,
                            "id": 597
                        }
                    ]
                },
                "sort": [
                    1685346243403
                ]
            }
        ]
    }
}

我需要过滤掉ID为498的条目,但不清楚它被返回的原因以及如何调整查询。

问题原因

ID为498的条目被返回,核心原因是它满足了查询中第二个bool的should条件里的最后一条规则:

{
    "bool": {
        "must_not": {
            "exists": {
                "field": "manager"
            }
        }
    }
}

查看该条目的_source数据,确实不存在manager字段,因此这条规则匹配成功,满足了minimum_should_match:1的要求,整个bool条件成立。再加上它同时满足其他must条件(companyId=3211002、status=0、approvals.approvalStatus=1),最终被纳入结果集。

解决方法

如果你原本的筛选逻辑是仅保留hrId为1005031,或者manager列表包含1005031的条目,只需删除第二个bool中多余的两条should规则即可。修改后的查询语句如下:

{
    "query": {
        "bool": {
            "must": [
                {
                     "term": {
                        "companyId": 3211002
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "term": {
                                    "hrId": 1005031
                                }
                            },
                            {
                                "terms": {
                                    "manager": [
                                        1005031
                                    ]
                                }
                            }
                        ],
                        "minimum_should_match": "1"
                    }
                },
                {
                    "terms": {
                        "status": [
                            "0"
                        ]
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "bool": {
                                    "must_not": {
                                        "exists": {
                                            "field": "approvals"
                                        }
                                    }
                                }
                            },
                            {
                                "nested": {
                                    "query": {
                                        "terms": {
                                            "approvals.approvalStatus": [
                                                1
                                            ]
                                        }
                                    },
                                    "path": "approvals"
                                }
                            }
                        ],
                        "minimum_should_match": "1"
                    }
                }
            ]
        }
    },
    "sort": [
        {
            "createTime": {
                "order": "desc"
            }
        }
    ]
}

修改后,只有hrId=1005031或manager列表包含1005031的条目会被匹配,ID为498的条目因不满足这两个条件,将被过滤掉。

内容的提问来源于stack exchange,提问作者ZhuXJun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.20 10:57:54