首页 > 解决方案 > 如何在 Elasticsearch 中声明嵌套字段的映射以允许存储不同的类型?

问题描述

本质上,我希望我的映射尽可能无模式,但允许嵌套类型并能够存储可能具有不同类型的数据:

当我尝试添加某些字段具有不同类型值的文档时,我收到如下错误:

“type”:“illegal_argument_exception”,“reason”:“不同类型的映射器 [data.customData.value],current_type [long],merged_type [text]”

这可以通过将字段值映射到文本来轻松解决(或通过首先插入仅包含文本的文档来动态创建它)。但是,我想避免使用模式。也许将所有嵌套在 customData 中的字段设置为文本?我怎么做?

我早些时候遇到了这个问题,但是在意外地设法获得一个有效的动态映射后它开始工作(因为一切都被视为文本。我后来意识到这个问题,因为我需要更改映射以允许嵌套类型。

这种数据的文档很难存储成功:

"customData": [
        {
          "value": "some_text",
          "key": "some_text"
        },
        {
          "value": 0,
          "key": "some_text"
        }
      ]

映射的一部分有效:

{
    "my_index": {
        "aliases": {},
        "mappings": {
            "_doc": {
                "properties": {
                    "data": {
                        "properties": {
                            "customData": {
                                "properties": {
                                    "key": {
                                        "type": "text",
                                        "fields": {
                                            "keyword": {
                                                "type": "keyword",
                                                "ignore_above": 256
                                            }
                                        }
                                    },
                                    "value": {
                                        "type": "text",
                                        "fields": {
                                            "keyword": {
                                                "type": "keyword",
                                                "ignore_above": 256
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    },
                    "some_list": {
                        "type": "nested",
                        "properties": {
                            "some_field": {
                                "type": "text",
                                "fields": {
                                    "keyword": {
                                        "type": "keyword",
                                        "ignore_above": 256
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

本质上,我希望映射尽可能无模式,但允许嵌套类型并能够存储可能具有不同类型的数据:

{
  "mappings": {
    "_doc": {
      "properties": {
        "data": {
          "type": "object"
        },
        "somee_list": {
          "type": "nested" 
        }
      }
    }
  }
}

那么解决这个问题的最佳方法是什么?

标签: elasticsearchtypesnestedmapping

解决方案


推荐阅读