我正在编写一个python脚本以在elasticsearch索引中获取唯一值。我正在使用术语聚合来获取唯一值及其计数。但是,当我将字段列表传递给脚本时,我意识到某些字段存储为
"abc" : {
"type" : "keyword"
}
有些存储为"xyz" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword"
}
}
}
在术语汇总期间,我使用查询{
"aggs" : {
"abc" : {
"terms" : {
"field" : "abc"
}
}
}, "size":0
}
但是,当在“xyz”上使用此查询时,它将给出错误Fielddata is disabled on text fields by default. Set fielddata=true on [description] 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.
要运行“xyz”查询,我需要向其中添加“.keyword”,但随后“abc”将无法运行。有什么方法可以检查哪个字段属于哪种类型,然后使用if / else相应地更新查询?
最佳答案
您可以同时使用-字段是可聚合且可搜索的,而无需.keyword
表示法。只需根据错误消息建议调整映射即可:
"xyz" : {
"type" : "text",
"fielddata": true
}
然后重新编制索引,您就可以开始了。至于是否有查询时间检查来确定哪些字段是-没有。 ElasticSearch的核心原则之一是预先定义和定义字段类型,以便对它们进行适当索引,以便优化搜索/聚合。因此,假设在查询时知道哪些字段属于哪种类型。
关于python - Elasticsearch术语聚合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64714070/