es的mapping设置

 自定义mapping的api

PUT test_index
{
  "mappings": {        #mappings关键字
    "doc": {            #type
      "properties": {        #字段名称和类型的定义
        "name":{              #字段名
          "type": "keyword"     #字段类型
        },
        "message":{
          "type": "text"
        },
        "age":{
          "type": "integer"
        }
      }}}}
  • mapping中字段类型一旦设定后 禁止直接修改。因为lucene实现的倒排索引生成后不允许修改
  • 除非重建索引映射,然后做reindex操作。
    1,POST _reindex
    {
      "source": {"index":"旧索引"},
      "dest": {"index":"备份索引"}
    }
    2,删除旧索引
    3,新索引建立mapping
    4,POST _reindex
    {
      "source": {"index":"备份索引"},
      "dest": {"index":"新索引"}
    }
    View Code

Field datatypes  字段的数据类型

  • 核心数据类型
    • 字符串类型: text(分词),keyword(不分词)一个用于全文检索,一个用于聚合和排序。
    • 数值型: long,integer,short,byte,double,float,half_float,scaled_float
    • 日期:date
    • 布尔:boolean
    • 二进制:binary
    • 范围类型:integer_range,float_range,long_range,double_range,date_range
  • 复杂数据类型
    • 数组  array
    • 对象  object
      PUT test_index
      {
        "mappings": {
          "doc": {
            "properties": {
              "obj":{              #obect类型字段
                "properties": {
                  "age":{
                    "type":"integer"
                   },
                  "name":{
                    "type":"text"
                  }
                }
              }
            }
          }
        }
      }
      PUT test_index/doc/1
      {
        "obj":[
          {
            "name":"alice white",
            "age":34
          },
          {
            "name":"peter brown",
            "age":26
          }
          ]
      }
      GET test_index/_search
      {
        "query": {
          "match": {
            "obj.name": "peter"
            }
        }
      }
      View Code
    • 嵌套类型  nested object
      PUT test_index
      {
        "mappings": {
          "doc": {
            "properties": {
              "man":{            #设置man字段为nested类型
                "type": "nested",  
                "properties": {
                  "age":{
                    "type":"integer"
                   },
                  "name":{
                    "type":"text"
                  }
                }}}}}}}
      PUT test_index/doc/1
      {
        "man":[
          {
            "name":"alice white",
            "age":34
          },
          {
            "name":"peter brown",
            "age":26
          }
          ]
      }
      # 嵌套类型的字段的查询和聚合:
      GET test_index/_search
      {
        "query": {       #查询
          "nested": {         #关键字
            "path": "man", 
            "query": {
              "match": {
                "man.name": "peter"  
              }
            }
          }
        },
        "size": 0, 
        "aggs": {
          "man": {   
            "nested": {    #聚合关键字
              "path": "man"
            },
            "aggs": {
              "avg_age": {
                "avg": {
                  "field": "man.age"
                }
              }
            }}}}
      View Code
  • 地理类型
    • geo_point
    • geo_shape
  • 专用类型
    • 记录ip地址 ip
    • 实现自动补全 completion
    • 记录分词数 token_count
    • 记录字符串hash值 murmur3
    • percolator
    • join

