包含的主要內(nèi)容: ES的數(shù)據(jù)類(lèi)型书幕,Mapping的基本使用新荤,如何使用復(fù)雜數(shù)據(jù)類(lèi)型
1、ES數(shù)據(jù)類(lèi)型總覽
1. 核心數(shù)據(jù)類(lèi)型
(1)字符串類(lèi)型: text, keyword
(2)數(shù)字類(lèi)型:long
, integer
, short
, byte
, double
, float
, half_float
, scaled_float
①long -- 帶符號(hào)的64位整數(shù)台汇,最小值-2^63苛骨,最大值2^63-1
②integer -- 帶符號(hào)的32位整數(shù),最小值-2^31苟呐,最大值2^31-1
③short -- 帶符號(hào)的16位整數(shù)痒芝,最小值-32768,最大值32767
④byte -- 帶符號(hào)的8位整數(shù)牵素,最小值-128严衬,最小值127
⑤double -- 雙精度64位IEEE 754 浮點(diǎn)數(shù)
⑥float -- 單精度32位IEEE 754 浮點(diǎn)數(shù)
⑦h(yuǎn)alf_float -- 半精度16位IEEE 754 浮點(diǎn)數(shù)
⑧scaled_float -- 帶有縮放因子的縮放類(lèi)型浮點(diǎn)數(shù)
(3)日期:date
(4)日期 納秒:date_nanos
(5)布爾型:boolean
(6)二進(jìn)制:binary
(7)范圍: integer_range
, float_range
, long_range
, double_range
, date_range
2. 復(fù)雜數(shù)據(jù)類(lèi)型
(1)Object: object( 用于單個(gè)JSON對(duì)象 )
(2)Nested: nested
( 用于JSON對(duì)象數(shù)組 )
3. 地理數(shù)據(jù)類(lèi)型
(1)Geo-point: geo_point ( 緯度/經(jīng)度 )
(2)Geo-shape: geo_shape ( 用于多邊形等復(fù)雜形狀 )
4. 特殊數(shù)據(jù)類(lèi)型
(1)IP: ip (IPv4 和 IPv6 地址)
(2)Completion類(lèi)型:completion ( 提供自動(dòng)完成建議 )
(3)Token count:token_count ( 計(jì)算字符串中token的數(shù)量 )
(4)mapper-murmur3:murmur3( 在索引時(shí)計(jì)算值的哈希并將其存儲(chǔ)在索引中 )
(5)mapper-annotated-text:annotated-text ( 索引包含特殊標(biāo)記的文本(通常用于標(biāo)識(shí)命名實(shí)體) )
(6)Percolator:(Accepts queries from the query-dsl)
(7)Join:( 為同一索引內(nèi)的文檔定義父/子關(guān)系 )
(8)Alias:( 為現(xiàn)有字段定義別名)
(9)Rank feature:(Record numeric feature to boost hits at query time.)
(10)Rank features:(Record numeric features to boost hits at query time.)
(11)Dense vector:(Record dense vectors of float values.)
(12)Sparse vector:(Record sparse vectors of float values.)
(13)Search-as-you-type:(A text-like field optimized for queries to implement as-you-type completion)
(14) Flattened : Allows an entire JSON object to be indexed as a single field.
(15) Shape : shape
for arbitrary cartesian geometries.
(16) Histogram : histogram
for pre-aggregated numerical values for percentiles aggregations.
5.數(shù)組類(lèi)型
? 在Elasticsearch中,數(shù)組不需要一個(gè)特定的數(shù)據(jù)類(lèi)型笆呆,任何字段都默認(rèn)可以包含一個(gè)或多個(gè)值请琳,當(dāng)然,這多個(gè)值都必須是字段指定的數(shù)據(jù)類(lèi)型赠幕。
6.Multi-fields
? Multi-fields 通常用來(lái)以不同的方式或目的索引同一個(gè)字段俄精。比如,一個(gè)字符串類(lèi)型字段可以同時(shí)被映射為 text 類(lèi)型以用于全文檢索榕堰、 keyword字段用于排序或聚合嘀倒。又或者,你可以用standard分析器、english分析器和french分析器來(lái)索引同一個(gè) text字段测蘑。
2、查看Mapping
GET /index/_mapping
3康二、手動(dòng)創(chuàng)建Mapping
只能創(chuàng)建index時(shí)手動(dòng)建立mapping碳胳,或者新增field mapping,但是不能update field mapping
PUT /website
{
"mappings": {
"properties": {
"author_id": {
"type": "long"
},
"title": {
"type": "text",
"analyzer": "english"
},
"content": {
"type": "text"
},
"post_date": {
"type": "date"
},
"publisher_id": {
"type": "keyword"
}
}
}
}
4沫勿、添加field并指定mapping
PUT /website/_mappings
{
"properties": {
"new_field": {
"type": "text",
"analyzer": "english"
}
}
}
##查看一下是否新增了
GET website/_mapping
## 存入數(shù)據(jù)
POST /website/_doc
{
"author_id":123123123,
"title":"english dog",
"content":"123",
"post_date":"2020-03-22",
"publisher_id":123
}
5挨约、測(cè)試Mapping的分詞
GET /website/_analyze
{
"field": "content",
"text": "my-dogs"
}
GET website/_analyze
{
"field": "new_field",
"text": "my dogs"
}
##未分詞的類(lèi)型
GET /website/_analyze
{
"field": "publisher_id",
"text": "my-dogs"
}
6、復(fù)雜數(shù)據(jù)類(lèi)型(object)
PUT /company/_doc/1
{
"address": {
"country": "china",
"province": "guangdong",
"city": "guangzhou"
},
"name": "jack",
"age": 27,
"join_date": "2017-01-01"
}
GET /company/_mapping
? 查詢(xún)結(jié)果
{
"company" : {
"mappings" : {
"properties" : {
"address" : {
"properties" : {
"city" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"country" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"province" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
},
"age" : {
"type" : "long"
},
"join_date" : {
"type" : "date"
},
"name" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}
復(fù)雜數(shù)據(jù)類(lèi)型會(huì)在底層進(jìn)行數(shù)據(jù)的轉(zhuǎn)換
##object類(lèi)型
{
"authors": [
{ "age": 26, "name": "Jack White"},
{ "age": 55, "name": "Tom Jones"},
{ "age": 39, "name": "Kitty Smith"}
]
}
存儲(chǔ)的格式产雹;name和age會(huì)分開(kāi)诫惭,從列式轉(zhuǎn)化成行式來(lái)存儲(chǔ)
{
"authors.age": [26, 55, 39],
"authors.name": [jack, white, tom, jones, kitty, smith]
}
補(bǔ)充一個(gè)官網(wǎng)示例:
PUT my_index
{
"mappings": {
"properties": {
"manager": {
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
},
"employees": {
"type": "nested", // 數(shù)組類(lèi)型(復(fù)雜數(shù)據(jù)類(lèi)型)
"properties": {
"age": { "type": "integer" },
"name": { "type": "text" }
}
}
}
}
}
PUT my_index/_doc/1
{
"region": "US",
"manager": {
"name": "Alice White",
"age": 30
},
"employees": [
{
"name": "John Smith",
"age": 34
},
{
"name": "Peter Brown",
"age": 26
}
]
}
Elasticsearch 6.x Mapping設(shè)置 - 掘金 https://juejin.im/post/5b799dcb6fb9a019be279bd7
Field datatypes | Elasticsearch Reference [7.6] | Elastic https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html