【发布时间】:2021-04-24 15:09:04
【问题描述】:
我从 elasticsearch 2.x 更新到 5.1 时遇到问题。但是,我的一些数据在较新的 elasticsearch 中不起作用,因为在 2.x 之前似乎启用了“默认情况下在文本字段上禁用字段数据”https://www.elastic.co/guide/en/elasticsearch/reference/5.1/fielddata.html。
有没有办法自动启用 fielddata 到文本字段?
我试过这样的代码
curl -XPUT http://localhost:9200/_template/template_1 -d '
{
"template": "*",
"mappings": {
"_default_": {
"properties": {
"fielddata-*": {
"type": "text",
"fielddata": true
}
}
}
}
}'
但看起来 elasticsearch 不理解字段名称中的通配符。对此的临时解决方案是我每 30 分钟运行一次 python 脚本,扫描所有索引并将 fielddata=true 添加到新字段。
问题是我在 elasticsearch 中有像“这很酷”这样的字符串数据。
curl -XPUT 'http://localhost:9200/example/exampleworking/1' -d '
{
"myfield": "this is cool"
}'
当尝试聚合时:
curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
"aggs": {
"foobar": {
"terms": {
"field": "myfield"
}
}
}
}'
“默认情况下,在文本字段上禁用 Fielddata。在 [myfield] 上设置 fielddata=true”
elasticsearch 文档建议使用 .keyword 而不是添加字段数据。但是,这并不是我想要的返回数据。
curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
"aggs": {
"foobar": {
"terms": {
"field": "myfield.keyword"
}
}
}
}'
返回:
"buckets" : [
{
"key" : "this is cool",
"doc_count" : 1
}
]
这是不正确的。然后我添加 fielddata true 并且一切正常:
curl -XPUT 'http://localhost:9200/example/_mapping/exampleworking' -d '
{
"properties": {
"myfield": {
"type": "text",
"fielddata": true
}
}
}'
然后聚合
curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
"aggs": {
"foobar": {
"terms": {
"field": "myfield"
}
}
}
}'
返回正确结果
"buckets" : [
{
"key" : "cool",
"doc_count" : 1
},
{
"key" : "is",
"doc_count" : 1
},
{
"key" : "this",
"doc_count" : 1
}
]
如何将此 fielddata=true 自动添加到所有文本字段的所有索引中?这甚至可能吗?在 elasticsearch 2.x 中,这是开箱即用的。
【问题讨论】:
-
我会回答自己的
标签: elasticsearch