1,mapping簡介
2,自定義 mapping
3,mapping演示
PUT my_index { "mappings": { "doc": { "properties": { "title": { "type": "text" }, "name": { "type": "keyword" }, "age": { "type": "integer" } } } } }
GET my_index/_mapping { "my_index": { "mappings": { "doc": { "properties": { "age": { "type": "integer" }, "name": { "type": "keyword" }, "title": { "type": "text" } } } } } }
PUT my_index/doc/1 { "name":"alfred a ", "age":9 } ---------------------------- { "_index": "my_index", "_type": "doc", "_id": "1", "_version": 2, "result": "updated", "_shards": { "total": 2, "successful": 1, "failed": 0 }, "_seq_no": 1, "_primary_term": 1 }
GET my_index/_search { "query":{ "match": { "name": "alfred" } } } ----------------------------------- { "took": 1, "timed_out": false, "_shards": { "total": 5, "successful": 5, "skipped": 0, "failed": 0 }, "hits": { "total": 1, "max_score": 0.2876821, "hits": [ { "_index": "my_index", "_type": "doc", "_id": "1", "_score": 0.2876821, "_source": { "name": "alfred", "age": 9 } } ] } }
DELETE my_index
4,自定義Mapping 之copy_to參數說明
5,自定義Mapping之index參數說明
6,自定義Mapping之index_options參數說明
7,mapping文檔說明
-------------------------------
8,數據類型
核心數據類型
字符串型:text、keyword(不會分詞)
數值型:long、integer、short、byte、double、float、half_float等
日期類型:date
布爾類型:boolean
二進制類型:binary
屬性名字 | 說明 |
text | 用於全文索引,該類型的字段將通過分詞器進行分詞,最終用於構建索引 |
keyword | 不分詞 |
long | 有符號64-bit integer:-2^63 ~ 2^63 - 1 |
integer | 有符號32-bit integer,-2^31 ~ 2^31 - 1 |
short | 有符號16-bit integer,-32768 ~ 32767 |
byte | 有符號8-bit integer,-128 ~ 127 |
double | 64-bit IEEE 754 浮點數 |
float | 32-bit IEEE 754 浮點數 |
half_float | 16-bit IEEE 754 浮點數 |
boolean | true,false |
date | https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-date-format.html |
binary | 該類型的字段把值當做經過 base64 編碼的字符串,默認不存儲,且不可搜索 |
范圍類型:integer_range、float_range、long_range、double_range、date_range
范圍類型例子:
標識一個數據范圍而不是一個值 如age:10~20 搜索{"gle":5,"lte":20} 則可以搜索出來數據
支持的數據類型 | 說明 |
integer_range |
|
float_range |
|
long_range |
|
double_range |
|
date_range |
64-bit 無符號整數,時間戳(單位:毫秒) |
ip_range |
IPV4 或 IPV6 格式的字符串 |
可選參數:
relation這只匹配模式
INTERSECTS 默認的匹配模式,只要搜索值與字段值有交集即可匹配到
WITHIN 字段值需要完全包含在搜索值之內,也就是字段值是搜索值的子集才搜索出來
CONTAINS 與WITHIN相反,只搜索字段值包含搜索值的文檔
例子:
1.添加index
put:127.0.0.1:9200/range_test
{
"mappings": { "_doc": { "properties": { "count": { "type": "integer_range" }, "create_date": { "type": "date_range", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } }
2.添加測試數據
post:127.0.0.1:9200/range_test/_doc/1
{
"count" : { "gte" : 1, "lte" : 100 }, "create_date" : { "gte" : "2019-02-1 12:00:00", "lte" : "2019-03-30" } }
3.測試搜索
get:127.0.0.1:9200/range_test/_doc/_search
{
"query":{ "term":{ "count":5 } } }
5在1~100之間可以搜索出來
{
"query" : { "range" : { "create_date" : { "gte" : "2019-02-01", "lte" : "2019-03-30", "relation" : "within" } } } }
復雜數據類型
數組類型:array 支持字符串 數值 object對象數組 數組元素必須為相同數據類型
對象類型:object
{ "name": "小明", "user_info": { "student_id": 111, "class_info": { "class_name": "1年級" } } }
被索引形式
{ "name":"小明", "user_info.student_id":"111", "user_info.student_info.class_name":"111" }
嵌套類型:nested object
能夠支持數組元素單獨的做索引
查詢api:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-nested-query.html
聚合api:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
排序api:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-reverse-nested-aggregation.html
檢索和高亮:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-inner-hits.html#nested-inner-hits
Nested和Object區別
put:127.0.0.1:9200/object_test/_doc/1 默認是object類型
{ "user_name":"小明", "subjects":[ {"subject_name":"地理","id":1}, {"subject_name":"英語","id":2} ] }
搜索名字為英語id為1的
{ "query":{ "bool":{ "must":[ {"match":{"subjects.subject_name":"英語"}}, {"match":{"subjects.id":"1"}} ] } } }
正常搜索不出來 測試時搜索出來了
因為索引為以下格式
{ "name":"小明", "subjects.subject_name":["英語","地理"], "subjects.subject_id":["1","2"] }
改為Nested 就不會
地理位置數據類型:geo_point、geo_shape
geo_point
幾種格式
object對象:"location": {"lat": 41.12, "lon": -71.34}
字符串:"location": "41.12,-71.34"
geohash:"location": "drm3btev3e86"
數組:"location": [ -71.34, 41.12 ]
geo_shape
查詢api:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-geo-bounding-box-query.html
專用類型:ip(記錄ip地址)、completion(實現自動補全)、token_count(記錄分詞數)、murmur3(記錄字符串hash值)
多字段特性
多字段特性(multi-fields),表示允許對同一字段采用不同的配置,比如分詞。
常見例子是對人名實現拼音搜索,只需要在人名中新增一個字段pinyin即可。但是這種方式不是十分優雅,multi-fields可以在不改變整體結構的前提下,增加一個子字段:
9,dynamic-mapping簡介
10,dynamic日期與數字識別
日期的自動識別可以自行配置日期的格式,默認情況下是:
["strict_date_opeional_time", "yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]
strict_date_opeional_time 是ISO 標准的日期格式,完整的格式如下:
YYYY-MM-DDhh:mm:ssTZD(eg:1997-07-16y19:20:30+01:00)
dynamic_date_formats:可以自定義日期類型
date_detection:可以關閉日期自動識別機制(默認開啟)
數字自動識別
字符串為數字的時候,默認不會自動識別為整型,因為字符串中出現數字是完全合理的。
numeric_detection 可以開啟字符串中數字的自動識別。
11,dynamic-template簡介
Dynamic Templates 意為 動態模板,它的作用是允許根據es自動識別的數據類型、字段名等來動態設定字段類型。
可以實現的效果如下:
- 所有字符串類型都設置為keyword類型,即默認不分詞
- 所有以message開頭的字段都設置為text類型,即分詞
- 所有以long_開頭的字段都設置為long類型
- 所有自動匹配為double類型的都設定為float類型,以節省空間
匹配規則參數
- match_mapping_type:匹配es自動識別的字段類型,如boolean、long等
- match、unmatch:匹配字段名
- path_match、path_unmatch:匹配路徑
舉例
字段類型匹配
首先PUT一個文檔,然后查看mapping:
PUT test_index/doc/1 { "name": "Tom" } GET test_index/_mapping
可以看到在默認情況下,字符串被識別成為text類型,並且有一個子字段keyword。
現在設置動態模板,要求匹配到string類型的字段設置為keyword:
PUT test_index { "mappings": { "doc": { "dynamic_templates": [ { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] } }
重新創建文檔並查看mapping:
name字段的類型變成了 keyword類型
字段匹配
現在想將以message開頭的字段且為string的匹配稱為text類型,其余為keyword:
PUT test_index { "mappings": { "doc": { "dynamic_templates": [ { "message_as_text": { "match_mapping_type": "string", "match": "message", "mapping": { "type": "text" } } }, { "strings_as_keywords": { "match_mapping_type": "string", "mapping": { "type": "keyword" } } } ] } } }
Dynamic Templates 的匹配順序是從上到下執行的,匹配到一個后后面的規則就會跳過
然后創建一個文檔並查看mapping:
PUT test_index/doc/1 { "name": "john", "message": "good boy" } GET test_index/_mapping
可以看到message被設置為了text類型,name還是keyword
double設定為float
12,自定義mapping的建議
一般步驟
自定義mapping 的步驟:
寫一條文檔到es的臨時索引中,獲取es自動生成的mapping
修改第一步得到的mapping,自定義相關配置
使用第2步的mapping創建實際的索引
實際舉例
假設我得到了需要存入es的文檔,首先將文檔寫入臨時的index中:
實際舉例
假設我得到了需要存入es的文檔,首先將文檔寫入臨時的index中:
PUT test_index/doc/1 { "referre": "-", "response_code": "200", "remote_ip": "172.0.0.1", "method": "POST", "username": "-", "http_version": "1.1", "body_sent": { "bytes": "0" }, "url": "/helloworld" }
然后查看es自動生成的mapping:
GET test_index/_mapping
現在希望將bytes設置為整型,url設置為text類型,其他都使用keyword(將上一步的輸出復制過來就好):
PUT product_index { "mappings": { "doc": { "properties": { "body_sent": { "properties": { "bytes": { "type": "long" } } }, "http_version": { "type": "keyword" }, "method": { "type": "keyword" }, "referre": { "type": "keyword" }, "remote_ip": { "type": "keyword" }, "response_code": { "type": "keyword" }, "url": { "type": "text" }, "username": { "type": "keyword" } } } } }
這樣直接將測試index的mapping復制過來進行修改,不會遺漏字段,修改完成設置一個index的名稱就行了。
然后就可以向實際的索引中寫入文檔了:
PUT product_index/doc/1 { "referre": "-", "response_code": "200", "remote_ip": "172.0.0.1", "method": "POST", "username": "-", "http_version": "1.1", "body_sent": { "bytes": "0" }, "url": "/helloworld" }
然后查看一下實際索引的mapping:
GET product_index/_mapping
13,索引模板
什么是索引模板
索引模板,index template,主要用於在新建索引時自動應用預先設置的配置,簡化索引創建的步驟。
模板中可以設定索引的配置以及mapping,可以有多個模板,根據order設置,order大的覆蓋小的范圍。
模板加載順序根據 order 從小到大加載,后面的大order的模板的配置將會覆蓋小 order配置。
獲取與刪除的API 如下:
舉例
這里設置了兩個索引模板:
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 } } } }
然后先創建一個foo_index,並獲取一下它的mapping:
PUT foo_index
GET foo_index/_mapping
因為這個index沒有被任何一個模板匹配到,所以它的mapping是空的
再創建一個bar_index,並獲取一下它的mapping:
PUT bar_index
GET bar_index/_mapping
這個索引匹配到了test_template
再創建一個test_index,並獲取一下它的index配置:
PUT test_index
GET test_index/
這個索引匹配到了test_template2模板,這使得索引的”_source”: {“enabled”: true}