Fielddata is disabled on text fields by default when upgrading from 2.x to 5.1

I have problem that I updated from elasticsearch 2.x to 5.1. However, some of my data does not work in newer elasticsearch because of this "Fielddata is disabled on text fields by default" https://www.elastic.co/guide/en/elasticsearch/reference/5.1/fielddata.html before 2.x it was enabled it seems.

Is there way to enable fielddata automatically to text fields?

I tried code like this

curl -XPUT http://localhost:9200/_template/template_1 -d '
{
  "template": "*",
  "mappings": {
    "_default_": {
      "properties": {
        "fielddata-*": {
          "type": "text",
          "fielddata": true
        }
      }
    }
  }
}'

but it looks like elasticsearch does not understand wildcard there in field name. Temporary solution to this is that I am running python script every 30 minutes, scanning all indices and adding fielddata=true to fields which are new.

The problem is that I have string data like "this is cool" in elasticsearch.

curl -XPUT 'http://localhost:9200/example/exampleworking/1' -d '
{
    "myfield": "this is cool"
}'

when trying to aggregate that:

curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
    "aggs": {
        "foobar": {
            "terms": {
                "field": "myfield"
            }
        }
    }   
}'

"Fielddata is disabled on text fields by default. Set fielddata=true on [myfield]"

that elasticsearch documentation suggest using .keyword instead of adding fielddata. However, that is not returning data what I want.

curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
    "aggs": {
        "foobar": {
            "terms": {
                "field": "myfield.keyword"
            }
        }
    }   
}'

returns:

  "buckets" : [
    {
      "key" : "this is cool",
      "doc_count" : 1
    }
  ]

which is not correct. Then I add fielddata true and everything works:

curl -XPUT 'http://localhost:9200/example/_mapping/exampleworking' -d '
{
  "properties": {
        "myfield": {
            "type": "text",
            "fielddata": true
        }
    }
}'

and then aggregate

curl 'http://localhost:9200/example/_search?pretty=true' -d '
{
    "aggs": {
        "foobar": {
            "terms": {
                "field": "myfield"
            }
        }
    }   
}'

return correct result

  "buckets" : [
    {
      "key" : "cool",
      "doc_count" : 1
    },
    {
      "key" : "is",
      "doc_count" : 1
    },
    {
      "key" : "this",
      "doc_count" : 1
    }
  ]

How I can add this fielddata=true automatically to all indices to all text fields? Is that even possible? In elasticsearch 2.x this is working out of the box.

i will answer to myself

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

this is doing what i want. Now all indexes have default settings fielddata true

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.