日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

04.elasticsearch-dynamic_mapping_and_index_template

發(fā)布時間:2024/2/28 编程问答 33 豆豆
生活随笔 收集整理的這篇文章主要介紹了 04.elasticsearch-dynamic_mapping_and_index_template 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

文章目錄

    • 1. dynamic mapping 設(shè)置
      • 1. es內(nèi)部支持的field類型的動態(tài)識別
        • 1. 默認(rèn)的field 識別
        • 2. date-detection
        • 3. numeric-detection
      • 2. 在mapping中設(shè)置動態(tài)的模板進(jìn)行識別
        • 1. data type + match_mapping_type
        • 2. match_and_unmatch + field name
        • 3. match_pattern + field name
        • 4. path_match 和 path_unmatch + 帶點符號的field name
        • 5. 使用filed name 和 dynamic_type 作為mapping param的設(shè)置
    • 2. index template
      • 1. 創(chuàng)建一個index template
      • 2. 一個新的index命中多個index-template的規(guī)則
      • 3. template中添加version

1. dynamic mapping 設(shè)置

dynamic mapping主要介紹了,es是如何根據(jù)indexing的doc來確定mapping的信息的,也就是根據(jù)每個json 屬性判斷mapping的每個field應(yīng)該為什么類型。既然說了是dynamic mapping,也就是對應(yīng)的mapping沒有定義,或者mapping存在但是沒有該field的情況,如果mapping中已經(jīng)定義了,那么直接根據(jù)mapping中定義的type去嘗試解析即可。
json能夠表達(dá)的類型是有限的,只能表達(dá)以下類型

  • 字符串
  • 數(shù)字
  • 對象(JSON 對象)
  • 數(shù)組
  • 布爾
  • Null
  • 比如日期date,數(shù)字中的long,integer是分不清的。所以es需要對這些進(jìn)行支持

    1. es內(nèi)部支持的field類型的動態(tài)識別

    1. 默認(rèn)的field 識別

    null: No field is added.

    true or false: boolean field

    floating point number: float field

    integer: long field

    object: object field

    array: Depends on the first non-null value in the array.

    string: 這個看需要,正常情況下是一個帶有keyword的text的field,如果開啟了date-detection可能會產(chǎn)生date field mapping, 如果開啟了numeric-detection課可能會產(chǎn)生 long或者float field mapping.

    需要注意的是小數(shù)都變成了float,沒有特殊識別double,integer,long都變成了long
    long 是可以兼容integer,但是float卻是不能兼容double的

    例如下面的請求

    PUT my_index05/_doc/1 {"number":123446567890233123446567890233123446567890233123446567890233.6 }返回 {"error": {"root_cause": [{"type": "mapper_parsing_exception","reason": "failed to parse field [number] of type [float] in document with id '1'. Preview of field's value: '1.2344656789023312E59'"}],"type": "mapper_parsing_exception","reason": "failed to parse field [number] of type [float] in document with id '1'. Preview of field's value: '1.2344656789023312E59'","caused_by": {"type": "illegal_argument_exception","reason": "[float] supports only finite values, but got [Infinity]"}},"status": 400 }

    在沒有提前創(chuàng)建mapping的情況下回報錯,因為number字段的值超出了float的范圍
    假如先定義mapping

    PUT my_index05 {"mappings": {"properties": {"number":{"type": "double"}}} }

    則是可以成功寫入的。

    2. date-detection

    es對日期做了特殊支持,默認(rèn)情況下滿足特定的格式的string進(jìn)行indexing的時候可能會被識別為 date字段
    默認(rèn)情況下識別的日期格式是 [ "strict_date_optional_time","yyyy/MM/dd HH:mm:ss Z||yyyy/MM/dd Z"]

    PUT my_index/_doc/1 {"create_date": "2015/09/02" } 這個是可以正確映射為日期的

    你也可以關(guān)掉這個設(shè)置

    PUT my_index {"mappings": {"date_detection": false} }

    同時還可以自定義日期的格式

    PUT my_index {"mappings": {"dynamic_date_formats": ["MM/dd/yyyy"]} }

    3. numeric-detection

    這個會把string類型的數(shù)字轉(zhuǎn)換為數(shù)字,轉(zhuǎn)為float或者long 類型(double長度的會報錯)
    這個設(shè)置默認(rèn)情況下是關(guān)閉的

    PUT my_index {"mappings": {"numeric_detection": true} }PUT my_index/_doc/1 {"my_float": "1.0", "my_integer": "1" }

    2. 在mapping中設(shè)置動態(tài)的模板進(jìn)行識別

    es還可以根據(jù)字段名,json數(shù)據(jù)原始類型等做一些更靈活的動態(tài)field映射,這種一般情況下都是通過特征進(jìn)行設(shè)置,相對來說可能會一條規(guī)則對過個field都有效,所以叫dynamic templates

    dynamic template 主要提供了一下集中方式進(jìn)行template設(shè)置

  • 通過 match_mapping_type + es 識別出來的json data type 來進(jìn)行模式識別,設(shè)置filed mapping param 和field type
  • 通過match_and_unmatch 或者 match_pattern + field name 來進(jìn)行模式識別,設(shè)置filed mapping param 和field type
  • 通過 path_match 和 path_unmatch + 帶點符號的field name的匹配 來進(jìn)行模式識別,設(shè)置filed mapping param 和field type
  • 1. data type + match_mapping_type

    data type是es識別出來的json的數(shù)據(jù)類型,在上面已經(jīng)有描述了,但是es默認(rèn)增加了對日期的識別,所以有下面幾種

  • boolean 布爾
  • date 日期
  • double 類型(這個地方有點奇怪是double而不是float,測試驗證過)
  • long 類型
  • object 類型
  • string 類型
  • 樣例

    PUT my_index {"mappings": {"dynamic_templates": [{"integers": {"match_mapping_type": "long","mapping": {"type": "integer"}}},{"strings": {"match_mapping_type": "string","mapping": {"type": "text","fields": {"raw": {"type": "keyword","ignore_above": 256}}}}},{"my_float":{"match_mapping_type":"double", # 注意這個地方只有寫成double才能正確匹配,生成mapping中的field的type為double, 沒有這個配置的話默認(rèn)生成的mapping field的type為float."mapping":{"type":"double"}}}]} }然后執(zhí)行 PUT my_index/_doc/1 {"my_integer": 922337203685477580, "my_string": "Some string" }返回{"error": {"root_cause": [{"type": "mapper_parsing_exception","reason": "failed to parse field [my_integer] of type [integer] in document with id '1'. Preview of field's value: '922337203685477580'"}],"type": "mapper_parsing_exception","reason": "failed to parse field [my_integer] of type [integer] in document with id '1'. Preview of field's value: '922337203685477580'","caused_by": {"type": "i_o_exception","reason": "Numeric value (922337203685477580) out of range of int\n at [Source: org.elasticsearch.common.bytes.BytesReference$MarkSupportingStreamInputWrapper@56f5802a; line: 2, column: 35]"}},"status": 400 }

    這個會生成把long 強(qiáng)制設(shè)置為 integer(indexing的時候是long也不行),上面indexing的時候傳入了一個大于integer范圍的值,導(dǎo)致了失敗,假如改成小于integer范圍的就可以成功
    假如我們直接把這個put語句應(yīng)用到一個新的自動生成的索引當(dāng)中,則是可以成功的

    PUT my_index01/_doc/1 {"my_integer": 922337203685477580, "my_string": "Some string" }

    這個是可以成功的。

    2. match_and_unmatch + field name

    簡單的模式匹配,可以設(shè)置以什么開頭,以什么結(jié)尾的filed

    樣例

    PUT my_index {"mappings": {"dynamic_templates": [{"longs_as_strings": {"match_mapping_type": "string","match": "long_*","unmatch": "*_text","mapping": {"type": "long"}}}]} }PUT my_index/_doc/1 {"long_num": "5", "long_text": "foo" }

    3. match_pattern + field name

    是上一個的增強(qiáng)版,filed name的模式匹配的時候允許使用正則表達(dá)式

    樣例

    PUT my_index {"mappings": {"dynamic_templates": [{"longs_as_strings": {"match_mapping_type": "string","match_pattern": "regex","match": "^long_*","unmatch": "*_text","mapping": {"type": "long"}}}]} }PUT my_index/_doc/1 {"long_num": "5", "long_text": "foo" }

    4. path_match 和 path_unmatch + 帶點符號的field name

    直接看樣例

    PUT my_index {"mappings": {"dynamic_templates": [{"full_name": {"path_match": "name.*","path_unmatch": "*.middle","mapping": {"type": "text","copy_to": "full_name"}}}]} }PUT my_index/_doc/1 {"name": {"first": "John","middle": "Winston","last": "Lennon"} }

    5. 使用filed name 和 dynamic_type 作為mapping param的設(shè)置

    這個是什么意思呢,就是將field name 和dynamic_type作為變量,在設(shè)置mapping的時候可以直接引用

    看一個樣例更容易理解

    PUT my_index {"mappings": {"dynamic_templates": [{"named_analyzers": {"match_mapping_type": "string","match": "*","mapping": {"type": "text","analyzer": "{name}"}}},{"no_doc_values": {"match_mapping_type":"*","mapping": {"type": "{dynamic_type}","doc_values": false}}}]} }PUT my_index/_doc/1 {"english": "Some English text", "count": 5 }

    這里的string類型的屬性都會使用它的field name作為analyzer,比如 english 字段的analyzer 就是 english-analyzer ,這個局限性很強(qiáng),因為只有有限數(shù)量的analyzer
    其他類型的field的type直接使用es探測到的type

    總感覺這個用處很小

    2. index template

    index template 的作用是什么呢,從字面意思上理解就是為index的mapping指定template使用的
    他的功能是你定一個mapping,并且指定一些模式匹配規(guī)則,可以指定哪些index創(chuàng)建的時候使用這個mapping,他主要是對還未創(chuàng)建的索引起作用。
    這個功能還是很有用的,比如在日志系統(tǒng)當(dāng)中,一般是按照天創(chuàng)建索引,索引的mapping基本上是一致的,或者說只有有限的幾種,而且索引的name一般也都是根據(jù)一些規(guī)則組成的,所以就可以定義一些index template,在每次創(chuàng)建索引的時候不用再指定mapping,直接會根據(jù)index template創(chuàng)建mapping。
    當(dāng)然,在index template中不僅可以指定mapping,還可以指定所有正常創(chuàng)建index的時候使用的參數(shù),包括alias,setting 等設(shè)置。

    1. 創(chuàng)建一個index template

    PUT _template/template_1 {"index_patterns": ["te*", "bar*"],"aliases" : {"kk-log" : { }},"settings": {"number_of_shards": 1},"mappings": {"_source": {"enabled": false},"properties": {"host_name": {"type": "keyword"},"created_at": {"type": "date","format": "EEE MMM dd HH:mm:ss Z yyyy"}}},"order" : 10 }

    這樣的話,所有以te,bar開頭的索引都會命中這個index-template,進(jìn)而對應(yīng)的index的會使用這個template中的設(shè)置。

    在template的請求體中有以下比較重要的地方

  • index_patterns: 這個是一個string數(shù)組,支持通過通配符*配置的index name ,進(jìn)而來匹配新創(chuàng)建時候的index
  • aliases: 索引別名,可以設(shè)置多個別名
  • mappings: mapping設(shè)置
  • settings: index settings
  • version: 這個是使用外部系統(tǒng)關(guān)聯(lián)管理使用index-template的時候使用的,es不會自動生成這個數(shù)字
  • order: 順序,當(dāng)一個index命中多個index-template的時候,會都使用,但是order值從小到大一次merge相關(guān)配置,也就是order值更大的相同設(shè)置會overried order值更小的index-template中的設(shè)置。
  • 2. 一個新的index命中多個index-template的規(guī)則

    這個在上面說的差不多了,這里舉一個栗子來看看吧

    PUT /_template/template_1 {"index_patterns" : ["test*"],"order" : 0,"settings" : {"number_of_shards" : 1},"mappings" : {"_source" : { "enabled" : false }} }PUT test_have01/_doc/1 {"my_integer": 922337203685477580, "my_string": "Some string" }GET test01/_search返回 {"took" : 0,"timed_out" : false,"_shards" : { "total" : 1, "successful" : 1, "skipped" : 0, "failed" : 0 },"hits" : {"total" : { "value" : 1, "relation" : "eq" },"max_score" : 1.0,"hits" : [{"_index" : "test_have01","_type" : "_doc","_id" : "1","_score" : 1.0}]} }

    可以看到返回的直接沒有_source字段了。

    增加一個order值更高的template,并打開source字段

    PUT /_template/template_2 {"index_patterns" : ["test_have*"],"order" : 1,"settings" : {"number_of_shards" : 1},"mappings" : {"_source" : { "enabled" : true }} }

    再寫入文檔

    PUT test_have01/_doc/1 {"my_integer": 922337203685477580, "my_string": "Some string" }GET test_have01/_search返回{"took" : 0,"timed_out" : false,"_shards" : { total" : 1, "successful" : 1, "skipped" 0, "failed" : 0 },"hits" : {"total" : { "value" : 1,@@relation" : "eq" },"max_score" : 1.0,"hits" : [{"_index" : "test_have01","_type" : "_doc","_id" : "1","_score" : 1.0,"_source" : {"my_integer" : 922337203685477580,"my_string" : "Some string"}}]} }

    可以看到template_2 中對_source字段的設(shè)置override 了temple_1中的設(shè)置,因為他的order值更大。

    3. template中添加version

    這個說事在外部系統(tǒng)管理template的時候使用

    PUT /_template/template_1 {"index_patterns" : ["*"],"order" : 0,"settings" : {"number_of_shards" : 1},"version": 123 }

    等后面有實際的使用經(jīng)驗了再來補(bǔ)充。

    超強(qiáng)干貨來襲 云風(fēng)專訪:近40年碼齡,通宵達(dá)旦的技術(shù)人生

    總結(jié)

    以上是生活随笔為你收集整理的04.elasticsearch-dynamic_mapping_and_index_template的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。