阅读 399

es - elasticsearch mapping - dynamic_templates

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

问 :dynamic_templates有什么特点?
答 :
在这里插入图片描述
问 :dynamic_templates如何使用?
答 :

# dynamic_templates,
# 将数字映射为integer
# 将String映射为text和keyword
# 
PUT /dynamic_templates_test
{
  "mappings" : {
    "dynamic_templates" : [
      {
        "integers" : {
          "match_mapping_type" : "long",
          "match"              : "*_age",
          "unmatch"            : "name_*",
          "mapping"            : {
            "type" : "integer"
          }
        }
      },
      {
        "strings" : {
          "match_mapping_type" : "string",
          "path_match"         : "*.name",
          "path_unmatch"       : "user.age",
          "mapping"            : {
            "type"   : "text",
            "fields" : {
              "raw" : {
                "type"         : "keyword",
                "ignore_above" : 256
              }
            }
          }
        }
      },
      {
        "d_dynamic" : {
          "match_mapping_type" : "string",
          "path_match"         : "analyzer_name.*",
          "mapping"            : {
            "type"     : "{dynamic_type}",
            "analyzer" : "{name}",
            "store"    : true
          }
        }
      }
    ]
  }
}

# 索引
POST /dynamic_templates_test/_doc/1
{
  "my_number" : 324324234,
  "my_age"    : 324324234,
  "my_string" : "hello",
  "user"      : {
    "name" : "good",
    "age"  : 18
  },
  "analyzer_name" : {
    "standard" : "hello",
    "english"  : "good"
  }
}

# show mapping
GET /dynamic_templates_test/_mapping

# 结果
{
  "dynamic_templates_test" : {
    "mappings" : {
      "dynamic_templates" : [
        {
          "integers" : {
            "match" : "*_age",
            "unmatch" : "name_*",
            "match_mapping_type" : "long",
            "mapping" : {
              "type" : "integer"
            }
          }
        },
        {
          "strings" : {
            "path_match" : "*.name",
            "path_unmatch" : "user.age",
            "match_mapping_type" : "string",
            "mapping" : {
              "fields" : {
                "raw" : {
                  "ignore_above" : 256,
                  "type" : "keyword"
                }
              },
              "type" : "text"
            }
          }
        },
        {
          "d_dynamic" : {
            "path_match" : "analyzer_name.*",
            "match_mapping_type" : "string",
            "mapping" : {
              "analyzer" : "{name}",
              "store" : true,
              "type" : "{dynamic_type}"
            }
          }
        }
      ],
      "properties" : {
        "analyzer_name" : {
          "properties" : {
            "english" : {
              "type" : "text",
              "store" : true,
              "analyzer" : "english"
            },
            "standard" : {
              "type" : "text",
              "store" : true,
              "analyzer" : "standard"
            }
          }
        },
        "my_age" : {
          "type" : "integer"
        },
        "my_number" : {
          "type" : "long"
        },
        "my_string" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "user" : {
          "properties" : {
            "age" : {
              "type" : "long"
            },
            "name" : {
              "type" : "text",
              "fields" : {
                "raw" : {
                  "type" : "keyword",
                  "ignore_above" : 256
                }
              }
            }
          }
        }
      }
    }
  }
}
复制代码
文章分类
后端
文章标签