Mapping 可以:
- 定義 Index 下的字段名(Field Name)
- 定義字段的類型,比如數值型、字符型、布爾型
- 定義倒排索引相關的配置,比如是否索引、記錄position等
GET kibana_sample_data_flights
{
"kibana_sample_data_flights" : {
"aliases" : { },
"mappings" : {
"properties" : {
"AvgTicketPrice" : {
"type" : "float"
},
"Cancelled" : {
"type" : "boolean"
},
"Carrier" : {
"type" : "keyword"
},
"Dest" : {
"type" : "keyword"
},
"DestAirportID" : {
"type" : "keyword"
},
"DestCityName" : {
"type" : "keyword"
},
...
}
},
"settings" : {
"index" : {
"number_of_shards" : "1",
"auto_expand_replicas" : "0-1",
"provided_name" : "kibana_sample_data_flights",
"creation_date" : "1574647860171",
"number_of_replicas" : "0",
"uuid" : "AagbcCRyTKSJepOGo1_DKQ",
"version" : {
"created" : "7040299"
}
}
}
}
}
類型的自動識別
JSON datatype | Elasticsearch datatype |
---|---|
空值 null | 沒有字段添加 |
布爾值 true or false | boolean |
整數 | long |
對象 | object |
數組 | 由第一個非空數值的類型所決定 |
字符串 | - 匹配日期格式,設置爲 Date - 匹配數字設置爲 double或long字段,該選縣默認關係 - 設置爲Text,並且增加 keyword 子字段 |
浮點數 | float |
數字用引號,默認當 text;日期格式會推到成 Date;有些類型會推導錯誤,比如日期類型。
#寫入文檔,查看 Mapping
PUT mapping_test/_doc/1
{
"firstName":"Chan",
"lastName": "Jackie",
"loginDate":"2018-07-24T10:29:48.103Z"
}
#查看 Mapping文件
GET mapping_test/_mapping
#Delete index
DELETE mapping_test
#dynamic mapping,推斷字段的類型
PUT mapping_test/_doc/1
{
"uid" : "123",
"isVip" : false,
"isAdmin": "true",
"age":19,
"heigh":180
}
#查看 Dynamic
GET mapping_test/_mapping
#默認Mapping支持dynamic,寫入的文檔中加入新的字段
PUT dynamic_mapping_test/_doc/1
{
"newField":"someValue"
}
如何顯示定義 Mapping
#設置 index 爲 false
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "text",
"index": false 控制當前字段是否被索引,默認爲true,爲false表示該字段不可被搜索
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming",
"mobile": "12345678"
}
POST /users/_search
{
"query": {
"match": {
"mobile":"12345678"
}
}
}
#設定Null_value
- 需要對 Null 值實現搜索
- 只有 Keyword 類型支持設定 null_value
DELETE users
PUT users
{
"mappings" : {
"properties" : {
"firstName" : {
"type" : "text"
},
"lastName" : {
"type" : "text"
},
"mobile" : {
"type" : "keyword",
"null_value": "NULL"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming",
"mobile": null
}
PUT users/_doc/2
{
"firstName":"Ruan2",
"lastName": "Yiming2"
}
GET users/_search
{
"query": {
"match": {
"mobile":"NULL"
}
}
}
# 設置 Copy to
- _all 在 7 中被 copy_to 所代替
- 滿足一些特定的搜索需求
- copy_to 將字段的數值拷貝到目標字段,實現類似 _all 的作用
- copy_to 的目標字段不出現在 _source 中
DELETE users
PUT users
{
"mappings": {
"properties": {
"firstName":{
"type": "text",
"copy_to": "fullName"
},
"lastName":{
"type": "text",
"copy_to": "fullName"
}
}
}
}
PUT users/_doc/1
{
"firstName":"Ruan",
"lastName": "Yiming"
}
使用 copy_to 之後就可以用 fullName 進行查詢
GET users/_search?q=fullName:(Ruan Yiming)
POST users/_search
{
"query": {
"match": {
"fullName":{
"query": "Ruan Yiming",
"operator": "and"
}
}
}
}
# 數組類型
Elasticsearch 中不提供專門的數組類型。但是任何字段,都可以包含多個相同類型的數值
PUT users/_doc/1
{
"name":"onebird",
"interests":"reading"
}
PUT users/_doc/1
{
"name":"twobirds",
"interests":["reading","music"]
}
POST users/_search
{
"query": {
"match_all": {}
}
}
GET users/_mapping
類型及範圍說明
下面這些表格摘自《從Lucene到Elasticsearch》