在 elasticsearch 1.7 中的索引下的所有(未来)类型中将字段设置为 not_analysed

Set fields to not_analysed in all (future) types under an index in elasticsearch 1.7

我的索引有很多类型,新类型的创建不是我能控制的。我知道数据结构很扎实,但我事先不知道类型的名称。

我想将一些字段设置为not_analysed,而一些应该被分析。有办法实现吗?

Dynamic mappings 是要走的路。由于您提到 analyzednot_analyzed 我认为您在谈论 string 字段。

我们的想法是更新您的索引和映射,以便为您的字符串字段包含一个动态模板:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [     <--- include this section in your existing mapping
        {
          "analyzed": {
            "match_mapping_type": "string",
            "match":   "field1",
            "mapping": {
              "type": "string"
            }
          }
        },
        {
          "not_analyzed": {
            "match_mapping_type": "string",
            "match":   "field2",
            "mapping": {
              "type": "string",
              "index": "not_analyzed"
            }
          }
        }
      ]
    }
  }
}

另一种方法是使每个新的字符串字段都成为 analyzednot_analyzed,这样您就不必枚举所有字段,只需使用:

PUT my_index
{
  "mappings": {
    "my_type": {
      "dynamic_templates": [     <--- include this section in your existing mapping
       {
         "strings": {
           "match_mapping_type": "string",   <-- match all string fields
           "mapping": {
             "type": "string",
             "fields": {
               "raw": {
                 "type":  "string",
                 "index": "not_analyzed",
                 "ignore_above": 256
               }
             }
           }
         }
       }
      ]
    }
  }
}

我还要补充 Val 的出色回答,您可能希望将这些动态模板添加到索引的 _default_ 映射中,因为您提到您事先不知道类型。例如:

PUT /my_index/_mapping/_default_
{
  "dynamic_templates": [
    {
      "analyzed": {
        "match_mapping_type": "string",
        "match": "*_text",
        "mapping": {
          "type": "string"
        }
      }
    },
    {
      "not_analyzed": {
        "match_mapping_type": "string",
        "match": "*_key",
        "mapping": {
          "type": "string",
          "index": "not_analyzed"
        }
      }
    }
  ]
}

有了它,您可以将任意类型添加到索引,并且将分析添加到新类型的文档中以“_text”结尾的任何字段。不会分析任何以“_key”结尾的字段。您可以阅读有关 default mapping in the docs.

的更多信息