2016-07-05 68 views
1

我有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 
      } 
     ] 
    } 
} 

我不知道如何让它使用单个请求来工作。 这个例子只是说明性的,在真实场景中,我有更多的字段和文档可供搜索和聚合,所以向服务器发出多个请求并不是一个好主意,尤其是因为自动完成系统应该尽可能快。

任何帮助将不胜感激。

回答

1

你需要一个过滤聚集过滤文件进行汇总根据你在查询本身的过滤器:

"aggs": { 
    "city": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "query": { 
       "match_phrase_prefix": { 
        "city": "new" 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggs": { 
     "cities": { 
      "terms": { 
      "field": "city.raw" 
      } 
     } 
     } 
    }, 
    "team": { 
     "filter": { 
     "bool": { 
      "must": [ 
      { 
       "query": { 
       "match_phrase_prefix": { 
        "team": "new" 
       } 
       } 
      } 
      ] 
     } 
     }, 
     "aggs": { 
     "cities": { 
      "terms": { 
      "field": "team.raw" 
      } 
     } 
     } 
    } 
    } 
+0

完美,它的工作!非常感谢你! – stefanobaldo

0

您的查询,

"query": { 
    "bool": { 
     "should": [ 
      { 
       "match_phrase_prefix": { 
        "city": "new" 
       } 
      }, 
      { 
       "match_phrase_prefix": { 
        "team": "new" 
       } 
      } 
     ] 
    } 
} 

回报与“城市文档:新约克队:布鲁克林篮网队“。因为“城市”字段具有前缀“新”,即使“团队”字段没有。

我想当你使用聚合的文件与“城市:纽约队:布鲁克林篮网”得到它计数。由于“城市:纽约”,“团队:布鲁克林网队”文档包含在查询的结果集中,并以桶形式计算。

如果你想检查这个,请将minimum_should_match设置为2。

+0

无论如何谢谢你的回答,但它并没有帮助我。 – stefanobaldo