elasticsearch - 默认情况下,elasticsearch 中的文本字段禁用 Fielddata

标签 elasticsearch

我从 elasticsearch 2.x 更新到 5.1 时遇到问题。但是,我的一些数据在较新的 elasticsearch 中不起作用,因为这个“默认情况下在文本字段上禁用字段数据”https://www.elastic.co/guide/en/elasticsearch/reference/5.1/fielddata.html在 2.x 之前,它似乎已启用。

有没有办法让 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 中有像“this is cool”这样的字符串数据。

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 而不是添加 fielddata。但是,这并没有返回我想要的数据。

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 中,这是开箱即用的。

最佳答案

我会自己回答

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

关于elasticsearch - 默认情况下,elasticsearch 中的文本字段禁用 Fielddata,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41704450/

相关文章:

插入后立即查询的 Elasticsearch 奇怪行为

elasticsearch - Elasticsearch 中的聚合+排序+分页

java - 在 Elasticsearch 中忽略 JsonIgnore

ruby-on-rails - 使用Elasticsearch的Array字段应使用什么索引标记器?

c# - 在 serilog 中使用 C# 参数

c# - 建议查询时间长

logging - 如何删除 Logstash 过滤器中所有具有 NULL 值的字段

php - Plastic/Elasticsearch-搜索具有空值的条目

elasticsearch - ElasticSearch聚合缺少数据

node.js - Elasticsearch我正在获得特定价格范围内的唯一可用尺寸,Elasticsearch返回所有值