天天看點

深究|Elasticsearch單字段支援的最大字元數?

思考:Elasticsearch單字段支援的最大字元數?

設定ignore_above之後引申的問題:

#1、ignore_above的作用?

ES中用于設定超過設定字元後,不被索引或者存儲。

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

#2、ignore_above用法:

PUT ali_test

{

 "mappings": {

 "ali_type": {

 "properties": {

 "url": {

 "type":"keyword",

 "ignore_above":256

 },

 "url_long": {

 "type":"keyword"

 "url_long_long": {

 "ignore_above":32766

 }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

#3、當字元超過給定長度後,能否存入?

驗證表名,對于以上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",

 "total": 2,

 "successful": 2,

 "created": true

結論:keyword類型的最大支援的長度為——32766個UTF-8類型的字元。

也就是說term精确比對的最大支援的長度為32766個UTF-8個字元。

#4、引申問題:text類型和keyword類型的存儲字元數差別?

text類型:支援分詞、全文檢索,不支援聚合、排序操作。

适合大字段存儲,如:文章詳情、content字段等;

keyword類型:支援精确比對,支援聚合、排序操作。

适合精準字段比對,如:url、name、title等字段。

一般情況,text和keyword共存,設定mapping如下:

 "title_v1": {

 "analyzer":"ik_max_word",

 "type":"text",

 "term_vector" : "with_positions_offsets",

 "fields":{

 "keyword":{

 "ignore_above":256,

#5、小結

ES5.X版本以後,keyword支援的最大長度為32766個UTF-8位元組數(至于多少個字元數需要根據業務場景定,建議參考最新版本的官方文檔說明),text對字元長度沒有限制。

設定ignore_above後,超過給定長度後的資料将不被索引,無法通過term精确比對檢索傳回結果。

參考:

https://www.elastic.co/guide/en/elasticsearch/reference/5.5/ignore-above.html

繼續閱讀