如何修复 ElasticSearch 'Fielddata is disabled on text fields by default' 对于 keyword 字段
问题:
你在 ElasticSearch 中有一个名为例如 patterns 的 keyword 类型字段。但是,当你查询此字段的聚合时,例如
es_fielddata_example.py
es.search(index="strings", body={
"size": 0,
"aggs" : {
"patterns" : {
"terms" : { "field" : "pattern" }
}
}
})你看到此错误消息:
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.'解决方案
此错误消息令人困惑,因为你已经有 keyword 字段。但是,ElasticSearch fielddata 文档告诉我们你需要在查询中使用 pattern.keyword 而不是仅使用 pattern。
完整示例:
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