ElasticSearch无痛脚本:如何在嵌套对象数组中进行迭代 [英] ElasticSearch Painless script: How to iterate in an array of Nested Objects

查看:452
本文介绍了ElasticSearch无痛脚本:如何在嵌套对象数组中进行迭代的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用function_scorescript_score创建脚本. 我有几个文档的rankings字段是type="nested". 该字段的映射为:

I am trying to create a script using the script_score of the function_score. I have several documents whose rankings field is type="nested". The mapping for the field is:

"rankings": {
        "type": "nested",
        "properties": {
          "rank1": {
            "type": "long"
          },
          "rank2": {
            "type": "float"
          },
          "subject": {
            "type": "text"
          }
        }
      }

示例文档为:

"rankings": [
{
    "rank1": 1051,
    "rank2": 78.5,
    "subject": "s1"
},
{
    "rank1": 45,
    "rank2": 34.7,
    "subject": "s2"
}]

我要实现的是迭代嵌套的排名对象.实际上,我需要使用for循环才能找到特定的subject并使用rank1, rank2进行计算. 到目前为止,我使用了类似的方法,但是它似乎不起作用(抛出编译错误):

What I want to achieve is to iterate over the nested objects of rankings. Actually, I need to use i.e. a for loop in order to find a particular subject and use the rank1, rank2 to compute something. So far, I use something like this but it does not seem to work (throwing a Compile error):

"function_score": {
"script_score": {
    "script": {
        "lang": "painless",
        "inline": 
                 "sum = 0;"
                 "for (item in doc['rankings_cug']) {"
                     "sum = sum + doc['rankings_cug.rank1'].value;"
                 "}"
         }
    }
}

我还尝试了以下选项:

    使用:而不是in
  1. for循环:for (item:doc['rankings'])没有成功.
  2. for循环使用in,但尝试遍历对象的特定元素,即rank1:for (item in doc['rankings.rank1'].values),该元素实际上已编译,但似乎找到了.
  1. for loop using : instead of in: for (item:doc['rankings']) with no success.
  2. for loop using in but trying to iterate over a specific element of the object, i.e. the rank1: for (item in doc['rankings.rank1'].values), which actually compile but it seems that it finds a zero-length array of rank1.

我已经阅读到_source元素可以返回类似JSON的对象,但是据我发现Search查询不支持该元素.

I have read that _source element is the one which can return JSON-like objects, but as far as I found out it is not supported in Search queries.

您能给我一些如何进行此操作的想法吗?

Can you please give me some ideas of how to proceed with that?

非常感谢.

推荐答案

您可以通过params._source访问_source.这将起作用:

You can access _source via params._source. This one will work:

PUT /rankings/result/1?refresh
{
  "rankings": [
    {
      "rank1": 1051,
      "rank2": 78.5,
      "subject": "s1"
    },
    {
      "rank1": 45,
      "rank2": 34.7,
      "subject": "s2"
    }
  ]
}

POST rankings/_search

POST rankings/_search
{
  "query": {
    "match": {
      "_id": "1"
    }
  },
  "script_fields": {
    "script_score": {
      "script": {
        "lang": "painless",
        "inline": "double sum = 0.0; for (item in params._source.rankings) { sum += item.rank2; } return sum;"
      }
    }
  }
}

DELETE rankings

这篇关于ElasticSearch无痛脚本:如何在嵌套对象数组中进行迭代的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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