【问题标题】:Fielddata is disabled on text fields by default in elasticsearch默认情况下,Elasticsearch 中的文本字段禁用 Fielddata
【发布时间】: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


【解决方案1】:

我会回答自己的

curl -XPUT http:/localhost:9200/_template/template_1 -d '
{
  "template": "*",
  "mappings": {
    "_default_": {
      "dynamic_templates": [
        {
          "strings2": {
            "match_mapping_type": "string",
            "mapping": {
              "type": "text",
              "fielddata": true
            }
          }
        }
      ]
    }
  }
}'

这是做我想做的事。现在所有索引都有默认设置 fielddata true

【讨论】:

  • 嗨,我也遇到了这个问题。我应用了您的模板,然后重新索引,但仍然从 kibana 得到错误,它通过聚合一些文本字段来绘制表格。正确的步骤是:创建模板->重新索引现有索引,所有文本字段都将具有“fielddata = true”?
  • 添加"fielddata": true 允许聚合文本字段,但这在规模上存在性能问题。更好的解决方案是添加第二个字段以使用多字段映射。看我的回答。
【解决方案2】:

添加 "fielddata": true 允许聚合文本字段,但这在规模上存在性能问题。更好的解决方案是使用多字段映射。

不幸的是,这在 Elasticsearch 的文档中隐藏得有点深,在 fielddata 映射参数下的警告中:https://www.elastic.co/guide/en/elasticsearch/reference/current/text.html#before-enabling-fielddata

这是一个完整的示例,说明这如何有助于术语聚合,截至 2021 年 4 月 24 日在 Elasticsearch 7.12 上进行了测试:

映射(在 ES7 中,在“放置索引模板”请求等主体的 mappings 属性下):

{
    "properties": {
        "bio": {
            "type": "text",
            "fields": {
                "keyword": {
                    "type": "keyword"
                }
            }
        }
    }
}

四个文档被索引:

{
    "bio": "Dogs are the best pet."
}
{
    "bio": "Cats are cute."
}
{
    "bio": "Cats are cute."
}
{
    "bio": "Cats are the greatest."
}

聚合查询:

{
    "size": 0,
    "aggs": {
        "bios_with_cats": {
            "filter": {
                "match": {
                    "bio": "cats"
                }
            },
            "aggs": {
                "bios": {
                    "terms": {
                        "field": "bio.keyword"
                    }
                }
            }
        }
    }
}

聚合查询结果:

{
  "took": 2,
  "timed_out": false,
  "_shards": {
    "total": 2,
    "successful": 2,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": {
      "value": 4,
      "relation": "eq"
    },
    "max_score": null,
    "hits": []
  },
  "aggregations": {
    "bios_with_cats": {
      "doc_count": 3,
      "bios": {
        "doc_count_error_upper_bound": 0,
        "sum_other_doc_count": 0,
        "buckets": [
          {
            "key": "Cats are cute.",
            "doc_count": 2
          },
          {
            "key": "Cats are the greatest.",
            "doc_count": 1
          }
        ]
      }
    }
  }
}

基本上,这个聚合表示“在 bios 像'猫'的文档中,每个不同的 bio 有多少?”排除bio属性中没有“cats”的一个文档,然后将剩余文档分组到桶中,其中一个有一个文档,另一个有两个文档。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-05-19
    • 1970-01-01
    • 1970-01-01
    • 2023-03-30
    • 1970-01-01
    • 1970-01-01
    • 2018-04-02
    • 1970-01-01
    相关资源
    最近更新 更多