在业务系统中,我们经常遇到下面的报错,设置为keyword类型的字段,插入很长的大段内容后,报字符超出异常,无法插入
如下截图:
大概的意思是写入该字段的值大于32766的长度,因此,字段解析失败,因而报以上错误信息。通过查阅文档,我们可以知道,keyword类型的字段长度是32766,而text类型是没有长度限制一说。
因此,我们将该字段类型改为text 字符串型,一定可以解决这个字段解析报错的问题。
检索超过 Keyword ignore_above 设定长度的字段后,无法返回结果?
ES中用于设置超过设定字符后,不被索引或者存储。
第一步,定义keyword,并指定ignore_above
PUT my_index_test
{
"mappings": {
"properties": {
"message": {
"type": "keyword",
"ignore_above": 20 这个字段message将会忽略内容超过20个字符长度的内容的检索,
}
}
}
}
第二步,插入几个文档,
PUT my_index_test/_doc/1
{
"message":"Syntax error"
}
PUT my_index_test/_doc/2
{
"message":"Syntax error with some long stacktrace nice to meet u"
}
第三步,验证搜索:
GET my_index_test/_search
{
"query": {
"term": {
"message": {
"value": "Syntax error"
}
}
}
}
//能正常匹配并返回
GET my_index_test/_search
{
"query": {
"term": {
"message": {
"value": "Syntax error with some long stacktrace nice to meet u"
}
}
}
}
//返回值为null,无法返回
那实际内容有没有被存入ES中呢,我们发现是已经被存入了的。
那么经过上面的验证测试,我们可以知道:
不管Ignore_above参数怎么设置,即便超过参数限定。文档都是可以存入的。但是不能被检索。
3,Keyword类型,临界长度验证实践2
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个字符后,能提交成功,返回结果如下:
"_id": "2000",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 2,
"failed": 0
},
"created": true
}
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。