线粒体同学 2023-05-29 19:49 采纳率: 50%
浏览 28

ElasticSearch

ES查询不生效问题:
SQL如下:


{
    "from": 0,
    "size": 20,
    "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"
            }
        }
    ]
}

为什么可以匹配到id为498的数据,求解答:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "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
                ]
            }
        ]
    }
}

  • 写回答

1条回答 默认 最新

  • 鱼弦 全栈领域优质创作者 2023-05-29 19:55
    关注

    根据您提供的信息,我们可以看到请求的结果中返回了两条记录,其中一条记录的"id"为498,符合您的查询条件,而另一条记录的"id"为501,不符合您的查询条件。因此,我们需要进一步分析您的查询条件和数据内容,以确定为什么可以匹配到id为498的数据。

    首先,根据您的查询条件,可以看到您要求满足以下条件的记录:

    "companyId"字段的值为3211002;
    "hrId"字段的值为1005031,或者"manager"字段的值包含1005031,或者"manager"字段为一个空数组,或者"manager"字段不存在;
    "status"字段的值为0;
    "approvals"字段不存在,或者"approvals"字段包含一个或多个元素,其中"approvalStatus"字段的值为1。
    根据查询结果中返回的数据,我们可以看到,id为498的记录符合这些条件,因为:

    "companyId"字段的值为3211002;
    "hrId"字段的值不是1005031,但是"manager"字段的值包含1005031,因此符合第二个查询条件;
    "status"字段的值为0;
    "approvals"字段存在,其中包含一个元素,其中"approvalStatus"字段的值为1,因此符合第四个查询条件。
    至于为什么另一条记录的"id"为501的记录也被返回,可能是因为该记录在其他查询条件中符合要求,但是在当前查询条件中不满足所有条件,因此得分为null,但是仍然会被返回。如果您希望只返回满足所有查询条件的记录,可以将查询条件中的"minimum_should_match"参数设置为"100%",以确保所有条件都必须满足。例如:

    json

    {
      "from": 0,
      "size": 20,
      "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": "100%"
              }
            }
          ]
        }
      },
      "sort": [
        {
          "createTime": {
            "order": "desc"
          }
        }
      ]
    }
    
    

    这样就可以确保只返回满足所有查询条件的记录了

    评论

报告相同问题?

问题事件

  • 创建了问题 5月29日

悬赏问题

  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line