Filtering expired child document while searching

Hi Everyone,
I am struck with this unique problem and hopefully one of you might know the answer.
Here is the situation:
I have an object hierarchy Asset -> Media Items (One to Many). Some of the Media Items may expire when your runs the query if text matches with the expired media Item then record shouldn't be returned .

Class Asset{
List<MediaItem> mediaItems;
}

Class MediaItem {
private String title;
private Instant expirationDate;
}
Data :
Media Item 1:
title : some media item
Expiration Date : 2023-12-30T23:00:00.000Z (Not Expired)

Media Item 2:
title : Shouldnotbelisted
Expiration Date : 2021-12-30T23:00:00.000Z (Expired)
{
	"from": 0,
	"size": 50,
	"query": {
		"bool": {
			"should": [			
				{
					"simple_query_string": {
						"query": "Shouldnotbelisted",
						"fields": [
							"asset.mediaItems.title^1.0"
						],
						"flags": -1,
						"default_operator": "or",
						"analyze_wildcard": false,
						"auto_generate_synonyms_phrase_query": true,
						"fuzzy_prefix_length": 0,
						"fuzzy_max_expansions": 50,
						"fuzzy_transpositions": true,
						"boost": 1.0
					}
				}
			],
			"adjust_pure_negative": true,
			"minimum_should_match": "1",
			"boost": 1.0
		}
	},
	"post_filter": {
		"bool": {
			"filter": [
			
				{
					"bool": {
						"should": [
						
							{
								"range": {
									"asset.mediaItems.expirationDate": {
										"from": "2022-12-21T17:50:28.879Z",
										"include_lower": true,
										"include_upper": true,
										"boost": 1.0
									}
								}
							}
						],
						"adjust_pure_negative": true,
					
						"boost": 1.0
					}
				}
			],
			"adjust_pure_negative": true,
			"boost": 1.0
		}
	},
	"version": true,
	"explain": true,
	"_source": {
		"includes": [
		
		],
		"excludes": []
	}
}

Tried. nested query as well but still it manages to find the expired media item

{
   
    "from": 0,
    "size": 10,
    "query": {
        "bool": {
            "should": [
                {
                    "nested": {
                        "query": {
                            "match": {
                                "asset.title": {
                                    "query": "Shouldnotbelisted",
                                    "operator": "OR",
                                    "prefix_length": 0,
                                    "max_expansions": 50,
                                    "fuzzy_transpositions": true,
                                    "lenient": false,
                                    "zero_terms_query": "NONE",
                                    "auto_generate_synonyms_phrase_query": true,
                                    "boost": 1.0
                                }
                            }
                        },
                        "path": "asset",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                },
                {
                    "nested": {
                        "query": {
                            "match": {
                                "asset.mediaItems.title": {
                                    "query": "Shouldnotbelisted",
                                    "operator": "OR",
                                    "prefix_length": 0,
                                    "max_expansions": 50,
                                    "fuzzy_transpositions": true,
                                    "lenient": false,
                                    "zero_terms_query": "NONE",
                                    "auto_generate_synonyms_phrase_query": true,
                                    "boost": 1.0
                                }
                            }
                        },
                        "path": "asset.mediaItems",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0,
                          "inner_hits" : {}
                    }
                }
            ],
            "adjust_pure_negative": true,
            "minimum_should_match": "1",
            "boost": 1.0
        }
    },
    "post_filter": {
        "bool": {
            "filter": [
                {
                    "nested": {
                        "query": {
                            "term": {
                                "asset.isActive": {
                                    "value": true,
                                    "boost": 1.0
                                }
                            }
                        },
                        "path": "asset",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                },
                {
                    "bool": {
                        "should": [
                            {
                                "nested": {
                                    "query": {
                                        "range": {
                                            "asset.mediaItems.expirationDate": {
                                                "lte": "2023-12-25T00:42:13.249169Z",
                                                "to": null,
                                                "include_lower": false,
                                                "include_upper": true,
                                                "boost": 1.0
                                            }
                                        }
                                    },
                                    "path": "asset.mediaItems",
                                    "ignore_unmapped": false,
                                    "score_mode": "none",
                                    "boost": 1.0
                                }
                            }
                        ],
                        "adjust_pure_negative": true,
                        "minimum_should_match": "1",
                        "boost": 1.0
                    }
                },
                {
                    "nested": {
                        "query": {
                            "range": {
                                "asset.maxExpirationDate": {
                                    "from": "2022-12-25T00:42:13.249182Z",
                                    "to": null,
                                    "include_lower": true,
                                    "include_upper": true,
                                    "boost": 1.0
                                }
                            }
                        },
                        "path": "asset",
                        "ignore_unmapped": false,
                        "score_mode": "none",
                        "boost": 1.0
                    }
                }
            ],
            "adjust_pure_negative": true,
            "boost": 1.0
        }
        
    },
    "version": true,
    "explain": false,
    "_source": false

}

Returns

{
    "took": 3,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 1,
            "relation": "eq"
        },
        "max_score": 0.0,
        "hits": [
            {
                "_index": "tmp_1c339e6c-9aaa-4b9b-8d59-6b5634292db7",
                "_type": "_doc",
                "_id": "7aSpRoUBhHD5CTx_QD5d",
                "_version": 1,
                "_score": 0.0,
                "_ignored": [
                    "asset.description.keyword"
                ],
                "inner_hits": {
                    "asset.mediaItems": {
                        "hits": {
                            "total": {
                                "value": 1,
                                "relation": "eq"
                            },
                            "max_score": 14.493011,
                            "hits": [
                                {
                                    "_index": "tmp_1c339e6c-9aaa-4b9b-8d59-6b5634292db7",
                                    "_type": "_doc",
                                    "_id": "7aSpRoUBhHD5CTx_QD5d",
                                    "_nested": {
                                        "field": "asset",
                                        "offset": 0,
                                        "_nested": {
                                            "field": "mediaItems",
                                            "offset": 1
                                        }
                                    },
                                    "_score": 14.493011,
                                    "_source": {
                                        "thumbnailSrc": "",
                                        "isAssetDefault": false,
                                        "language": {
                                            "title": "English"
                                        },
                                        "title": "Shouldnotbelisted",
                                        "classificationId": "INTERNAL",
                                        "createdOn": 0,
                                        "url": "url",
                                        "modificationDate": "2020-10-15T09:14:30.776Z",
                                        "originId": "03dd87a9-b68c-464c-80d1-c53e0b1a72eb",
                                        "uploadDate": "2020-10-15T09:12:09.817Z",
                                        "assetId": "c67dd86e-dc69-4ae2-9fe6-8d3885f0d617xxx",
                                        "isMediaTypeDefault": false,
                                        "mediaTypeId": "HYPERLINK",
                                        "variantId": "CUSTOM_THEME",
                                        "id": "d753eb8e-bb13-4ca4-a02f-21cd6dddd3b7",
                                        "expirationDate": "2021-12-30T23:00:00.000Z"
                                    }
                                }
                            ]
                        }
                    }
                }
            }
        ]
    }
}

This query returns the record even though media Item is expired but when I change the expired date to anything more than 2023 (After the expired date of first media Item ) then it filters out content. How can we solve this? Any help is highly appreciated

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.