I want to retrieve the documents where
- Latest Previous Linkage info (sorted based on modifiedTime desc)'s associationType is OWNER, deleted false
ES Template mapping contains these properties
"properties": {
"previousLinkageInfo": {
"type": "nested",
"properties": {
"associationType": {
"type": "keyword",
"normalizer": "lowercase_normalizer"
},
"modifiedTime": {
"type": "long"
},
"deleted": {
"type": "boolean"
}
}
}
}
My document looks like this with previousLinkageInfo defined as nested object in ES template.
{
"id": "id1",
"previousLinkageInfo": [
{
"associationType": "OWNER",
"deleted": false,
"modifiedTime": 123422
},
{
"associationType": "USER",
"deleted": false,
"modifiedTime": 123421
}
]
}
I break this down into 2 steps
- Check if I am able to get inner hits as desired - working fine
- Filter on top of the inner hits that have associationType as OWNED
Step 1: I tried to get inner hit using the criteria of previousLinkageInfo.deleted false and based on previousLinkageInfo.modifiedTime desc.
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "previousLinkageInfo",
"query": {
"bool": {
"must": [
{
"match": {
"previousLinkageInfo.deleted": false
}
}
]
}
},
"score_mode": "sum",
"inner_hits": {
"name": "nested_docs",
"size": 1,
"sort": [
{
"previousLinkageInfo.modifiedTime": {
"order": "desc"
}
}
]
}
}
}
]
}
}
}
This query is working fine with below result as expected
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 1,
"successful": 1,
"skipped": 0,
"failed": 0
},
"hits": {
"total": {
"value": 1,
"relation": "eq"
},
"max_score": 0.36464313,
"hits": [
{
"_index": "test_customer_vehicle_association",
"_type": "_doc",
"_id": "1",
"_score": 0.36464313,
"_source": {
"id": "id1",
"previousLinkageInfo": [
{
"associationType": "OWNER",
"deleted": false,
"modifiedTime": 123424,
"associationStartDate": 2,
"associationEndDate": 3
},
{
"associationType": "USER",
"deleted": false,
"modifiedTime": 123423,
"associationStartDate": 1,
"associationEndDate": 2
}
]
},
"inner_hits": {
"nested_docs": {
"hits": {
"total": {
"value": 2,
"relation": "eq"
},
"max_score": null,
"hits": [
{
"_index": "test_customer_vehicle_association",
"_type": "_doc",
"_id": "1",
"_nested": {
"field": "previousLinkageInfo",
"offset": 0
},
"_score": null,
"_source": {
"modifiedTime": 123424,
"deleted": false,
"associationType": "OWNER",
"associationEndDate": 3,
"associationStartDate": 2
},
"sort": [
123424
]
}
]
}
}
}
}
]
}
}
Step 2: Next step is to check if my inner hit's associationType is OWNER. For this, I tried using script which is not working.
{
"query": {
"bool": {
"must": [
{
"nested": {
"path": "previousLinkageInfo",
"query": {
"bool": {
"must": [
{
"match": {
"previousLinkageInfo.deleted": false
}
}
]
}
},
"score_mode": "sum",
"inner_hits": {
"name": "nested_docs",
"size": 1,
"sort": [
{
"previousLinkageInfo.modifiedTime": {
"order": "desc"
}
}
]
}
}
}
],
"filter": [
{
"script": {
"script": {
"source": "def latestInnerHit = (params['nested_docs'] != null && params['nested_docs'].hits.hits.length > 0) ? params['nested_docs'].hits.hits[0]['_source'] : null; return latestInnerHit != null && latestInnerHit['associationType'] == 'OWNER';",
"lang": "painless"
}
}
}
]
}
}
}
I tried multiple ways to access the inner hits but params['nested_docs'] is null because of which I am not able to get desired results. Any help will highly be appreciated.