英文:
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"
}
}
}
}
]
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论