我有 elasticsearch 集群 - 版本 1.3.0。该集群的索引文档没有启用 _source,因此在检索命中时,我通常基于请求中的“fields”参数。
现在我正在为重复分组功能实现 top_hits
聚合。我想获取 top_hits
结果中的字段,但现在无法执行此操作,因为默认情况下在我的映射中未启用 _source
。您能否建议我一个选项/解决方法来实现此目标而不更改现有映射?
我在top-hits aggregation doc中没有找到它。非常感谢任何帮助。
谢谢!
最佳答案
使用script fields :
"aggs": {
"sample": {
"top_hits": {
"size": 1,
"script_fields": {
"field1": {
"script": "doc['field1']"
},
"field2": {
"script": "doc['field2']"
}
...
}
}
}
}
但是,如果 field1
或field2
进行分析后,您需要一个子字段来保留 not_analyzed
字段的版本。为什么?因为,如果以任何方式分析法线场,doc['field']
调用将返回分析后的术语,而不是索引的初始内容。
类似这样的事情:
"mappings": {
"test": {
"_source": {
"enabled": false
},
"properties": {
"field1": {
"type": "string",
"fields": {
"notAnalyzed": {
"type": "string",
"index": "not_analyzed"
}
}
}
}
}
}
以及查询:
"aggs": {
"sample": {
"top_hits": {
"size": 1,
"script_fields": {
"field1": {
"script": "doc['field1.notAnalyzed']"
}
}
}
}
}
关于elasticsearch - 如何获取top_hits聚合中的字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32754266/