【问题标题】:Elasticsearch - dynamic mapping with multi-field supportElasticsearch - 支持多字段的动态映射
【发布时间】:2019-01-17 10:02:49
【问题描述】:

是否可以动态添加具有多字段支持的新字段?

我的索引具有仅在索引时才知道的属性。所以这些字段将包含在动态映射中。

但是,当动态添加一个新字段时,我需要将其映射为text 并带有三个子字段:keyworddate(如果它适合dynamic_date_formats)和long .

通过这三个子字段,我将能够以最高性能搜索和聚合许多查询。

我知道我可以使用带有keyvalue 属性的nested 字段将我的索引与这些“动态字段”进行“预”映射,因此我可以使用这三个子字段创建值属性。但我不想创建嵌套的键/值字段,因为在对大量文档执行聚合时它不是很快。

【问题讨论】:

    标签: elasticsearch


    【解决方案1】:

    我找到了。

    Dynamic templates 就是答案。

    非常简单:)

    {
      "mappings": {
        "doc": {
          "dynamic_templates": [
            {
             "objs": {
                "match_mapping_type": "object",
                "mapping": {
                    "type": "{dynamic_type}"
                }
             }  
            },
            {
              "attrs": {
                "match_mapping_type": "*",
                "mapping": {
                  "type": "text",
                  "fields": {
                    "raw": {
                        "type": "keyword"
                    },
                    "long": {
                        "type": "long",
                        "ignore_malformed": true
                    },
                    "double": {
                        "type": "double",
                        "ignore_malformed": true
                    },
                    "date": {
                        "type": "date",
                        "format": "dd/MM/yyyy||dd/MM/yyyy HH:mm:ss||dd/MM/yyyy HH:mm",
                        "ignore_malformed": true
                    }
                  }
                }
              }
            }
          ],
          "dynamic": "strict",
          "properties": {
            "fixed": {
                "properties": {
                    "aaa": {
                        "type": "text"
                    },
                    "bbb": {
                        "type": "long"
                    },
                    "ccc": {
                        "type": "date",
                        "format": "dd/MM/yyyy"
                    }
                }
            },
            "dyn": {
                "dynamic": true,
                "properties": {
                }
            }
          }
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2016-04-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多