ElasticSearch:查询每个类别的N个项目 [英] ElasticSearch: query for N items of each category

查看:54
本文介绍了ElasticSearch:查询每个类别的N个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在ElasticSearch(5.5)中有一个商品索引,其中每个商品都有一个类别"字段,例如手套",外套",毛巾".

I have an index of goods in ElasticSearch (5.5), of them every product has a field "category", like "GLOVES", "COAT", "TOWEL".

通过 terms 查询,我可以选择属于几个类别的项目,例如

With the terms query I can select items belonging to several categories, e.g.

{
    "terms": {
        "div_id": ["COAT", "DRESS", "JACKET"]
    }
}

现在的问题是,我希望每个类型都包含多个项目,例如不少于3个(假设答案的总大小为15条记录).

Now the problem is that I want to have in response several items of each type, say, not less than 3 (given that total size of answer is 15 records).

我不知道如何执行此操作.使用给定的直接"方式,它可以返回任何类别的任何数字.我得到的最接近的结果是添加random_score,该结果使结果多样化",但这取决于每种类别在索引中所占的百分比.

And I have no clear idea how to do this. With the given "straight" way it may return any number from any category. The closest I get is to add random_score which makes result "diverse", but it then depends on how many percents every category takes in the index.

我怀疑应该采用不同的方法,但是貌似无法猜出正确的关键字.

I suspect there should be different approach, but can't guess correct keywords, seemingly.

提前谢谢!

推荐答案

您可能要尝试agg记录的热门歌曲

You may want to try top hits agg documented here.

这篇关于ElasticSearch:查询每个类别的N个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