弹性搜索中子对象的过滤字段



我被这个独特的问题打动了,希望你们中有人知道答案。情况如下:我有一个对象层次Asset ->媒体项目(一对多)。某些媒体项可能在运行查询时过期,如果文本与过期的媒体项匹配,则不应返回记录。

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": []
}
}

。嵌套查询,但它仍然设法找到过期的媒体项

{

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

返回
{
"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"
}
}
]
}
}
}
}
]
}
}

此查询返回记录,即使媒体项目已过期,但当我将过期日期更改为2023以上的任何内容(在第一个媒体项目的过期日期之后)时,它会过滤掉内容。我们如何解决这个问题?任何帮助都是非常感谢的

您的查询是正确的。

但它似乎不工作,因为任何一个问题从下面:

Issue 1: Field Name

请检查您提供的字段名称是否正确,根据Elasticsearch中的索引映射。您可以检查parent.childrenparent.children.expirationDate,而不像childrenchildren.expirationDate那样在字段名中提供父级。

"query": {
"bool": {
"should": [
{
"bool": {
"must_not": [
{
"exists": {
"field": "children",
"boost": 1
}
}
],
"adjust_pure_negative": true,
"boost": 1
}
},
{
"range": {
"children.expirationDate": {
"from": "2022-21-12 17:50:28",
"include_lower": true,
"include_upper": true,
"boost": 1
}
}
}
]
}
}

问题2:日期字段没有正确映射格式

如果上述解决方案没有解决,请检查日期是否正确映射在索引中。

你可以验证你的日期是否被正确存储,通过在expirationDate上应用sort,如下所示,它将在结果中以毫秒为单位重新显示日期,你可以将毫秒转换为日期,并验证它与你的文档日期相同:

{
"sort": [
{
"children.expirationDate": {
"order": "desc"
}
}
]
}

结果:


{
"_index": "74881108",
"_id": "436NOIUB4wNKNry7gjUP",
"_score": null,
"_source": {
"id": "1",
"title": "first",
"children": {
"id": "1",
"title": "child1",
"expirationDate": "2023-21-12 15:14:35"
}
},
"sort": [
1703171675000
]
}

这里,1703171675000毫秒相当于2023-21-12 15:14:35

最新更新