前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
过滤器,并在其中嵌套您的术语
聚合。
You could try using a filter
aggregation based on a limit
filter, and nest your terms
aggregation in it.
请注意,限制在shard级应用(请参阅文档)。
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屋!