如何获取正确的数据?

huangapple go评论70阅读模式
英文:

How to get the right data?

问题

我不理解为什么返回了ID = 498。

我的目标是过滤掉ID = 498,我不知道如何做到这一点。

英文:

I have this ElasticSearch Query for ES ,version: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"
            }
        }
    ]
}

But,ES gives me back this:

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

I don't understand why ID = 498 is returned.

My goal is to filter out id = 498, I don't know how to do that.

答案1

得分: 1

你应该将嵌套查询放在must_not内部,这样应该可以工作:

GET test_hr/_search
{
  "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": {
                    "nested": {
                      "path": "approvals",
                      "query": {
                        "exists": {
                          "field": "approvals"
                        }
                      }
                    }
                  }
                }
              }
            ],
            "minimum_should_match": "1"
          }
        }
      ]
    }
  },
  "sort": [
    {
      "createTime": {
        "order": "desc"
      }
    }
  ]
}

测试部分:

PUT test_hr
{
  "mappings": {
    "properties": {
      "approvals":{
        "type": "nested"
      }
    }
  }
}

PUT test_hr/_doc/501
{
  "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
}

PUT test_hr/_doc/498
{
  "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
    }
  ]
}

以下是两个不同查询的示例:

不工作的查询:

# not working
GET test_hr/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "exists": {
            "field": "approvals"
          }
        }
      ]
    }
  }
}

工作的查询:

# working
GET test_hr/_search
{
  "query": {
    "bool": {
      "must_not": [
        {
          "nested": {
            "path": "approvals",
            "query": {
              "exists": {
                "field": "approvals"
              }
            }
          }
        }
      ]
    }
  }
}
英文:

You should add the nested query inside of a must_not, it should work:

GET test_hr/_search
{
"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": {
"nested": {
"path": "approvals",
"query": {
"exists": {
"field": "approvals"
}
}
}
}
}
}
],
"minimum_should_match": "1"
}
}
]
}
},
"sort": [
{
"createTime": {
"order": "desc"
}
}
]
}

test

PUT test_hr
{
"mappings": {
"properties": {
"approvals":{
"type": "nested"
}
}
}
}
PUT test_hr/_doc/501
{
"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
}
PUT test_hr/_doc/498
{
"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
}
]
}

#not working
GET test_hr/_search
{
"query": {
"bool": {
"must_not": [
{
"exists": {
"field": "approvals"
}
}
]
}
}
}
#working
GET test_hr/_search
{
"query": {
"bool": {
"must_not": [
{
"nested": {
"path": "approvals",
"query": {
"exists": {
"field": "approvals"
}
}
}
}
]
}
}
}

如何获取正确的数据?

huangapple
  • 本文由 发表于 2023年5月29日 20:21:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/76357334.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定