【问题标题】:How to use nested field in Elasticsearch filter script如何在 Elasticsearch 过滤脚本中使用嵌套字段
【发布时间】:2020-01-12 03:02:08
【问题描述】:

我有以下映射:

 "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

谢谢。

【问题讨论】:

  • 您确定属性values 属于您的pim_catalog_product 类型吗?此外,在您提供的缩写映射中,我没有在您的 values 映射中看到 stocks_created-date 属性。
  • @JamesWoodruff 对误导性映射感到抱歉。我已经更新了问题。

标签: elasticsearch


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多