如何在elasticsearch中映射动态字段值?

How to map dynamic field value in elasticsearch?

我正在映射一个 couchbase 网关文档,我想告诉 elasticsearch 避免索引网关添加的内部属性,如“_sync”,这个对象包含另一个名为 "channels" 的对象,它具有如下形式:

"channels": {
  "i7de5558-32ad-48ca-bf91-858c3a1e4588": 12
}

所以我猜这个对象的映射应该是这样的:

"channels": {
     "type": "object",
     "properties": {
         "i7de5558-32ad-48ca-bf91-858c3a1e4588": {
             "type": "integer", 
             "index": "not_analyze"
         }
     }
}

问题是密钥总是在变化,所以我不知道是否应该使用这样的通配符“*”:{"type": "integer", "index": "not_analyze"} 为此 属性 或做其他事情。 有什么建议吗?

有一种动态方法可以根据需要执行此操作,称为 dynamic template

使用模板,您可以创建如下规则:

    PUT /my_index
{
    "mappings": {
        "my_type": {
            "date_detection": false
        }
    }
}

在您的情况下,您可以创建一个模板来将频道对象内的所有新闻字段设置为 not_analyzed。 希望对你有帮助

如果字段是整数类型,则不必在映射中显式提供它们。您可以使用这些字段创建一个空的映射、索引文档。 Elasticsearch 将推断字段类型并动态更新映射。您也可以为此使用 dynamic templates

{
 "mappings": {
  "my_type": {
     "dynamic_templates": [
        {
           "analysed_string_template": {
              "path_match": "channels.*",
              "mapping": {
                 "type": "integer"
              }
           }
        }
     ]
    }
   }
 }