mapping参数:

  • dynamic 参数动态添加新字段
    • -true  允许自动将检测到的新字段加到映射中(默认的)
    • -false 不允许自动新增字段,文档可以写入,但无法对字段进行搜索等操作。不会添加在映射中。且在kibana上面看到的新字段上会显示一个黄色感叹号,刷新index pattern也无效。
    • -strict 文档不能写入,写入会报错
  • analyzer   指定分词器
  • ignore_above  超过ignore_above的字符串将不会被索引或存储
    PUT test_index
    {
      "mappings": {
        "doc":{
          "properties": {
            "message":{
              "type": "keyword",
              "ignore_above": 20  #字段值超过20个字符的字符串不会被索引或者存储
            }
          }
        }
      }
    }
    POST test_index/doc/_bulk
    {"index":{"_id":1}}
    {"message":"test message"}
    {"index":{"_id":2}}
    {"message":"test message  with some long stacktrace messages test test"}
    
    GET test_index/_search
    {
      "size": 0, 
      "aggs": {
        "message": {
          "terms": {
            "field": "message",
            "size": 10
          }
        }
      }
    }----------------------->只能得到第一个桶
          "buckets": [
            {
              "key": "test message",
              "doc_count": 1
            }
          ]
    
    GET test_index/_search    
    {
      "query": {
        "query_string": {
          "default_field": "message",
          "query": "*message*"
        }
      }
    }
    ------------->只能搜索到id为1的文档
      "hits": {
        "total": 1,
        "max_score": 1,
        "hits": [
          {
            "_index": "test_index",
            "_type": "doc",
            "_id": "1",
            "_score": 1,
            "_source": {
              "message": "test message"
            }
          }
        ]
    View Code
  • index    true | false  控制字段是否被索引,默认为true。
  • doc_values   本质是一个序列化的列式存储 。列式存储适用于聚合,排序,脚本操作。
    • true   默认对除了analyzed strings以外的所有字段开启。
    • false  不能用于聚合、排序和脚本操作
  • fields  对同一字段采用不同类型配置。比如string字段映射为text做全文搜索,映射为keyword做聚合和排序
    PUT test_index
    {
      "mappings": {
        "doc":{
          "properties": {
            "name":{
              "type": "text",     #text类型,用于全文检索
              "fields": {
                "keyword":{    #name.keyword
                  "type": "keyword"   #keyword类型,用于排序和聚合
                }
              }
            }
          }
        }
      }
    }
    PUT test_index/doc/1
    {
      "name":"Jack smis"
    }
    PUT test_index/doc/2
    {
      "name":"Jack"
    }
    GET test_index/_search
    {
      "query": {
        "match": {
          "name": "jack"      #text类型字段
        }
      },
      "sort": [
        {
          "name.keyword": {    #keyword类型字段
            "order": "desc"
          }
        }
      ],
      "aggs": {
        "name_count": {
          "value_count": {
            "field": "name.keyword"   #keyword类型字段
          }
        }
      }
    }
    View Code
  • properties  object字段或nested字段包含子字段,称为properties。properties可以是任何数据类型
    PUT test_index
    {
      "mappings": {
        "doc": {
          "properties": {
            "dev":{                #object类型字段
              "properties": {
                "name":{
                  "type":"text"
                },
                "age":{
                  "type":"integer"
                }
              }
            },
            "rel":{
              "type": "nested",    #nested类型字段
              "properties": {
                "age":{
                  "type":"integer"
                },
                "name":{
                  "type":"text"
                }
              }
            }
          }
        }
      }
    }
    PUT test_index/doc/1
    {
      "dev":{
        "name":"john smith",
        "age":23
      },
      "rel":[
        {
          "name":"alice white",
          "age":34
        },
        {
          "name":"peter brown",
          "age":26
        }
        ]
    }
    View Code
  •  norms   时间评分因子,如果不关心评分可以禁用

Dynamic mapping

es可以自动识别字段类型,依靠json文档的字段类型来实现自动识别。支持的类型如下:

json      es

null: 忽略

boolean:boolean

浮点数:float

整数:long

object:object

string:日期设为date类型(默认开启)。

            数字设为float或者long类型(默认关闭)。

            设为text类型,并附带keyword的子字段。

dynamic field mapping 动态字段映射

  •  date detection   日期检测   默认开启
    #当创建一个日期格式的文档时dynamic_date_formats为
    # ["strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
    PUT t-index/doc/1
    {
      "create_date":"2018/07/03"
    }
    --------------》
    {
      "t-index": {
        "mappings": {
          "doc": {
            "properties": {
              "create_date": {
                "type": "date",   #自动识别日期为date类型
                "format": "yyyy/MM/dd HH:mm:ss||yyyy/MM/dd||epoch_millis"
              }}}}}}
    
    #禁用日期检测 设置date_detection为false的情况
    PUT t-index
    {
      "mappings": {
        "doc":{
          "date_detection": false
        }
      }
    }
    PUT t-index/doc/2
    {
      "new_date":"2018/05/20"
    }
    GET t-index/_mapping
    --------------------------》
    {
      "t-index": {
        "mappings": {
          "doc": {
            "date_detection": false,
            "properties": {
              "new_date": {
                "type": "text",  #日期为text类型
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }}}}}}}}}
    View Code 
  •  dynamic_date_formats  自定义日期格式
    PUT t2-index
    {
      "mappings": {
        "doc": {
          "dynamic_date_formats": ["yyyy-MM-dd"]
        }
      }
    }
    PUT t2-index/doc/1
    {
      "create_time":"2018-07-03"
    }
    GET t2-index/_mapping
    --------------------》
    {
      "t2-index": {
        "mappings": {
          "doc": {
            "dynamic_date_formats": [
              "yyyy-MM-dd"
            ],
            "properties": {
              "create_time": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 256
                  }}}}}}}}
    View Code 
  • numeric_detection  默认不会自动识别字符串里面的数字。可以开启
    PUT t-index
    {
      "mappings": {
        "doc": {
          "numeric_detection": true   #打开数字检测
        }
      }
    }
    PUT t-index/doc/1
    {
      "my_float":"1.2",
      "my_integer":"1"
    }
    GET t-index/_mapping 
    
    --------------------》
    {
      "t-index": {
        "mappings": {
          "doc": {
            "numeric_detection": true,
            "properties": {
              "my_float": {
                "type": "float"  #自动检测1.2为float
              },
              "my_integer": {
                "type": "long"  #自动检测1为long
              }}}}}}
    View Code 

