qwa*_*waz 4 faceted-search elasticsearch
我有以下类别聚合
{
"aggs": {
"category": {
"terms": { "field": "category.name" }
}
}
}
// results
"category": {
"buckets": [
{
"key": "computer & office",
"doc_count": 365
},
{
"key": "home & garden",
"doc_count": 171
},
{
"key": "consumer electronics",
"doc_count": 49
},
]
}
Run Code Online (Sandbox Code Playgroud)
如何传递附加字段(例如category.id类别存储桶),以便我可以category.id根据用户是否单击特定聚合进行查询。我不太清楚如何查询聚合,是否有任何直接的方法,或者您必须进行新的查询并将存储桶传递key给查询过滤器。
在 上使用子聚合category.id,在查看结果时您会做更多的工作,但我认为这比更改映射更好:
{
"aggs": {
"name": {
"terms": {
"field": "name"
},
"aggs": {
"id": {
"terms": {
"field": "id"
}
}
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
结果如下所示:
"aggregations": {
"name": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": "consumer electronics",
"doc_count": 2,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 2,
"doc_count": 2
}
]
}
},
{
"key": "computer & office",
"doc_count": 1,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 5,
"doc_count": 1
}
]
}
},
{
"key": "home & garden",
"doc_count": 1,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 1,
"doc_count": 1
}
]
}
},
{
"key": "whatever",
"doc_count": 1,
"id": {
"doc_count_error_upper_bound": 0,
"sum_other_doc_count": 0,
"buckets": [
{
"key": 3,
"doc_count": 1
}
]
}
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
您仍然拥有类别名称,但现在您还可以将id第二次聚合中的类别名称作为根存储桶中的子存储桶:
"key": "consumer electronics",
...
"id": {
...
"buckets": [
{
"key": 2,
"doc_count": 2
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3079 次 |
| 最近记录: |