【问题标题】:ElasticSearch overriding mapping from text to objectElasticSearch 覆盖从文本到对象的映射
【发布时间】:2022-04-15 01:02:16
【问题描述】:

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

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

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

这是默认的索引模板,顺序为 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


    【解决方案1】:

    您可以在动态映射中使用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

    【讨论】:

    猜你喜欢
    • 2021-03-15
    • 2021-06-10
    • 2014-07-21
    • 1970-01-01
    • 2012-02-20
    • 2019-10-04
    • 2021-10-08
    • 2012-03-17
    • 1970-01-01
    相关资源
    最近更新 更多