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"
              }
            }
          }
      }
    }
  }
}

 



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM