NestQuery In Elastic search and it's dosen't work I got data like this :
{
"took": 17,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 6,
"max_score": null,
"hits": [
{
"_index": "candidate",
"_type": "application",
"_id": "4013361",
"_score": null,
"_routing": "64657bb087209b2b37c8f2f2",
"_parent": "64657bb087209b2b37c8f2f2",
"_source": {
"evaluation_records": []
},
"sort": [
1699897664000
]
},
{
"_index": "candidate",
"_type": "application",
"_id": "4005398",
"_score": null,
"_routing": "636b73f187209b293dcd37a3",
"_parent": "636b73f187209b293dcd37a3",
"_source": {
"evaluation_records": []
},
"sort": [
1675245647000
]
},
{
"_index": "candidate",
"_type": "application",
"_id": "4003184",
"_score": null,
"_routing": "6376e71787209b293dcd3917",
"_parent": "6376e71787209b293dcd3917",
"_source": {
"evaluation_records": [
{
"score": 0.0,
"account_id": null,
"create_time": "2023-01-12T11:07:23",
"provider": 4,
"project_id": "133",
"phase_id": 1170,
"project_title": "一个视频题-老",
"project_id_not_analyzed": "133",
"hr_id": 82752,
"status": 1
}
]
},
"sort": [
1673521621000
]
},
{
"_index": "candidate",
"_type": "application",
"_id": "3995775",
"_score": null,
"_routing": "6275e73687209b47703a41ca",
"_parent": "6275e73687209b47703a41ca",
"_source": {
"evaluation_records": []
},
"sort": [
1668000149000
]
},
{
"_index": "candidate",
"_type": "application",
"_id": "3997529",
"_score": null,
"_routing": "63041b6d87209b7edf3d1228",
"_parent": "63041b6d87209b7edf3d1228",
"_source": {
"evaluation_records": [
{
"score": null,
"account_id": null,
"create_time": "2022-09-02T16:19:55",
"provider": 3,
"project_id": "54",
"phase_id": 1170,
"project_title": null,
"project_id_not_analyzed": "54",
"hr_id": 82752,
"status": 0
}
]
},
"sort": [
1662133365000
]
},
{
"_index": "candidate",
"_type": "application",
"_id": "3996285",
"_score": null,
"_routing": "6255328e87209b47703a410c",
"_parent": "6255328e87209b47703a410c",
"_source": {
"evaluation_records": [
{
"score": 23.2,
"account_id": null,
"create_time": "2022-09-19T15:45:32",
"provider": 3,
"project_id": "54",
"phase_id": 1170,
"project_title": null,
"project_id_not_analyzed": "54",
"hr_id": 82752,
"status": 1
}
]
},
"sort": [
1655291124000
]
}
]
},
"aggregations": {
"CandidateProfileIdAgg": {
"value": 6
}
}
}
when i useing query like :
{
"bool": {
"should": {
"bool": {
"should": {
"bool": {
"should": {
"nested": {
"path": "evaluation_records",
"query": {
"bool": {
"must": {
"bool": {
"should": [
{
"bool": {
"must_not": {
"exists": {
"field": "evaluation_records"
}
}
}
},
{
"bool": {
"must": [
{
"exists": {
"field": "evaluation_records"
}
},
{
"terms": {
"evaluation_records": []
}
}
]
}
}
]
}
}
}
}
}
}
}
}
}
}
}
}
and it's dosen't work , If I want to filter out data where "evaluation records": [], what should I do? ES Mapping like this:
....
"evaluation_records": {
"type": "nested",
"properties": {
"account_id": {
"type": "long"
},
"create_time": {
"type": "date",
"format": "strict_date_optional_time||epoch_millis"
},
"hr_id": {
"type": "long"
},
"phase_id": {
"type": "long"
},
"project_id": {
"type": "string"
},
"project_id_not_analyzed": {
"type": "string",
"index": "not_analyzed"
},
"project_title": {
"type": "string",
"index": "not_analyzed"
},
"provider": {
"type": "long"
},
"remark": {
"type": "string",
"index": "not_analyzed"
},
"score": {
"type": "double"
},
"status": {
"type": "long"
}
}
}
...
Requirement is filter out records that have not been scheduled for evaluation exams. Tried a lot of search methods are not good,
Use must condition with exist query in the nested query.
GET evaluation_nested/_search
{
"query": {
"nested": {
"path": "evaluation_records",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "evaluation_records"
}
}
]
}
}
}
}
}
To reproduce try the following. (Note: The following codes for Elasticsearch v7 and higher)
PUT evaluation_nested
{
"mappings": {
"properties": {
"evaluation_records": {
"type": "nested"
}
}
}
}
PUT evaluation_nested/_doc/1
{
"evaluation_records": [
{
"score": 0,
"account_id": null,
"create_time": "2023-01-12T11:07:23",
"provider": 4,
"project_id": "133",
"phase_id": 1170,
"project_title": "一个视频题-老",
"project_id_not_analyzed": "133",
"hr_id": 82752,
"status": 1
}
]
}
PUT evaluation_nested/_doc/2
{
"test_field": "value",
"evaluation_records": []
}
#you will see only the _doc: 1
GET evaluation_nested/_search
{
"query": {
"nested": {
"path": "evaluation_records",
"query": {
"bool": {
"must": [
{
"exists": {
"field": "evaluation_records"
}
}
]
}
}
}
}
}
If you see the _doc 2
in the response check the following.
Instead of putting the must_not
inside your nested
query, you should put the nested
query inside of a must_not
. - reference