代码之家  ›  专栏  ›  技术社区  ›  Ali Ok

ElasticSearch覆盖从文本到对象的映射

  •  0
  • Ali Ok  · 技术社区  · 7 年前

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

    {
      "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"},
            ...
          }
        }
      }
    }
    

    这样做很好,但我最终定义了“message”对象中的所有字段(tag、requestId等等)。

    下面是一个示例文档:

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

    我试过很多东西,但都不行。

    我使用的是ElasticSearch 2.4.4版。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Pierre Mallet    7 年前

    你可以用 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