How to fix ElasticSearch 'Fielddata is disabled on text fields by default' for keyword field
Problem:
You have a field in ElasticSearch named e.g. patterns
of type keyword
. However, when you query for an aggregation of this field e.g.
es_fielddata_example.py
es.search(index="strings", body={
"size": 0,
"aggs" : {
"patterns" : {
"terms" : { "field" : "pattern" }
}
}
})
you see this error message:
es_fielddata_error.txt
elasticsearch.exceptions.RequestError: RequestError(400, 'search_phase_execution_exception', 'Fielddata is disabled on text fields by default. Set fielddata=true on [pattern] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.'
Solution
This error message is confusing since you already have a keyword
field. However, the ElasticSearch fielddata documentation tells us that you need to to use pattern.keyword
in the query instead of just pattern
.
Full example:
es_fielddata_fix.py
es.search(index="strings", body={
"size": 0,
"aggs" : {
"patterns" : {
"terms" : { "field" : "pattern.keyword" }
}
}
})
Check out similar posts by category:
Databases, ElasticSearch
If this post helped you, please consider buying me a coffee or donating via PayPal to support research & publishing of new posts on TechOverflow