Elasticsearch篇之Mapping設置

Elasticsearch篇之Mapping設置

Mapping簡介

類似數據庫中的表結構定義,主要作用如下:

  • 定義Index下的字段名(Field Name)
  • 定義字段的類型,比如數值型、字符串型、布爾型等
  • 定義倒排索引相關的配置,比如是否索引、記錄position等
GET books/_mapping

展示效果
在這裏插入圖片描述

自定義Mapping

在這裏插入圖片描述

PUT my_index
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "properties": {
        "title":{
          "type": "text"
        },
        "name": {
          "type": "keyword"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}

在這裏插入圖片描述
在這裏插入圖片描述

  • Mapping中的字段類型一旦設定後,禁止直接修改,原因如下
    • Lucene實現的倒排索引生成後不允許修改
  • 重新建立新的索引,然後做reindex操作
  • 允許新增字段
    • 通過dynamic參數來控制字段的新增
    • true(默認)允許自動新增字段
    • false不允許自動新增字段,但是文檔可以正常寫入,但無法對字段進行查詢等操作
    • strict文檔不能寫入,報錯
PUT my_index
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "dynamic": false, 
      "properties": {
        "title":{
          "type": "text"
        },
        "name": {
          "type": "keyword"
        },
        "age": {
          "type": "integer"
        }
      }
    }
  }
}
GET my_index/_mapping
PUT my_index/doc/1
{
  "title": "hello word",
  "age": 21,
  "name": "wf"
}
PUT my_index/doc/2
{
  "title": "hello word",
  "age": 21,
  "name": "ls",
  "address": "china"
}

直接使用elasticsearch-head插件進行數據的查看
在這裏插入圖片描述
進行查詢對比
在這裏插入圖片描述
在這裏插入圖片描述
“dynamic”: "strict"的時候在插入文檔時會出現如下
在這裏插入圖片描述

copy_to參數說明

  • 將該字段的值複製到目標字段,實現類似_all的作用
  • 不會出現在_source中,只用來搜索
PUT my_index
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "properties": {
        "first_name":{
          "type": "text",
          "copy_to": "full_name"
        },
        "last_name": {
          "type": "text",
          "copy_to": "full_name"
        },
        "full_name": {
          "type": "text"
        }
      }
    }
  }
}

在這裏插入圖片描述
在這裏插入圖片描述
增加一條數據

PUT my_index/doc/1
{
  "first_name": "張",
  "last_name": "三"
}

在這裏插入圖片描述

查詢數據(full_name爲張三,必須同時滿足)

GET my_index/_search
{
  "query": {
    "match": {
      "full_name": {
        "query": "張三",
        "operator": "and"
      }
    }
  }
}

在這裏插入圖片描述

Index參數說明

在這裏插入圖片描述

PUT my_index1
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "properties": {
        "cookie":{
          "type": "text",
          "index": false
        }
      }
    }
  }
}
PUT my_index1/doc/1
{
  "cookie": "cookie-name-test"
}
GET my_index1/_search
{
  "query": {
    "match": {
      "cookie": "name"
    }
  }
}

查詢結果
在這裏插入圖片描述
身份證號 手機號 密碼 可以設置爲不檢索

index_option

設置和之前index類似,在字段下面有個index_option

  • index_option用於控制倒排索引記錄的內容,有如下4種配置
    • docs只記錄doc id
    • freqs 記錄doc id和term frequencies
    • positions 記錄doc id、term frequencies和term position
    • offsets 記錄doc id、term frequencies、term position和character offsets
  • text類型默認配置positions,其他默認爲docs
  • 記錄內容越多,佔用空間越大

null_value

設置和之前index類似,在字段下面有個null_value

  • 當字段遇到null值時的處理策略,默認爲null,即空值,此時es會忽略改值。可以通過設定改值設定字段的默認值

Mapping官方文檔

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-params.html

數據類型

https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html

  • 核心數據類型
    • 字符串型text, keyword
    • 數值型long, integer, short, byte, double, float, halffloat, scaled_float
    • 日期類型date
    • 布爾類型boolean
    • 二進制類型binary
    • 範圍類型integer_range, float_range, long_range, double_range, date_range
  • 複雜數據類型
    • 數組類型array
    • 對象類型object
    • 嵌套類型nested object
  • 地理位置數據類型
    • geo_point
    • geo_shape
  • 專用類型
    • 記錄ip地址ip
    • 實現自動補全completion
    • 記錄分詞數token_count
    • 記錄字符串hash值murmur3
    • percolator
    • join
  • 多字段特性multi-fields
    • 允許對同一個字段採用不同的配置,比如分詞,常見例子如對人名實現拼音搜索,只需要在人名中新增一個子字段爲pinyin即可。
      在這裏插入圖片描述

Dynamic Mapping簡介

es可以自動識別文檔字段類型,從而降低用戶使用成本,如下所示

PUT my_index2/doc/1
{
  "username": "zs",
  "age": 1
}
GET my_index2/_mapping

在這裏插入圖片描述
es是依靠JSON文檔的字段類型來實現自動識別字段類型,支持的類型如下:
在這裏插入圖片描述

PUT my_index2/doc/1
{
  "username": "zs",
  "age": 14,
  "birth": "1949-10-01",
  "married": false,
  "year": "2020",
  "tags": ["boy","fashion"],
  "money": 999.999
}
GET my_index2/_mapping

在這裏插入圖片描述

dynamic日期與數字識別

日期

