我有2场和一些文档的索引,如下所示:Elasticsearch聚集多个领域分别
city team
=========================================
New York New York Knicks
New York Brooklyn Nets
New Orleans New Orleans Pelicans
我的目标是提供在这两个领域搜索的automplete,像这样:
Query: [ new ]
+----------------------+
| Cities |
+----------------------+
| New York |
| New Orleans |
+----------------------|
| Teams |
+----------------------|
| New York Knicks |
| New Orleans Pelicans |
+----------------------+
我的查询来筛选文件很简单:
"query": {
"bool": {
"should": [
{
"match_phrase_prefix": {
"city": "new"
}
},
{
"match_phrase_prefix": {
"team": "new"
}
}
]
}
}
然而,我我在聚合上遇到了麻烦。我的第一个方法是:
"aggs": {
"city": {
"terms": {
"field": "city.raw"
}
},
"team": {
"terms": {
"field": "team.raw"
}
}
}
(raw
是字段的not_analyzed
副本聚集的目的)
这并不起作用,因为Brooklyn Nets
被列入结果 - 它不应该:
"aggregations": {
"city": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "New York",
"doc_count": 2
},
{
"key": "New Orleans",
"doc_count": 1
}
]
},
"team": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "Brooklyn Nets",
"doc_count": 1
},
{
"key": "New Orleans Pelicans",
"doc_count": 1
},
{
"key": "New York Knicks",
"doc_count": 1
}
]
}
}
我不知道如何让它使用单个请求来工作。 这个例子只是说明性的,在真实场景中,我有更多的字段和文档可供搜索和聚合,所以向服务器发出多个请求并不是一个好主意,尤其是因为自动完成系统应该尽可能快。
任何帮助将不胜感激。
完美,它的工作!非常感谢你! – stefanobaldo