Script_fields calcul in nested


(rria) #1

I created new Index like This

   POST times/available
    {
      "name": "Nursery one",
      "available": [
        {
          "number_places": 1,
          "date": "2018-04-01"
        },
        {
          "number_places": 1,
          "date": "2018-04-14"
        },
        {
          "number_places": 1,
          "date": "2018-04-30"
        },
        {
          "number_places": 1,
          "date": "2018-05-01"
        },
        {
          "number_places": 1,
          "date": "2018-05-24"
        },
        {
          "number_places": 1,
          "date": "2018-06-05"
        }
      ]
    }
My Query Like this
POST times/available/_search
{
  "query": {
    "match_all": {}
  },
  "script_fields": {
    "scoreee": {
      "script": {
        "lang": "painless",
        "source": """
            int total = 200;
            for (int i = 0; i < doc['available'].length; ++i) {
              total += doc['available'];
            }
            return total;
          """
      }
    }
  }
}

But not working
"failures" : [
{
"shard" : 0,
"index" : "times",
"node" : "9ykUreS0SLiGV9sRBNpZ5g",
"reason" : {
"type" : "script_exception",
"reason" : "runtime error",
"script_stack" : [
"""
total += doc['available.number_places'][0].value;
}

""",
            "                                          ^---- HERE"
          ],
          "script" : """
            int total = 200;
            for (int i = 0; i < 20; ++i) {
              total += doc['available.number_places'][0].value;
            }
            return total;
""",
          "lang" : "painless",
          "caused_by" : {
            "type" : "illegal_argument_exception",
            "reason" : "dynamic getter [java.lang.Long, value] not found"
          }
        }
      }
    ]

(Alexander Reelsen) #2

Hey,

you are not taking into account that doc['available'] already is an array, so you cannot access the number_places field directly. Try doc['available'][0].number_places.value (on top of my head, untested)

--Alex


(system) closed #3

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.