ElasticSearch : "copy_to" 一个嵌套字段

ElasticSearch : "copy_to" a nested fields

我尝试使用 ES "copy_to" 属性将对象字段复制到嵌套字段中,但尽管我多次尝试,但我还是遇到了错误。这是我的结构:

      ...
      "identifiedBy": {
        "type": "object",
        "properties": {
          "type": {
            "type": "keyword",
            "copy_to": "nested_identifiers.type"
          },
          "value": {
            "type": "text",
            "analyzer": "identifier-analyzer",
            "copy_to": "nested_identifiers.type"
          },
          "note": {
            "type": "text"
          },
          "qualifier": {
            "type": "keyword"
          },
          "source": {
            "type": "keyword",
            "copy_to": "nested_identifiers.type"
          },
          "status": {
            "type": "text"
          }
        }
      },
      "nested_identifiers": {
        "type": "nested",
        "properties": {
          "type": {
            "type": "keyword",
          },
          "value": {
            "type": "text",
            "analyzer": "identifier-analyzer",
          },
          "source": {
            "type": "keyword",
          }
        }
      }
      ...

映射错误为

java.lang.IllegalArgumentException: Illegal combination of [copy_to] and [nested] 
mappings: [copy_to] may only copy data to the current nested document or any of its 
parents, however one [copy_to] directive is trying to copy data from nested object [null]
to [nested_identifiers]

有谁知道解决我的问题的方法吗?

感谢您的帮助。

Tldr;

因为 Elasticsearch 索引嵌套文档的方式。如果不更新映射,这是不可能的。

确实有解决方法,使用 include_in_root: true setting

否则,我建议您在编制索引之前对数据进行预处理,并在此预处理过程中将数据复制到嵌套字段中。也许使用 ingest pipeline ?

摄取管道

PUT /72270706/
{
  "mappings": {
    "properties": {
      "root_type":{
        "type": "keyword"
      },
      "nested_doc":{
        "type": "nested",
        "properties": {
          "nested_type":{
            "type": "keyword"
          }
        }
      }
    }
  }
}

PUT _ingest/pipeline/set_nested_type
{
  "processors": [
    {
      "set": {
        "field": "nested_doc.nested_type",
        "copy_from": "root_type"
      }
    }
  ]
}

POST /72270706/_doc?pipeline=set_nested_type
{
  "root_type": "a type"
}

GET /72270706/_search

应该给你

{
  "took" : 392,
  "timed_out" : false,
  "_shards" : {
...
  },
  "hits" : {
...
    },
    "max_score" : 1.0,
    "hits" : [
      {
        "_index" : "72270706",
        "_id" : "laOB0YABOgujegeQNA8D",
        "_score" : 1.0,
        "_source" : {
          "root_type" : "a type",
          "nested_doc" : {
            "nested_type" : "a type"
          }
        }
      }
    ]
  }
}

变通

      ...
      "identifiedBy": {
        "type": "object",
        "properties": {
          "type": {
            "type": "keyword",
            "copy_to": "nested_identifiers.type"
          },
          "value": {
            "type": "text",
            "analyzer": "identifier-analyzer",
            "copy_to": "nested_identifiers.type"
          },
          "note": {
            "type": "text"
          },
          "qualifier": {
            "type": "keyword"
          },
          "source": {
            "type": "keyword",
            "copy_to": "nested_identifiers.type"
          },
          "status": {
            "type": "text"
          }
        }
      },
      "nested_identifiers": {
        "type": "nested",
        "include_in_root": true,
        "properties": {
          "type": {
            "type": "keyword",
          },
          "value": {
            "type": "text",
            "analyzer": "identifier-analyzer",
          },
          "source": {
            "type": "keyword",
          }
        }
      }
      ...

您将需要 re index 现有数据。

但请注意,copy_to 不会将信息复制到嵌套对象中。但是对于另一个具有相同名称但未嵌套的字段。