iT邦幫忙

第 12 屆 iThome 鐵人賽

DAY 15
1
Elastic Stack on Cloud

Elastic Stack武學練功坊系列 第 15 篇

Elastic Stack第十五重

  • 分享至 

  • xImage
  •  

Mapping Part III(結構)

所有的field data types 蠻多內容的,所以預計會分幾篇來說明
後續幾篇介紹的field data types是我認為用到的機率比較高或是個人比較常用的


Field data types

每一個field都有一個 field data type ,又可稱為 field type ,
這個type代表著這個field儲存的data的類型

field types可分群為不同的 family ,
在同一 family 下,支援相同的搜尋功能,差異在於使用空間或是效能特性

不過,目前只有 family keyword 有多個field types,即 keyword, constant_keyword, 和 wildcard field type,
其餘的 family 都只有一個 field type,例如: boolean type family 包含了一個 field type,即為 boolean


以下開始介紹各種 field data type

binary
接受的資料類型: binary value(二進制值)以base64編碼的字串
特性:無法被搜尋
e.g.,
index mapping

PUT my-index-000001
{
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "blob": {
        "type": "binary"
      }
    }
  }
}

Sample data (樣本資料)

PUT my-index-000001/_doc/1
{
  "name": "Some binary blob",
  "blob": "U29tZSBiaW5hcnkgYmxvYg=="
}

Search on field with binary field data type

GET my-index-000001/_search
{
  "query": {
    "match": {
      "blob": "29tZSBi"
    }
  }
}

Response
Response

boolean
接受的資料類型:

被解析的值 input value
False false, "false", ""(empty string)
True true, "true"

e.g.,
index mapping

PUT my-index-000002
{
  "mappings": {
    "properties": {
      "is_published": {
        "type": "boolean"
      }
    }
  }
}

Sample data (樣本資料)

POST my-index-000002/_doc/1
{
  "is_published": "true" 
}

POST my-index-000002/_doc/2
{
  "is_published": false
}

Search all in index

GET my-index-000002/_search

Response
Response

Response說明
發現 _source 還是會有 "true" 而不是 true,不過實際上搜尋或後分類還是會被正確解析的

Search true value

GET my-index-000002/_search
{
  "query": {
    "term": {
      "is_published": true 
    }
  }
}

Response
Response

aggs

GET my-index-000002/_search
{
  "size": 0,
  "aggs": {
    "publish_state": {
      "terms": {
        "field": "is_published"
      }
    }
  }
}

Response
Response

Response說明
buckets內的key會用 1/0 來代表 true/false,
且多了key key_as_string,會用 "true"/"false" 來代表 true/false

numeric types
這就和大部分的RDBMS很像,
提供的field type有:long, integer, short, byte, double, float, half_float, scaled_float
主要差異就是N-bit的integer還是floating point number

比較特別,額外說明一下
scaled_float
對於蠻多情境,會透過 scaling factor(比例因子) 把浮點數字(float-point)變成整數(integer),
這就是 scaled_float 可以做到的,
例如:美元最小單位: 美分(cent) = 0.01美元(dollar)
價錢就都會有到小數點後第二位,此時就可使用
field name price 且 field type scaled_float 搭配 scaling_factor 設定為 100,
在新增document時,會把輸入的值乘以 scaling_factor 值,且四捨五入為 long 值
也就是es在對這欄位做相關動作時,是在操作 integer ,
這有益於節省空間,因為 integers 比 floating points 容易壓縮

index mapping

PUT my-index-000003
{
  "mappings": {
    "properties": {
      "price": {
        "type": "scaled_float",
        "scaling_factor": 100
      }
    }
  }
}

Sample data (樣本資料)

PUT my-index-000003/_doc/1
{
  "price": 1.45
}

在第十三重其實有提到 metadata fields,先來介紹其中一個 metadata field,即 _source

_source
原始的JSON ,即為在新增document時,輸入的JSON data
言下之意,這些並不會被es更動過,才能讓後續 fetch requests (e.g., get or search),
可以回傳原始值

所以可能會納悶,為什麼有些地方說明到es會額外parse的,但在 _source 卻一樣呢?
例如:
boolean field data type 輸入範例資料 "is_published": "true" ,_source 一樣是 字串的"true"

docvalue_fields
如果真的很想知道實際es處理過的結構,可以使用 docvalue_fields 參數,會回傳 fields 的 doc values

doc values 存的值和 _source 一樣,只是es為了排序和後分類而優化過

怕誤導所以這邊補充一下,這邊的 doc_values 不代表是es在搜尋時轉換的值

以查看 boolean field type 的 sample data 為例:

Reqeust

GET my-index-000002/_search
{
  "docvalue_fields": ["is_published"]
}

Response
Response


小小新手,如有理解錯誤或寫錯再請不吝提醒或糾正


Reference

Field data types
Metadata fields - _source
Doc value fields


上一篇
Elastic Stack第十四重
下一篇
Elastic Stack第十六重
系列文
Elastic Stack武學練功坊 共 30 篇
圖片
  熱門推薦
圖片
{{ item.channelVendor }} | {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言