Elasticsearch how to set fielddata: true for all text fields

Viewed 9380

Can somebody tell me please how to set fielddata true for all text fields. I need to sort according them and it throws me an error Fielddata is disabled on text fields by default Here is piece of mapping where street or city are text fields which throws me this error.

"address": {
                    "properties": {
                        "city": {
                            "type": "text",
                            "boost": 5,
                            "analyzer": "suggestion"
                        },
                        "zip": {
                            "type": "text"
                        },
                        "street": {
                            "type": "text",
                            "boost": 7,
                            "analyzer": "suggestion"
                        }
1 Answers

you need to update the mapping of your field for this purpose you can use the put mapping API of elasticsearch.

PUT yourindexName/_mapping/_doc
{
  "properties": {
     "yourfield": { 
       "type":     "text",
       "fielddata": true
     }
  }
}

Although it is not advisable to enable the fielddata on text fields as they can consume a lot of heap space.

I suggest you to go through this link explaining the rationale behind this in detail. elasticsearch fielddata

Also alternatively you can try the below mapping for your fields:

 "yourfield": { 
    "type": "text",
    "fields": {
      "keyword": {
        "type": "keyword"
      }
    }
  }

Instead of plain text only...i hope it will serve your purpose.

Happy coding :)

Related