一 自定義mapping
- Mapping類似於資料庫中表定義,定義Index欄位名,欄位型別及倒排索引相關配置;
- Mapping中欄位型別一旦設定後,禁止修改,若修改,重新建立新索引,進行reindex操作;
- 通過dynamic引數控制欄位新增,引數有true/false/strict;
##dynamic控制欄位新增
##true:預設允許自動新增欄位; false:不允許自動新增欄位,文件可以正常寫入,但是無法對該欄位進行查詢;strict:文件不能寫入;
PUT my_index
{
"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,world",
"desc":"nothing here" ##dynamic為false,可以新增文件,但是不能查詢該欄位;
}
二 常用引數設定
- copy_to
將該欄位的值複製到目標欄位,不會出現在_source中,只用來搜尋;
PUT my_index
{
"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": "John",
"last_name": "Smith"
}
GET my_index/_search
{
"query": {
"match": {
"full_name": { ##full_name中同時包含John和Smith
"query": "John Smith",
"operator": "and"
}
}
}
}
- index
控制當前欄位是否時索引,預設為true,即記錄索引,false不記錄,即不可搜尋;
PUT my_index
{
"mappings": {
"doc": {
"properties": {
"cookie": {
"type": "text",
"index": false
}
}
}
}
}
PUT my_index/doc/1
{
"cookie":"name=alfred"
}
GET my_index/_search
{
"query":{
"match": {
"cookie": "name" ##Cannot search on field [cookie] since it is not indexed
}
}
}
- index_options
用於控制倒排索引記錄的內容;
docs: 記錄doc id
freqs: 記錄doc id 和 term frequencies
positions: 記錄doc id/term frequencies/term position
offsets: 記錄doc id/term frequencies/term position/character offsets
- null_value
當欄位遇到null值時的處理策略,預設為null,即空值,es會自動忽略,可以通過設定該欄位的預設值;
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"status_code": {
"type": "keyword",
"null_value": "NULL"
}
}
}
}
}
PUT my_index/my_type/1
{
"status_code": null
}
PUT my_index/my_type/2
{
"status_code": []
}
GET my_index/_search
{
"query": {
"term": {
"status_code": "NULL"
}
}
}
三 資料型別
字串型: 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
巢狀型別: nested object
geo_point
geo_shape
記錄ip地址: ip
實現自動補全: completion
記錄分詞數: token_count
記錄字串hash值: murmur3
percolator
join
允許對用一個欄位採用不同的配置,比如分詞,常見例子如對任命實現拼音搜尋,僅需再人名中增加pinyin子欄位即可;
四 Dynamic Mapping
dynamic_date_formats:自定義日期型別
PUT my_index
{
"mappings":{
"my_type":{
"dynamic_date_formats":["MM/dd/yyyy"]
}
}
}
不定義mapping,新增文件後的日期格式識別為text型別
numeric_detection: 開啟字串中數字自動識別
##將字串中的數字識別為數值型別
PUT my_index
{
"mappings":{
"my_type":{
"numeric_dectection":true
}
}
}
PUT my_index/my_type/1
{
"my_float":"1.0",
"my_integer":"1"
}
允許根據es自動識別的資料型別/欄位名等來動態設定欄位型別;
##match_mapping_type匹配自動識別欄位型別,match,unmatch匹配欄位名,path_match,path_unmath匹配路徑
PUT my_product_index
{
"mappings": {
"doc": {
"dynamic_templates": [
{
"strings": {
"match_mapping_type": "string",
"mapping": {
"type": "keyword"
}
}
}
]
}
五 Index Template
- 用於在新建索引時自動應用預先設定的配置,簡化所以建立,若有多個模板,根據order大小,大的會覆蓋掉小的;
PUT _template/test_template
{
"index_patterns": ["te*", "bar*"],
"order":0,
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": false
},
"properties": {
"name": {
"type": "keyword"
}
}
}
}
}
PUT _template/test_template2
{
"index_patterns": ["test*"],
"order":1,
"settings": {
"number_of_shards": 1
},
"mappings": {
"doc": {
"_source": {
"enabled": true
}
}
}
}
PUT test_index
GET test_index/ ##order大的會覆蓋小的