ElasticSearch 欄位資料型態 === [Field data types](https://www.elastic.co/guide/en/elasticsearch/reference/current/mapping-types.html) --- ## Dynamic mapping and Explicit mappings --- ## 動態 Mapping - Elasticsearch僅通過索引文檔即可自動添加新欄位。 :::info 使用[Dynamic templates](https://www.elastic.co/guide/en/elasticsearch/reference/current/dynamic-templates.html)來定義自定義映射,這些自定義映射將基於匹配條件動態添加欄位。 ::: --- ## 明確 Mapping 在建立index的時候創建映射類型和欄位映射,或通過mapping API添加映射類型和欄位到一個已經存在的index中。 --- 建立Index ```json= PUT /my-index-000001 { "mappings": { "properties": { "age": { "type": "integer" }, "email": { "type": "keyword" }, "name": { "type": "text" } } } } ``` --- 新增欄位 ```json= PUT /my-index-000001/_mapping { "properties": { "employee-id": { "type": "keyword", "index": false } } } ``` --- # String datatypes --- 1. Text - 用於索引全文值的字段,分析過程允許Elasticsearch搜索每個全文字段中的單個單詞。文本字段不用於排序,很少用於聚合。 --- 2. Keyword - 用於索引結構化內容的字段,它們通常用於過濾、排序和聚合。關鍵字字段只能按其確切值進行搜索。 --- # Numeric datatypes - Long: 帶符號64位元整數 (-2^64^ ~ 2^64^-1) - Integer: 帶符號32位元整數 (-2^32^ ~ 2^32^-1) - Short:帶符號16位元整數 (-2^16^ ~ 2^16^-1) - Byte:帶符號8位元整數 (-2^8^ ~ 2^8^-1) - Double: 雙精度64位元浮點數 - Float:精度32位元浮點數 - Half_float:半精度16位元浮點數 - Scaled_float:會透過 scaling factor把浮點數字變成整數 --- ```json= PUT my-index-000001 { "mappings": { "properties": { "number_of_bytes": { "type": "integer" }, "time_in_seconds": { "type": "float" }, "price": { "type": "scaled_float", "scaling_factor": 100 } } } } ``` --- # Date datatype JSON沒有日期數據類型,因此Elasticsearch中的日期可以是: - 包含格式化日期的字符串 "2015-01-01"或"2015/01/01 12:10:30"。 - 一個自紀元以來毫秒數的數字。 - 一個自紀元以來秒數的數字。 --- 日期格式可以自定義,但是如果未指定,則預設使用: :::info "strict_date_optional_time||epoch_millis" ::: ```json= PUT my-index-000001/_doc/1 { "date": "2015-01-01" } PUT my-index-000001/_doc/2 { "date": "2015-01-01T12:10:30Z" } PUT my-index-000001/_doc/3 { "date": 1420070400001 } ``` --- # Boolean datatype 布林字段接受JSON true和false值,但也可以接受被解釋為true或false的字符串。 - False values: false , "false" - True valus: true, "true" --- # Binary datatype 二進制類型接受二進制值作為Base64編碼的字符串。 默認情況下不存儲該字段,並且不可搜索。 --- ```json= PUT my-index { "mappings" : { "properties" : { "rawdata" : { "type" : "binary" } } } } ``` ```json= POST my-index2/_doc/ { "rawdata" : "aGVsbG93b3JsZA==" } ``` --- # Range datatype <!-- .slide: style="font-size: 32px;" --> Range型態支持 - Integer_range:一個帶符號32位元Range (-2^32^ ~ 2^32^-1) - Float_range: 一個精度32位元浮點數Range - Long_range: 一個帶符號64位元Range(-2^64^ ~ 2^64^-1) - Dobule_range: 一個雙精度64位元浮點數Range - Date_range:自系統紀元以來經過的無符號64位整數毫秒。 - Ip_range: 支持IPv4或IPv6(或混合)地址的一系列ip值 --- ```json= PUT range_index { "settings": { "number_of_shards": 2 }, "mappings": { "properties": { "expected_attendees": { "type": "integer_range" }, "time_frame": { "type": "date_range", "format": "yyyy-MM-dd HH:mm:ss||yyyy-MM-dd||epoch_millis" } } } } ``` --- ```json= PUT range_index/_doc/1?refresh { "expected_attendees" : { "gte" : 10, "lt" : 20 }, "time_frame" : { "gte" : "2015-10-31 12:00:00", "lte" : "2015-11-01" } } ``` --- ```json= GET range_index/_search { "query" : { "range" : { "time_frame" : { "gte" : "2015-10-31", "lte" : "2015-11-01", "relation" : "within" } } } } ``` --- # Array datatype 在Elasticsearch中,沒有專用的Array類型。預設情況下,任何字段都可以包含零個或多個值,而 Array 中的所有值必須具有相同的資料類型。 例如: - 字串陣列: ["one","two"] - 數值陣列: [1,2] - 陣列的陣列: [1, [2,3]] 相當於 [1,2,3] - 物件陣列: [{"name":"Mary","age":12}, {"name":"John","age":10}] --- # Object datatype - JSON文檔本質上是分層的:文檔可能包含內部物件,而內部物件本身可能包含內部物件: ```json= PUT my-index-000001/_doc/1 { "region": "US", "manager": { "age": 30, "name": { "first": "John", "last": "Smith" } } } ``` --- # Nested datatype nested類型是特殊的object型別,它允許物件陣列以可以彼此獨立查詢的方式進行索引。 [說明](https://www.elastic.co/guide/en/elasticsearch/reference/master/nested.html) --- ## object arrary 如何展平 Lucene沒有內部物件的概念,因此Elasticsearch將物件層次結構展平為一個簡單的字段名稱和值列表。 例如,以下文件: --- ```json= PUT my-index-000001/_doc/1 { "group" : "fans", "user" : [ { "first" : "John", "last" : "Smith" }, { "first" : "Alice", "last" : "White" } ] } ``` --- ```json= GET my-index-000001/_search { "query": { "bool": { "must": [ { "match": { "user.first": "Alice" } }, { "match": { "user.last": "Smith" } } ] } } } ``` --- 內部轉換成像這樣的文檔 其中 alice 和 white 的關聯性丟失了 ```json= { "group" : "fans", "user.first" : [ "alice", "john" ], "user.last" : [ "smith", "white" ] } ``` --- 使用nested類型 ```json= PUT my-index-000002 { "mappings": { "properties": { "user": { "type": "nested" } } } } ``` --- 搜尋 nested field type 需使用 nested query ```json= GET my-index-000002/_search { "query": { "nested": { "path": "user", "query": { "bool": { "must": [ { "match": { "user.first": "Alice" } }, { "match": { "user.last": "Smith" } } ] } } } } } ``` :::info 如果需要索引object arrary 並保持數組中每個對象的獨立性,請使用nested數據類型而不是 object數據類型。 ::: --- # 敬請指教 ----
{"metaMigratedAt":"2023-06-16T00:31:10.298Z","metaMigratedFrom":"Content","title":"ElasticSearch \n欄位資料型態","breaks":true,"contributors":"[{\"id\":\"565617f1-fa63-410c-9fb9-a9278632bb7a\",\"add\":7032,\"del\":1459}]"}
    857 views