前 N 个结果的聚合 [英] Aggregation on top N results
问题描述
问题:
如果我搜索iphone",我会得到 400 个产品结果,我的产品类别聚合会返回结果集中的前 3 个类别.
If I search for "iphone" I get 400 product results and the product category aggregation I have returns the top 3 categories in the results set.
这些类别将包括智能手机、手机壳和手机配件.
Those categories would include smartphones, phone cases and mobile phone accessories.
如果我搜索iphone 6",我会得到 1400 个结果,因为额外的6"返回匹配更多产品.产品类别聚合现在返回所有这些结果的前 3 个类别.
If I search "iphone 6" I get 1400 results because of the extra "6" returns matches to more products. The product category aggregation now returns the top 3 categories for all those results.
前 3 个产品类别现在将是从电缆到计算机显示器的所有产品.
The top 3 product categories will now be everything from cables to computer monitors.
我需要做的是获得前 100 个结果的前 3 个类别.
What I need to do is get the top 3 categories for the top 100 results.
我尝试过的:
我尝试在顶级类别聚合中使用 top_hits
聚合,但它只返回每个类别中的顶级产品.
I've tried using the top_hits
aggregation within the top category aggregation but that only returns the top products in each category.
像这样:
{
"aggs": {
"product_categories": {
"terms": {
"field": "product_category",
"size": 10,
}
}
"aggs": {
"top-categories": {
"top_hits": {
"size" : 3
}
}
}
}
}
<小时>
我还尝试创建一个 top_hits
聚合,其中包含一个子聚合以获得顶级类别,但这也不起作用.
I've also tried creating a top_hits
aggregation with a sub-aggregation within to get the top categories but that doesn't work either.
{
"aggs": {
"top-categories": {
"top_hits": {
"size" : 100
}
"aggs": {
"product_categories": {
"terms": {
"field": "product_category",
"size": 3,
}
}
}
}
}
}
<小时>
谁能帮我解决这个问题?
Can anyone help me with this problem?
推荐答案
您可以尝试使用基于 limit
过滤器的 filter
聚合,并嵌套您的 term
聚合在其中.
You could try using a filter
aggregation based on a limit
filter, and nest your terms
aggregation in it.
请注意,限制应用于分片级别(请参阅文档).
Be aware that the limit is applied at shard level (see the documentation).
但是,这应该适合您的案例,查询如下:
However, this should do the job for your case, with a query like :
{
"aggs": {
"limit_results": {
"filter": {
"limit": {
"value": 100
}
},
"aggs": {
"product_categories": {
"terms": {
"field": "product_category",
"size": 10
}
}
}
}
}
}
这篇关于前 N 个结果的聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!