1.概述
在业务系统中,遇到过两个问题:
-
问题1:设置为keyword类型的字段,插入很长的大段内容后,报字符超出异常,无法插入。
-
问题2:检索超过ignore_above设定长度的字段后,无法返回结果。
思考:Elasticsearch单字段支持的最大字符数?
本文是基于设置ignore_above之后引申的问题展开讨论与思考。
2.ignore_above
2.1 ignore_above的作用?
ES中用于设置超过设定字符后,不被索引或者存储。
Strings longer than the ignore_above setting will not be indexed or stored.
2.2 ignore_above用法
PUT ali_test
{
"mappings": {
"properties": {
"url": {
"type": "keyword",
"ignore_above": 256
},
"url_long": {
"type": "keyword"
},
"url_long_long": {
"type": "keyword",
"ignore_above": 32766
}
}
}
}
2.3 长度超限
当字符超过给定长度后,能否存入?
验证表名,对于以上mapping中设置的url,url_long,url_long_long3个字段。超过256字符的url,都可以存入。
2.3.1 keyword类型,普通长度验证
插入url长度为:1705个字符,如下所示:
post ali_test/_doc/1
{
"url": "1705个字符的url"
}
GET ali_test/_doc/_search
{
"query": {
"term": {
"url": "1705个字符的url"
}
}
}
案例
post ali_test/_doc/2
{
"url": "https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirect"
}
检索:
GET ali_test/_doc/_search
{
"query": {
"term": {
"url": "https://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirecthttps://mp.weixin.qq.com/s?__biz=MzI2NDY1MTA3OQ==&mid=2247489110&idx=1&sn=363521b9e88974da18585128eab40cd5&chksm=eaa83e7edddfb76830099dbc8bf963fa218577d9bd183733c9dcfac036d7fbd60474d2400a8b&scene=21#wechat_redirect"
}
}
}
返回结果
{
"took" : 3,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 0,
"relation" : "eq"
},
"max_score" : null,
"hits" : [ ]
}
}
2.4 结论:
1705个字符,url、url_long、url_long_long都可以存入,可以通过head插件查看结果。
但是url term检索无法检索返回结果,原因: url字段设置了"ignore_above":256,导致超出256个字符后不被索引。
2.5 对于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个字符。
3.text类型和keyword类型的存储字符数区别?
text类型:支持分词、全文检索,不支持聚合、排序操作。适合大字段存储,如:文章详情、content字段等;
keyword类型:支持精确匹配,支持聚合、排序操作。适合精准字段匹配,如:url、name、title等字段。
一般情况,text和keyword共存,设置mapping如下:
{
"mappings": {
"properties": {
"title_v1": {
"analyzer": "ik_max_word",
"type": "text",
"term_vector": "with_positions_offsets",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}
}
}
}
4.小结
1)ES5.X版本以后,keyword支持的最大长度为32766个UTF-8字符,text对字符长度没有限制。
2)设置ignore_above后,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。