ElasticSearch Painless script: How to iterate in an array of Nested Objects

前端 未结 3 1075
挽巷
挽巷 2021-02-07 08:40

I am trying to create a script using the script_score of the function_score. I have several documents whose rankings field is type=\

3条回答
  •  滥情空心
    2021-02-07 09:13

    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
    

提交回复
热议问题