2017-09-15 153 views
0

我有一些数据:弹性搜索分组唯一值

[{ 
    "article_id": 257710962, 
    "category_id": "category1" 
}, 
{ 
    "article_id": 257710962, 
    "category_id": "category2" 
}, 
{ 
    "article_id": 257713968, 
    "category_id": "category3" 
}, 
{ 
    "article_id": 257717376, 
    "category_id": "category4" 
}] 

我应该怎样做一个查询得到的结果是这样

[{ 
    "article_id": 257710962, 
    "category_id": ["category1", "category2"] 
}, 
{ 
    "article_id": 257713968, 
    "category_id": "category3" 
}, 
{ 
    "article_id": 257717376, 
    "category_id": "category4" 
}] 

我需要做的查询,因为我使用分页在我的页面上。所以,防止重复是必须的。 (我使用的article_id作为我的主键)

回答

0

如果每个文件源是这样

"_source": { 
       "article_id": 257710962, 
       "category_id": "category2" 
      } 

那么你可以尝试条款聚集排名靠前的聚集

GET /my_index/my_type/_search 
{ 
    "size": 0, 
    "aggs": { 
     "agg1": { 
     "terms": { 
      "field": "article_id" 
     }, 
     "aggs": { 
      "agg2": { 
       "top_hits": {} 
      } 
     } 
     } 
    } 
} 

作进一步说明:https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-metrics-top-hits-aggregation.html

+0

“from”键怎么样?对于分页 – kudaponi

+0

我不认为如果聚合_'from'_键将起作用。 您需要猜测预期结果总数的可靠大小,然后以数组 [https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations]传递_'size'_参数-bucket-方面,aggregation.html#搜索聚合-桶方面,聚集近似-数] – torzonhot