簡體   English   中英

如何在 Elasticsearch 過濾器腳本中使用嵌套字段

[英]How to use nested field in Elasticsearch filter script

我有以下映射:

 "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

在此處提供完整映射和示例數據的要點

謝謝。

感謝擴展映射! 調用嵌套對象中的字段時,請嘗試使用點表示法引用內部字段。 例子:

"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 值,它就是默認值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM