gpt4 book ai didi

elasticsearch - 如何在Elasticsearch过滤器脚本中使用嵌套字段

转载 作者:行者123 更新时间:2023-12-02 23:48:16 25 4
gpt4 key购买 nike

我有以下映射:

 "properties": {
"created": {
"type": "date"
},
"id": {
"type": "keyword"
},
"identifier": {
"type": "keyword"
},
"values": {
"properties": {
"description_created-date": {
"properties": {
"<all_channels>": {
"properties": {
"<all_locales>": {
"type": "date"
}
}
}
}
},
"footwear_size-option": {
"properties": {
"<all_channels>": {
"properties": {
"<all_locales>": {
"type": "keyword"
}
}
}
}
}
}
}
}

现在,我想创建一个基于description_created-date字段的查询,并通过比较某个日期在无痛脚本中使用此值。
GET index/pim_catalog_product/_search
{
"query": {
"constant_score": {
"filter": {
"bool": {
"filter": [
{
"script": {
"script": {
"source": "doc['values']['description_created-date']['<all_channels>']['<all_locales>'].value == '2019-12-19'",
"lang": "painless"
}
}
}
]
}
}
}
}
}

但我得到以下错误:
{
"shard": 0,
"index": "index",
"node": "cmh1RMS1SHO92SA3jPAkJA",
"reason": {
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:81)",
"org.elasticsearch.search.lookup.LeafDocLookup.get(LeafDocLookup.java:39)",
"doc['values']['description_created-date']['<all_channels>']['<all_locales>'].value == '2019-12-19'",
" ^---- HERE"
],
"script": "doc['values']['description_created-date']['<all_channels>']['<all_locales>'].value == '2019-12-19'",
"lang": "painless",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "No field found for [values] in mapping with types [pim_catalog_product]"
}
}
}

(我知道我无法像这样比较日期,但这是另一个问题)。

通过 values.description_created-date字段进行搜索的工作原理是:
GET index/pim_catalog_product/_search
{
"query": {
"match": {
"values.description_created-date.<all_channels>.<all_locales>": "2019-12-19"
}
}
}

当我获得特定文档时,该字段的值将显示为:
"values": {
"description_created-date": {
"<all_channels>": {
"<all_locales>": "2019-12-19"
}
}
}

如何在脚本过滤器中使用此字段?我需要执行以下操作:
(pseudocode)
"source": "doc['values']['stocks_created-date'].value > doc['created'].value + 2 days"

我正在使用elasicsearch v6.5.0,这是一个带有elasticsearch和kibana的docker-compose文件:
version: '3'
services:
elasticsearch:
image: docker.elastic.co/elasticsearch/elasticsearch:6.5.0
environment:
- discovery.type=single-node
ports:
- 9200:9200
kibana:
image: docker.elastic.co/kibana/kibana:6.5.0
ports:
- 5601:5601

和要点,具有完整的映射和示例数据 here

谢谢。

最佳答案

感谢您提供的扩展映射!在嵌套对象中调用字段时,请尝试使用点表示法引用内部字段。例:

"source": "doc['values.description_created.<all_channels>.<all_locales>'].value == 2019"

另外,您可以将复合查询减少为仅主要 constant_score复合查询。例:
GET index/_search
{
"query": {
"constant_score": {
"filter": {
"script": {
"script": {
"source": "doc['values.description_created.<all_channels>.<all_locales>'].value == 2019"
}
}
},
"boost": 1
}
}
}

注意:“boost”值是可选的,但如果不提供boost值,则为默认值。

关于elasticsearch - 如何在Elasticsearch过滤器脚本中使用嵌套字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59668273/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com