在 Elasticsearch 中的所有字段上创建分析和 not_analyzed 索引

Creating analyzed and not_analyzed index on all fields in Elasticsearch

我是 Elasticsearch 的新手。我需要创建具有 "analyzed" 和 "not_analyzed" 索引的所有字符串字段。我已经能够为 1 或 2 个字段执行此操作,但如果我的文档中有 50 个字段,我该如何执行此操作?我使用以下代码在几个字段上创建了这样的索引:

mapping = {
    "my_type": {
        "properties": {
            "random_num": {"type": "string"},
            "category": { "type":"multi_field", "fields":{ "category":{ "type":"string", "index":"analyzed" },"untouched":{ "type":"string", "index":"not_analyzed" } } },
            "url": {"type": "string"},
            "id": {"type": "string"},
            "desc": { "type":"multi_field", "fields":{ "desc":{ "type":"string", "index":"analyzed" },"untouched":{ "type":"string", "index":"not_analyzed" } } }
        }
    }
}

如果有人能帮助我解决这个问题,我将不胜感激。谢谢!

您可以浏览文档 here。 使用动态索引模板,您可以添加如下规则并实现相同的目标。 在下面的示例中,我启用了一个名为 raw 的附加字段,如果大小小于 256,即 not_analyzed。

{
  "mappings": {
    "my_type": {
      "dynamic_templates": [
        {
          "string_fields": {
            "mapping": {
              "type": "string",
              "fields": {
                "raw": {
                  "index": "not_analyzed",
                  "ignore_above": 256,
                  "type": "string"
                }
              }
            },
            "match_mapping_type": "string",
            "match": "*"
          }
        }
      ]
    }
  }
}