深究|Elasticsearch單字段支持的最大字符數?

在業務系統中,遇到過兩個問題:app

問題1:設置爲keyword類型的字段,插入很長的大段內容後,報字符超出異常,沒法插入。ide

問題2:檢索超過ignore_above設定長度的字段後,沒法返回結果。post

思考:Elasticsearch單字段支持的最大字符數?url

本文是基於設置ignore_above以後引伸的問題展開討論與思考。插件

013d

ignore_above的做用?code

ES中用於設置超過設定字符後,不被索引或者存儲。blog

Strings longer than the ignore_above setting will not be indexed or stored.排序

02索引

ignore_above用法

PUT ali_test

{

"mappings": {

"ali_type": {

    "properties": {

           "url": {

          "type":"keyword",

           "ignore_above":256

        },

           "url_long": {

          "type":"keyword"

        },

         "url_long_long": {

          "type":"keyword",

           "ignore_above":32766

        }

    }

}

}

}

03

當字符超過給定長度後,可否存入?

驗證表名,對於以上mapping中設置的url,url_long,url_long_long3個字段。超過256字符的url,均可以存入。

3.1 keyword類型,普通長度驗證

插入url長度爲:1705個字符,以下所示:

post ali_test/ali_type/1

{

"url" : "1705個字符的url"

}

url參考地址:http://t.cn/zH6FHG7

檢索:

GET ali_test/ali_type/_search

{

"query": {

"term": {

"url" : "1705個字符的url"

}

}

}

返回結果:

{

"took": 1,

"timed_out": false,

"_shards": {

"total": 5,

"successful": 5,

"failed": 0

},

"hits": {

"total": 0,

"max_score": null,

"hits": []

}

}

結論:

1705個字符,url、url_long、url_long_long均可以存入,能夠經過head插件查看結果。

可是url term檢索沒法檢索返回結果,緣由: url字段設置了"ignore_above":256,致使超出256個字符後不被索引。

深究|Elasticsearch單字段支持的最大字符數?

3.2 對於keyword類型,臨界長度驗證

post 32767個字符的文檔,報錯以下:

{
"error":{
"root_cause":[
{

"type":"illegal_argument_exception",

            "reason":"Document contains at least one immense term in field="url_long" (whose UTF8 encoding is longer than the max length 32766), all of which were skipped.  Please correct the analyzer to not produce such terms.  The prefix of the first immense term is: '[104, 116, 116, 112, 58, 47, 47, 119, 119, 119, 46, 103, 111, 111, 103, 108, 101, 46, 99, 111, 109, 47, 115, 101, 97, 114, 99, 104, 63, 104]...', original message: bytes can be at most 32766 in length; got 32767"

        }
    ],
    "caused_by":{

        "type":"max_bytes_length_exceeded_exception",

        "reason":"max_bytes_length_exceeded_exception: bytes can be at most 32766 in length; got 32767"

    }
},
"status":400

}

post 32766個字符後,能提交成功,返回結果以下:

{

"_index": "ali_test",

"_type": "ali_type",

"_id": "2000",

"_version": 1,

"result": "created",

"_shards": {

"total": 2,

"successful": 2,

"failed": 0

},

"created": true

}

結論:keyword類型的最大支持的長度爲——32766個UTF-8類型的字符。

也就是說term精確匹配的最大支持的長度爲32766個UTF-8個字符。

04

text類型和keyword類型的存儲字符數區別?

text類型:支持分詞、全文檢索,不支持聚合、排序操做。適合大字段存儲,如:文章詳情、content字段等;

keyword類型:支持精確匹配,支持聚合、排序操做。適合精準字段匹配,如:url、name、title等字段。

通常狀況,text和keyword共存,設置mapping以下:

{

"mappings": {

"ali_type": {

    "properties": {

        "title_v1": {

             "analyzer":"ik_max_word",

                "type":"text",

             "term_vector" : "with_positions_offsets",

                "fields":{

                    "keyword":{

                        "ignore_above":256,

                        "type":"keyword"

                    }

                }

        }

    }

}

}

}

05

小結

1)ES5.X版本之後,keyword支持的最大長度爲32766個UTF-8字符,text對字符長度沒有限制。

2)設置ignore_above後,超過給定長度後的數據將不被索引,沒法經過term精確匹配檢索返回結果。

參考:

http://t.cn/ROXyGes

相關文章
相關標籤/搜索