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