Elasticsearch按嵌套对象计数排序 [英] Elasticsearch sort by count of nested objects

查看:103
本文介绍了Elasticsearch按嵌套对象计数排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下ES内容,基本上是网站中包含嵌套商品的产品列表。

I have the following ES content, basically a list of product with nested offers from sites.

{
"product": {
  "ean": "abc",
  "features": {
     "product_type": "DVD player"
  },
  "color": "Black",
  "manufacturer": "Sony",
  "sitedetails": [
     {
        "name": "amazon.com",
        "sku": "zzz",
        "url": "http://www.amazon.com/dp/zzz"
     },
     {
        "name": "bestbuy.com",
        "sku": "aaa",
        "url": "http://www.bestbuy.com/aaa"
     }
  ],
  "category": "Portable DVD Players"
 },
 product: { etc etc},
 product: { etc etc},
}

如何按站点详细信息的数量排序?

How can I sort by the count of sitedetails?

tnx!

推荐答案

从问题示例中看,它看起来像是单个文档,但单个json不可能文档具有多个字段具有相同名称的相同级别。

From the example in the question, it seems like as a single document but it is not possible for a single json document to have multiple fields at the same level with the same name.

因此,假设每个产品字段代表一个不同的顶级文档

Hence, going ahead with the assumption that each product field represents a different top-level document

{
  "size": 10,
  "from": 0,
  "query": {
    "match_all": {} 
  },
  "sort" : {
    "_script" : {
      "script": "doc['sitedetails'].values.size()",
      "order": "desc",
      "type" : "number"
    }
  }
}

这篇关于Elasticsearch按嵌套对象计数排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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