V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
klakekent
V2EX  ›  Elasticsearch

如何对查询内容进行二次排序

  •  
  •   klakekent · 2018-06-03 13:39:25 +08:00 · 3401 次点击
    这是一个创建于 2125 天前的主题,其中的信息可能已经有所发展或是发生改变。
    现在遇到这样一个需求

    我的索引里面保存了从各个网站爬回来对产品以及价格信息,基本信息是这样

    product_id, source_id,price,discount

    所以我通过 collapse 对 product_id 进行折叠分组,并且是通过 price 来排序的,所以得到对结果就是每个 product 的最低价,但是我现在还想对这个返回对结果再次排序,比如通过 discount 来排序,应该怎么做呢?
    mysql 大概是这样

    select * from (select * from product group by product_id order by price asc ) t order by discount desc


    用 aggregation 目前没有思路
    5 条回复    2018-06-04 14:20:35 +08:00
    WhyAreYouSoSad
        1
    WhyAreYouSoSad  
       2018-06-03 13:55:21 +08:00 via iPhone
    重新定义一下这个问题,是不是按 a 字段排序,当值相等的时候,再用 b 排序。
    klakekent
        2
    klakekent  
    OP
       2018-06-03 20:27:09 +08:00
    @WhyAreYouSoSad 不是

    这个需求是首先选出所有产品在各个网站的最低价,所以我用了 collapse 通过 order by price desc 来取顶层数据

    取出来的结果集可以在按照折扣百分比 折扣数量等条件再来排序一次,这个二次排序我就不知道怎么弄了
    WhyAreYouSoSad
        3
    WhyAreYouSoSad  
       2018-06-03 21:27:43 +08:00
    @klakekent
    了解下权值排序,然后想下你自己说的二次排序的定义是什么,什么条件下进行。这算是需求的一部分。
    9066v
        4
    9066v  
       2018-06-04 09:38:05 +08:00
    select * from product group by product_id order by price asc, 用这样的语句是无法获取到正确的数据的,group by 查询出来的数据是按照主键排序的。
    klakekent
        5
    klakekent  
    OP
       2018-06-04 14:20:35 +08:00
    @WhyAreYouSoSad 其实就是我现在可以通过 collapse 来按照最低价格分组 拿到每个 product 的最低价的那条数据详细信息, 我还想再基于这个结果集,再来一次排序
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1147 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 109ms · UTC 23:00 · PVG 07:00 · LAX 16:00 · JFK 19:00
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.