索引的mapping
mapping類似于ddl,創(chuàng)建索引的時(shí)候,可以預(yù)先指定字段的類型及相關(guān)屬性民逼。
#索引名/_mapping
GET /lib1/_mapping
{
"lib1" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "long"
},
"doc" : {
"properties" : {
"sex" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"sex" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
1. 核心數(shù)據(jù)類型
String(text、keyword)
text:
text類型被用來(lái)索引長(zhǎng)文本涮帘,在建立索引前會(huì)對(duì)這些文本進(jìn)行分詞拼苍,轉(zhuǎn)化為詞的組合,建立索引调缨。允許es來(lái)檢索這些詞語(yǔ)疮鲫。text類型不能用來(lái)排序和聚合。
keyword:
不需要進(jìn)行分詞弦叶】》福可以被原來(lái)檢索過(guò)濾,排序和聚合伤哺。keyword類型字段只能用本身來(lái)進(jìn)行檢索燕侠。
數(shù)字類型:long者祖、integer、short绢彤、byte七问、double、float
日期類型:date
布爾類型:boolean
二進(jìn)制: binary
2. 復(fù)雜數(shù)據(jù)類型
-
數(shù)組類型:不需要專門(mén)指定數(shù)組元素的type
- 字符型數(shù)組:["one" , "two"]
- 整型數(shù)組:[1 , 2, 3]
- 數(shù)組型數(shù)組:[1 , [2, 3]] 等價(jià)于 [1, 2, 3]
- 對(duì)象數(shù)組:[{"name" : "xiaochao", "age":22},{"name" : "xiaoyi", "age":20}]
對(duì)象類型(object datatype):_ object _ 用于單個(gè)json對(duì)象
嵌套類型(nested datatype):_ nested _ 用于對(duì)象數(shù)組
3. 地理位置類型(Geo datatypes)
地理位置類型(Geo-point datatype):_ geo-point _ 用于經(jīng)緯度坐標(biāo)
地理位置類型(Geo-shape datatype):_ geo-shape _ 用于類似多邊形的復(fù)雜形狀
1. 創(chuàng)建不同的基本類型字段文檔
#老版本在索引后還有一級(jí)type
#POST /myindex/article/1
POST /myindex/_doc/article1
{
"post_date":"2019-05-10", //注意,日期只能是年-月-日茫舶,不能有時(shí)分秒
"tile":"java",
"content":"java is the best language",
"id":119,
"test":["hello","world"]
}
POST /myindex/_doc/article2
{
"post_date":"2019-06-10", //注意,日期只能是年-月-日械巡,不能有時(shí)分秒
"tile":"html",
"content":"I like html",
"id":120,
"test":["html","good"]
}
POST /myindex/_doc/article3
{
"post_date":"2019-06-15", //注意,日期只能是年-月-日,不能有時(shí)分秒
"tile":"es",
"content":"es is a new tool",
"id":121,
"test":["elasticsearch","very good"]
}
2. 通過(guò)映射 _mapping 查看數(shù)據(jù)類型
#老版本: GET /索引/type/_mapping
GET /myindex/_mapping
{
"myindex" : {
"mappings" : {
"properties" : {
"content" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"id" : {
"type" : "long"
},
"post_date" : {
"type" : "date"
},
"test" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"tile" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
3. 通過(guò) _search 查看索引下的所有文檔
//
#老版本: GET /索引/type/_search
GET /myindex/_search
{
"took" : 4, #查詢耗時(shí)4毫秒
"timed_out" : false, # 是否超時(shí)
"_shards" : {
"total" : 1, #分片數(shù)
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "article1",
"_score" : 1.0, # 查出來(lái)的文檔和查詢條件的匹配度
"_source" : {
"post_date" : "2019-05-10",
"tile" : "java",
"content" : "java is the best language",
"id" : 119,
"test" : [
"hello",
"world"
]
}
},
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "article2",
"_score" : 1.0, # 查出來(lái)的文檔和查詢條件的匹配度
"_source" : {
"post_date" : "2019-06-10",
"tile" : "html",
"content" : "I like html",
"id" : 120,
"test" : [
"html",
"good"
]
}
},
{
"_index" : "myindex",
"_type" : "_doc",
"_id" : "article3",
"_score" : 1.0, # 查出來(lái)的文檔和查詢條件的匹配度
"_source" : {
"post_date" : "2019-06-15",
"tile" : "es",
"content" : "es is a new tool",
"id" : 121,
"test" : [
"elasticsearch",
"very good"
]
}
}
]
}
}
4. 按條件檢索:_search?q=column:val
//按條件檢索
#查不到數(shù)據(jù)奇适,因?yàn)闆](méi)有對(duì)日期類型數(shù)據(jù)進(jìn)行分詞
GET /myindex/_search?q=post_date:2019
#能查出數(shù)據(jù)
GET /myindex/_search?q=post_date:2019-06-15
#能查出數(shù)據(jù)坟比,因?yàn)閏ontent是text類型,有進(jìn)行分詞,排序
GET /myindex/_search?q=content:html&sort=post_date:desc
5. object類型及底層存儲(chǔ)格式
//
PUT /lib5/_doc/person1
{
"name":"Tom",
"age":25,
"birthday":"1985-12-12",
"address":{
"country":"china",
"province":"guangdong",
"city":"shenzhen"
}
}
GET /lib5/_mapping
#底層存儲(chǔ)格式
{
"name":["Tom"],
"age":[25],
"birthday":["1985-12-12"],
"address.country":["china"],
"address.province":["guangdong"],
"address.city":["shenzhen"]
}
}
PUT /lib5/_doc/persons1
{
"persons":[
{"name":"lisi","age":27},
{"name":"wangwu","age":26},
{"name":"zhaoliu","age":23}
]
}
GET /lib5/_mapping
#底層存儲(chǔ)
{
"persons.name":["lisi","wangwu","zhaoliu"],
"persons.age"[27,26,23]
}
6. 手動(dòng)創(chuàng)建mapping
//手動(dòng)創(chuàng)建mapping
DELETE lib5
#手動(dòng)創(chuàng)建mapping
PUT /lib5
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 0
},
"mappings": {
"properties": {
"age" : {"type" : "long"},
"birthday" : {"type" : "date","index": false}, #不希望建立倒排索引
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"content":{"type":"text","analyzer": "standard"},
"price":{"type": "double"},
"number":{"type": "integer"}
}
}
}
GET /lib5/_mapping