深究|Elasticsearch单字段支持的最大字符数?

本文涉及的产品
检索分析服务 Elasticsearch 版,2核4GB开发者规格 1个月
简介: 在业务系统中,遇到过两个问题:问题1:设置为keyword类型的字段,插入很长的大段内容后,报字符超出异常,无法插入。问题2:检索超过ignore_above设定长度的字段后,无法返回结果。

思考: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": {

 "type":"keyword",

 "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....省略"

}

1

2

3

4

url参考地址:

http://t.cn/zH6FHG7


检索:


GET ali_test/ali_type/_search

{

 "query": {

 "term": {

"url" : "1705个字符的url"


}

}

}

1

2

3

4

5

6

7

8

9

返回结果:


{

 "took": 1,

 "timed_out": false,

 "_shards": {

 "total": 5,

 "successful": 5,

 "failed": 0

 },

 "hits": {

 "total": 0,

 "max_score": null,

 "hits": []

 }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

结论:1705个字符,url、url_long、url_long_long都可以存入,可以通过head插件查看结果。

但是url term检索无法检索返回结果,原因: url字段设置了"ignore_above":256,导致超出256个字符后不被索引。

image.png

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

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

post 32766个字符后,能提交成功,返回结果如下:


{

 "_index": "ali_test",

 "_type": "ali_type",

 "_id": "2000",

 "_version": 1,

 "result": "created",

 "_shards": {

 "total": 2,

 "successful": 2,

 "failed": 0

 },

 "created": true

}

1

2

3

4

5

6

7

8

9

10

11

12

13

结论:keyword类型的最大支持的长度为——32766个UTF-8类型的字符。

也就是说term精确匹配的最大支持的长度为32766个UTF-8个字符。


#4、引申问题: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"

 }

 }

 }

 }

 }

 }

}

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

#5、小结

ES5.X版本以后,keyword支持的最大长度为32766个UTF-8字节数(至于多少个字符数需要根据业务场景定,建议参考最新版本的官方文档说明),text对字符长度没有限制。


设置ignore_above后,超过给定长度后的数据将不被索引,无法通过term精确匹配检索返回结果。


参考:

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

相关实践学习
使用阿里云Elasticsearch体验信息检索加速
通过创建登录阿里云Elasticsearch集群,使用DataWorks将MySQL数据同步至Elasticsearch,体验多条件检索效果,简单展示数据同步和信息检索加速的过程和操作。
ElasticSearch 入门精讲
ElasticSearch是一个开源的、基于Lucene的、分布式、高扩展、高实时的搜索与数据分析引擎。根据DB-Engines的排名显示,Elasticsearch是最受欢迎的企业搜索引擎,其次是Apache Solr(也是基于Lucene)。 ElasticSearch的实现原理主要分为以下几个步骤: 用户将数据提交到Elastic Search 数据库中 通过分词控制器去将对应的语句分词,将其权重和分词结果一并存入数据 当用户搜索数据时候,再根据权重将结果排名、打分 将返回结果呈现给用户 Elasticsearch可以用于搜索各种文档。它提供可扩展的搜索,具有接近实时的搜索,并支持多租户。
相关文章
|
1月前
|
PHP
Elasticsearch模糊查询单字段多字段
Elasticsearch模糊查询单字段多字段
28 0
Windows、Mac系统 Elasticsearch离线文档(支持选择版本)安装部署教程
此文档是针对当前最新版本,或指定版本安装教程 如需下载 Elastic认证考试特定版本,请戳: Elasticsearch离线文档安装部署教程(Elastic认证考试版) 鉴于备考环境需要经常访问官方文档,而官方访问速度齁慢齁慢。特此给出官方文档离线版食谱,各位客官细细品尝。
Windows、Mac系统 Elasticsearch离线文档(支持选择版本)安装部署教程
|
自然语言处理 索引
Elasticsearch添加拼音搜索支持
Elasticsearch添加拼音搜索支持
203 0
|
SQL 自然语言处理 索引
ElasticSearch不支持分组查询么?
在使用es进行组合查询的时候,遇到一个非常有意思的场景,特此记录一下 某些场景下,直接针对某个Field进行分组查询,居然无法返回结果,会给出类似Text fields are not optimised for operations that require per-document field data like aggregations and sorting, so these operations are disabled by default的提示信息,接下来看一下这个问题是个什么情况,以及如何解决
449 0
ElasticSearch不支持分组查询么?
|
自然语言处理 达摩院 搜索推荐
【新版本】开放搜索开源兼容版,支持Elasticsearch做搜索召回引擎
9月15日阿里云开放搜索重磅发布【开源兼容版】,搜索召回环节同时支持阿里云自研Ha3引擎与阿里云Elasticsearch引擎,并提供多行业的搜索算法能力,助力企业高效实现搜索效果深度优化。
684 0
【新版本】开放搜索开源兼容版,支持Elasticsearch做搜索召回引擎
|
存储 SQL NoSQL
探究 | Elasticsearch不支持事务有什么好的弥补方案吗?
1、问题 源自星球同学的提问:es如何与hive或mysql结合使用?es不支持事务有什么好的弥补方案吗?
402 0
探究 | Elasticsearch不支持事务有什么好的弥补方案吗?
|
自然语言处理 达摩院 搜索推荐
阿里云开放搜索发布开源兼容版,支持Elasticsearch做搜索召回引擎
9月15日,阿里云开放搜索正式上线开源兼容版,搜索召回环节同时支持阿里云自研Ha3引擎与阿里云Elasticsearch引擎,并提供多行业的搜索算法能力,助力企业高效实现搜索效果深度优化。
703 0
阿里云开放搜索发布开源兼容版,支持Elasticsearch做搜索召回引擎
|
自然语言处理 索引
Elasticsearch添加拼音搜索支持
Elasticsearch添加拼音搜索支持
213 0
|
分布式计算 关系型数据库 存储
深究|Elasticsearch单字段支持的最大字符数?
思考:Elasticsearch单字段支持的最大字符数?详解设置ignore_above之后引申的问题。
2599 0
|
数据库 开发者 存储
探究 | Elasticsearch不支持事务有什么好的弥补方案吗?
同学的提问:es如何与hive或mysql结合使用?es不支持事务有什么好的弥补方案吗?
1353 0