如何在Elasticsearch中同时按父字段和嵌套字段排序? [英] How to sort In Elasticsearch by parent and nested fields at the same time?

查看:93
本文介绍了如何在Elasticsearch中同时按父字段和嵌套字段排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要同时在Elasticsearch中按父字段和嵌套字段进行排序.我的数据是这样的:

I need to sort in Elasticsearch by parent and nested fields at the same time. My data are like this:

[
    {
        "id": "1",
        "rank": 8,
        "price": 12.45,
        "offers": [
            {
                "id": "777",
                "rank": 12,
                "price": 45.75
            }
        ]
    },
    {
        "id": "2",
        "rank": 35,
        "price": 5.95,
        "offers": null
    }
]

我需要对 rank 上的结果进行排序,以使当 offers 不为 null 时,我应该使用嵌套的offer.rank 值,否则我应该采用父 rank 值.我尝试了此脚本,但没有用:

I need to sort the results on rank in such a manner that when the offers are not null I should take nested offers.rank value, otherwise I should take the parent rank value. I tried this script, but it did not work:

    "sort": [
        {
            "_script": {
                "script": {
                    "source": "doc['offers'].size()==0 ? doc['rank'].value : doc['offers.rank'].value",
                    "lang": "painless"
                },
                "type": "number",
                "order": "asc"
            }
        }
    ]

它不起作用可能是因为 offers.rank 来自嵌套的 offers 对象,该对象无法访问.但是我不知道如何处理它-如果我为整个脚本添加一个嵌套条件,那么我的父值 doc ['rank'].value 将不再可用.可以同时在此处按父字段和嵌套字段进行排序吗?

It did not work probably because offers.rank is from a nested offers object, which is not accessible. But I do not understand how to handle it - if I add a nested condition for the whole script, then my parent value doc['rank'].value will not be accessible anymore. It is possible to sort here by parent and nested fields at the same time?

推荐答案

您正确地假设无法访问父级.现在,您可以

You're right in assuming that the parent rank would not be accessible. Now, you could either

  1. 创建2个单独的排序对象",一个用于父级,一个用于嵌套商品,然后使用
  1. create 2 separate sort 'objects', one for the parent, one for the nested offers, and then sum the results up using the sort mode or
  2. iterate on the _source instead:

{
  "sort": [
    {
      "_script": {
        "script": {
          "source": """
          if (params._source.offers instanceof ArrayList
              && params._source.offers.length > 0) {
            return params._source['offers'][0].rank;
          }
          return params._source.rank
          """,
          "lang": "painless"
        },
        "type": "number",
        "order": "asc"
      }
    }
  ]
}

请注意,由于此处使用的是报价" ArrayList ,因此您需要某种机制来选择排名.这取决于您-我只是访问了第一个报价的排名,您可能想对数组列表进行排序&选择最高的一个...

Note that since we're working with an 'offers' ArrayList here, you'll need some sort of mechanism to pick a rank. That's up to you -- I've simply accessed the first offer's rank and you may want to sort the array list & pick the highest one...

如果您愿意的话,这里是单线飞机:

Here's a one-liner if that's what you prefer:

params._source.offers instanceof ArrayList && params._source.offers.length > 0 ? params._source['offers'][0].rank : params._source.rank

这篇关于如何在Elasticsearch中同时按父字段和嵌套字段排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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