首页 > 解决方案 > ElasticSearch 覆盖从文本到对象的映射

问题描述

我正在尝试覆盖字段的映射。

有一个默认索引模板(我无法更改),我用自定义模板覆盖它。

默认索引将“消息”字段映射为文本,但我需要将其视为对象并使其字段可索引/可搜索。

这是默认的索引模板,顺序为 10。

{
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "message_field": {
            "mapping": {
              "index": true,
              "norms": false,
              "type": "text"
            },
            "match": "message",
            "match_mapping_type": "string"
          }
        },
        ...
      ],
      "properties": {
        "message": {
          "doc_values": false,
          "index": true,
          "norms": false,
          "type": "text"
        },
        ...
      }
    }
  },
  "order": 10,
  "template": "project.*"
}

这是我的覆盖:

{
  "template" : "project.*",
  "order" : 100,
  "dynamic_templates": [
    {
      "message_field": {
        "mapping": {
          "type": "object"
        },
        "match": "message"
      }
    }
  ],
  "mappings": {
    "message": {
      "enabled": true,
      "properties": {
        "tag": {"type": "string", "index": "not_analyzed"},
        "requestId": {"type": "integer"},
        ...
      }
    }
  }
}

这很好用,但我最终在“消息”对象中定义了所有字段(标签、requestId、...)。

有没有办法让“消息”对象中的所有字段都可索引/可搜索?

这是一个示例文档:

{
  "level": "30",
  ...
  "kubernetes": {
    "container_name": "data-sync-server",
    "namespace_name": "alitest03",
    ...
  },
  "message": {
    "tag": "AUDIT",
    "requestId": 1234,
    ...
    },
  }
  ...
}

尝试了很多东西,但我无法让它工作。

我正在使用 ElasticSearch 2.4.4 版。

标签: elasticsearch

解决方案


您可以在动态映射中使用path_match属性:

就像是 :

{
  "template": "project.*",
  "order": 100,
  "mappings": {
    "<your document type here>": {
      "dynamic_templates": [
        {
          "message_field": {
            "mapping": {
              "type": "object"
            },
            "match": "message"
          }
        },
        {
          "message_properties": {
            "path_match": "message.*",
            "mapping": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      ]
    }
  }
}

但是您可能必须区分字符串/数字match_mapping_type


推荐阅读