ElasticSearch中的子对象过滤字段

inkz8wg9  于 2022-12-26  发布在  ElasticSearch
关注(0)|答案(1)|浏览(158)
    • bounty将在6天后过期**。回答此问题可获得+50的声誉奖励。Lokesha S正在寻找来自声誉良好来源的答案:能解决问题人

我对这个独特的问题感到震惊,希望你们中的一个可能知道答案。情况如下:我有一个对象层次结构资产-〉媒体项目(一对多)。一些媒体项目可能会过期时,您运行查询,如果文本与过期的媒体项目匹配,那么记录不应返回。

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(在第一个媒体项目的过期日期之后),然后它过滤掉内容。我们如何解决这个问题?任何帮助都非常感谢

i34xakig

i34xakig1#

您的查询是正确的。
但它似乎是不工作,因为任何一个问题,从下面:

    • 问题1:字段名称**

请根据Elasticsearch中的索引Map检查您提供的字段名称是否正确。您可以检查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:日期字段未正确Map到格式**

如果上述解决方案未解决,请检查日期是否正确Map或未在索引中。
您可以通过对expirationDate应用排序来验证您的日期是否正确存储,如下所示,这将在结果中返回以毫秒为单位的日期,您可以将毫秒转换为日期,并验证它是否与您的文档日期相同:

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

相关问题