【Elasticsearch】7.9 单字段支持的最大字符数

561 篇文章 547 订阅 ¥79.90 ¥99.00
本文探讨了Elasticsearch中单字段的最大字符限制,重点关注`ignore_above`参数的作用和用法。当keyword类型的字段字符超过32766个UTF-8字符时,会导致存储或索引问题。`ignore_above`设置允许忽略超过指定长度的字符串,不进行索引。同时,文章还比较了text和keyword类型在存储和检索上的区别。
摘要由CSDN通过智能技术生成

在这里插入图片描述

1.概述

1.1 官网

官网:https://www.elastic.co/guide/en/elasticsearch/reference/current/ignore-above.html#ignore-above

ignore_above

长于ignore_above设置的字符串将不会被索引或存储。对于字符串数组,ignore_above将分别应用于每个数组元素,且字符串元素的长度大于ignore_above将不会被索引或存储的字符串元素。

_source如果启用了这是Elasticsearch中的默认值,则所有字符串/数组元素仍将存在于该字段中。

# 1
PUT my-index-000001
{
  "mappings": {
    "properties": {
      "message": {
        "type": "keyword",
        "ignore_above": 20 
      }
    }
  }
}

# 2
PUT my-index-000001/_doc/1 
{
  "message": "Syntax error"
}

# 3
PUT my-index-000001/_doc/2 
{
  "message": "Syntax error with some long stacktrace"
}

# 4
GET my-index-000001/_search 
{
  "aggs": {
    "messages": {
      "terms": {
        "field": "message"
      }
    }
  }
}
  1. 该字段将忽略任何超过20个字符的字符串。
  2. 该文档已成功建立索引。
  3. 该文档将被索引,但不对该message字段建立索引。
  4. 搜索返回两个文档,但术语聚合中仅存在第一个文档。

此选项对于防止Lucene的术语字节长度限制也很有用32766。

ignore_above的值是字符数,但是Lucene会计算字节数。如果您使用带有许多非ASCII字符的UTF-8文本,则您可能希望将限制设置为,32766 / 4 = 8191因为UTF-8字符最多可以占用4个字节。

2.业务

下面转载:https://blog.csdn.net/laoyang360/article/details/78207980

在业务系统中,遇到过两个问题:

问题1:设置为keyword类型的字段,插入很长的大段内容后,报字符超出异常,无法插入。
问题2:检索超过ignore_above设定长度的字段后,无法返回结果。

思考:Elasticsearch单字段支持的最大字符数?

设置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": {
  "ali_type": {
  "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/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个字符后不被索引。

在这里插入图片描述

2.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个字符。

2.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"
						}
					}
				}
			}
		}
	}
}

小结

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

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

参考:

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

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Elasticsearch 7.9中,创建索引是一个基本操作,用于存储和检索据。以下是创建索引的步骤: 1. **打开Elasticsearch客户端**:你可以使用curl命令行工具,Elasticsearch REST API客户端(如Sense或Beats中的Kibana Dev Tools),或者Elasticsearch的Java、Python等官方客户端库。 2. **定义索引信息**:首先,你需要指定索引的名字。例如,如果你想创建一个名为`my-index`的索引,可以这样表示: ```json { "index": { "name": "my-index" } } ``` 3. **发送请求**:将这个JSON结构作为POST请求发送到`/_create`端点。完整的URL可能是`http://localhost:9200/my-index/_create`,其中`localhost:9200`是Elasticsearch的默认地址,需要根据实际情况替换。 示例用curl命令: ```sh curl -X POST 'http://localhost:9200/my-index/_create' -H 'Content-Type: application/json' -d ' { "index": { "name": "my-index" } } ' ``` 4. **检查响应**:如果索引创建成功,Elasticsearch会返回一个响应确认索引已创建。如果索引已经存在,或者创建过程中出现错误,响应会包含相应的错误信息。 5. **配置索引模板**(可选):为了更高效地管理索引,可以预先定义索引模板,设置字段映射、分析器等属性。 6. **将文档插入索引**:创建索引后,你可以开始将文档(通常是JSON格式)插入到索引中。 注意:在实际使用中,可能还需要设置一些选项,比如分片和副本,以提高查询性能和容错性。具体配置应根据应用需求调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值