美文网首页
实现美团搜索中相同商家折叠显示

实现美团搜索中相同商家折叠显示

作者: 郭彦超 | 来源:发表于2020-04-11 23:00 被阅读0次

我们平时在用美团或饿了么搜索点餐时,会发现相同商家的商品会进行折叠显示,按照一定的打分规则排序后取前两个商品进行展示,其它则放在查看更多里;这么做可以为其它商家或商品带来更多的曝光机会。

image.png

下面我们通过ES查询试着模仿上面的显示效果,在我们已构建好的索引库搜索title包含火锅的商品,并对返回结果中相同商家商品进行折叠,每个商家将显示一个收藏最多的和一个最近上架的商品;查询如下:

GET search_data/_search
{
    "_source": ["title","user_collect","create_time","id"], 
    "query": {
        "match": {
            "title": "火锅"
        }
    },
    "collapse" : {
        "field" : "create_user", 
        "max_concurrent_group_searches":4,
        "inner_hits": [
            {
                "_source": ["title","user_collect","create_time","id"], 
                "name": "most_liked",  
                "size": 1,
                "sort": ["user_collect"]
            },
            {
               "_source": ["title","user_collect","create_time","id"], 
                "name": "most_recent", 
                "size": 1,
                "sort": [{ "create_time": "desc" }]
            }
        ]
    } 
}


  • collapse
    collapse查询中需要指定要聚合的field,这里按create_user进行聚合
    max_concurrent_group_searches用来指定折叠查询的最大并发查询数,建议设置为节点数或索引分片数
    inner_hits支持数组,允许按照多种方式折叠,每种方式都可以设置单独的排序规则,另外需要注意的是不前仅支持sort排序,还不支持function_score
    size用来控制折叠后的头部商品数量
    不建议设置太多的分组方式,这样会降低整体查询性能

上述查询结果:

     "inner_hits" : {
          "most_liked" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "search_data",
                  "_type" : "_doc",
                  "_id" : "458066",
                  "_score" : null,
                  "_ignored" : [
                    "xk_last_create_time"
                  ],
                  "_source" : {
                    "user_collect" : 23,
                    "create_time" : "2017-11-15 14:00:20",
                    "id" : "458066",
                    "title" : "火锅店开业促销"
                  },
                  "sort" : [
                    23
                  ]
                }
              ]
            }
          },
          "most_recent" : {
            "hits" : {
              "total" : {
                "value" : 2,
                "relation" : "eq"
              },
              "max_score" : null,
              "hits" : [
                {
                  "_index" : "search_data",
                  "_type" : "_doc",
                  "_id" : "458320",
                  "_score" : null,
                  "_ignored" : [
                    "xk_last_create_time"
                  ],
                  "_source" : {
                    "user_collect" : 66,
                    "create_time" : "2017-11-15 17:49:34",
                    "id" : "458320",
                    "title" : "火锅店开业"
                  },
                  "sort" : [
                    1510768174000
                  ]
                }
              ]
            }
          }
        }

相关文章

网友评论

      本文标题:实现美团搜索中相同商家折叠显示

      本文链接:https://www.haomeiwen.com/subject/xowpmhtx.html