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

问题描述 投票:0回答:1

我有以下映射:

 "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的要点

谢谢。

elasticsearch
1个回答
0
投票

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

"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值,则为默认值。

© www.soinside.com 2019 - 2024. All rights reserved.