日期的自動識別可以自行配置日期格式,以滿足各種需求
默認是:["strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z|lyyyy/MM/dd Z"]
strict_date_optional_time是ISO datetime的格式,完整格式類似:YYYY-MM-DDThh:mm:ssTZD (eg 2020-01-11T14:30:30+01:00)
dynamic_date_formats可以自定義日期類型
date_detection可以關閉日期自動識別的機制
這兩個參數直接在type下面做
自定日期識別格式

PUT my_index2
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "dynamic_date_formats": ["MM/dd/yyyy"]
    }
  }
}
PUT my_index2/doc/1
{
  "create_date": "11/01/2020"
}
GET my_index2/_mapping

在這裏插入圖片描述
關閉日期自動識別機制

PUT my_index2
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "date_detection": false
    }
  }
}
PUT my_index2/doc/1
{
  "create_date": "11/01/2020"
}
GET my_index2/_mapping

在這裏插入圖片描述

數字

字符串是數字是,默認不會自動識別問整型,因爲字符串中出現數字是完全合理的
numeric_detection可以開啓字符串中數字的自動識別

PUT my_index
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "numeric_detection": true
    }
  }
}

PUT my_index/doc/1
{
  "my_float": "99.99",
  "my_int": "100"
}
GET my_index/_mapping

在這裏插入圖片描述

dynamic-template簡介

允許根據es自動識別的數據類型、字段名、等動態設定字段類型,可以實現如下效果:

  • 所有字符串類型都設定爲keyword,即默認不分詞

  • 所有以message_開頭的字段都設定爲text類型,即分詞

  • 所有以long_開頭的字段都設定爲long類型

  • 所有自動匹配的double類型都設定爲float類型,以節省空間
    在這裏插入圖片描述
    匹配規則一般有如下幾個參數:

  • match_mapping_type匹配es自動識別的字段類型,如boolean、long、string等

  • match、unmatch匹配字段名

  • path_match、path_unmatch匹配路徑

字符串默認使用keyword類型
以message_開頭的字段都設置爲text類型
double類型設定爲float,節省空間

PUT my_index/doc/1
{
  "name": "張三 ",
  "message_sex": "男",
  "message_address": "中國北京",
  "gpa": 9999.123456
}
GET my_index/_mapping

在這裏插入圖片描述

PUT my_index
{
  "settings": 
  {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
      "dynamic_templates": [
        {
          "message_as_text": {
          "match_mapping_type": "string",
          "match": "message_*",
          "mapping": {
              "type": "text"
            }
          }
        },
        {
          "string_as_keywords": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "keyword"
            }
          }
        },
        {
          "double_as_float": {
          "match_mapping_type": "double",
          "mapping": {
              "type": "float"
            }
          }
        }
        
      ]
    }
  }
}
PUT my_index/doc/1
{
  "name": "張三 ",
  "message_sex": "男",
  "message_address": "中國北京",
  "gpa": 9999.123456
}
GET my_index/_mapping

在這裏插入圖片描述

自定義mapping的建議

自定義Mapping的操作步驟如下:

  • 寫入一條文檔到es的臨時索引中,獲取es自動生成的mapping
  • 修改步驟1得到的mapping,自定義相關配置
  • 使用步驟2的mapping創建實際所需索引
DELETE my_index
PUT my_index/doc/1
{
  "referrer": "-",
  "response_code": "200",
  "remote_ip": "127.0.0.1",
  "method": "POST",
  "user_name": "-",
  "http_version": "1.1",
  "body_sent": {
    "bytes": "0"
  },
  "url": "/test"
}
GET my_index/_mapping
PUT my_index
{
  "settings": {
    "number_of_shards": "5",
    "number_of_replicas": "0"
  },
  "mappings": {
    "doc": {
       "properties": {
          "body_sent": {
            "properties": {
              "bytes": {
                "type": "long"
              }
            }
          },
          "http_version": {
            "type": "keyword"
          },
          "method": {
            "type": "keyword"
          },
          "referrer": {
            "type": "keyword"
          },
          "remote_ip": {
            "type": "keyword"
          },
          "response_code": {
            "type": "keyword"
          },
          "url": {
            "type": "text"
          },
          "user_name": {
            "type": "keyword"
          }
      }
    }
  }
}

在這裏插入圖片描述
可以使用dynamic-template設置text爲keyword然後其他字段再單獨設置

索引模板

索引模板,英文爲Index Template,主要用於在新建索引時自動應用預先設定的配置,簡化索引創建的操作步驟

  • 可以設定索引的配置和mapping
  • 可以有多個模板,根據order設置,order大的覆蓋小的配置

在這裏插入圖片描述
獲取與刪除的API如下

  • GET _template
  • GET _template/test_template
  • DELETE _template/test_template
GET _template
PUT _template/test_template
{
  "index_patterns": ["te*", "bar*"],
  "order": 0,
  "settings": {
    "number_of_shards": 3,
    "number_of_replicas": 0
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": false
      },
      "properties": {
        "name": {
          "type": "keyword"
        }
      }
    }
  }
}

PUT _template/test_template2
{
  "index_patterns": ["test*"],
  "order": 1,
  "settings": {
    "number_of_shards": 1, 
    "number_of_replicas": 0
  },
  "mappings": {
    "doc": {
      "_source": {
        "enabled": true
      },
      "properties": {
        "name": {
          "type": "text"
        }
      }
    }
  }
}
PUT foo_index
GET foo_index

PUT bar_index
GET bar_index

PUT test_index
GET test_index

在這裏插入圖片描述
在這裏插入圖片描述
在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章