dyamic-templates  动态模板

 格式为:

"dynamic_templates": [  #数组,可以指定多个匹配规则
    {
      "my_template_name": {     #template名称
        "match_mapping_type":"string",   #匹配规则 
        "mapping":{    
          ...     #设置mapping信息
        }   
      }
    },
    ...
  ]
#举个栗子 以message开头的字段设为text类型,其他string设置成keyword
PUT test_index
{
  "mappings": {  
    "doc": {     # type
      "dynamic_templates":[  # 关键字
      {
        "message_as_text":{   #模板名称
          "match_mapping_type":"string",  #匹配条件
          "match":"message*",    #匹配message开头的字段
          "mapping":{      #设置mapping
            "type":"text" 
          }
        }
      },
      {
        "string_as_keyword":{      # 模板名称
          "match_mapping_type":"string",  #匹配条件
          "mapping":{          #设置mapping
            "type":"keyword",
       "ignore_above":256
} } }]}}}

匹配条件有:match_mapping_type, match, match_pattern, unmatch, path_match, path_unmatch.

  • match_mapping_type

            自动检测类型有 :boolean, date, double, long, object, string

  • PUT my_index
    {
      "mappings": {
        "doc":{
          "dynamic_templates":[
            {
              "my_string":{
                "match_mapping_type":"string",
                "mapping":{
                  "type":"text",
                  "fields":{
                    "keyword":{
                      "type":"keyword",
                      "ignore_above":265
                    }
                  }
                }
              }
            }
          ]
        }
      }
    }
    
    PUT my_index/doc/1
    {
      "name":"Jahn smis"
    }
    GET my_index/_mapping
    
    ------------------->
    
    {
      "my_index": {
        "mappings": {
          "doc": {
            "dynamic_templates": [
              {
                "my_string": {
                  "match_mapping_type": "string",
                  "mapping": {
                    "fields": {
                      "keyword": {
                        "ignore_above": 265,
                        "type": "keyword"
                      }
                    },
                    "type": "text"
                  }
                }
              }
            ],
            "properties": {
              "name": {
                "type": "text",
                "fields": {
                  "keyword": {
                    "type": "keyword",
                    "ignore_above": 265
                  }
                }
              }
            }
          }
        }
      }
    }
    View Code
  • match,unmatch 匹配字段名称  
    PUT my_index
    {
      "mappings": {
        "_doc": {
          "dynamic_templates": [
            {
              "longs_as_strings": {
                "match_mapping_type": "string",
                "match":   "long_*",
                "unmatch": "*_text",
                "mapping": {
                  "type": "long"
                }
              }
            }
          ]
        }
      }
    }
    View Code
  • path_match ,path_umatch
    PUT my_index
    {
      "mappings": {
        "_doc": {
          "dynamic_templates": [
            {
              "full_name": {
                "path_match":   "name.*",
                "path_unmatch": "*.middle",
                "mapping": {
                  "type":       "text",
                  "copy_to":    "full_name"
                }
              }
            }
          ]
        }
      }
    }
    View Code

Index Templates 索引模板

定义在创建新索引时自动应用的模板。模板包括setting和mapping和index_patterns

api:    

 GET _cat/templates
 PUT|GET|DELETE  _template/test_template

PUT _template/nginx-access
{
    "order": 0,   # order 设置优先级
    "index_patterns": [  # 匹配索引名称 
      "nginx-access*"   #以nginx-access开头的索引会应用该模板
    ], 
    "settings": {      # 索引setting配置
      "index": {
        "number_of_shards": "6",
        "refresh_interval": "5s"
      }
    },
    "mappings": {     #mapping配置 
      "doc": {        # type,可以去掉这个,7.x里面默认是_doc
        "date_detection": false,
        "dynamic_templates": [
          {
            "string_fields": {
              "match": "*",
              "match_mapping_type": "string",
              "mapping": {
                "type": "keyword",
                "norms": false,
                "ignore_above": 256
              }
            }
          }
        ],
        "properties": {
          "@timestamp": {
            "type": "date"
          },
          "@version": {
            "type": "keyword"
          },
          "url_args": {
            "type": "nested",
            "properties": {
              "key": {
                "type": "keyword"
              },
              "value": {
                "type": "keyword"
              }
            }
          }
      }
    }
  }
}


原文地址:https://www.cnblogs.com/xiaobaozi-95/p/9283950.html