How to get the current entry of a foreach pipeline instruction using painless scripting?










0















I am trying to count JSON objects based on a flag.
To do so, I created two foreach pipelines iterating over my objects.



I want to count all the objects in the documents array for which the field "count" is set to true.



POST _ingest/pipeline/_simulate

"pipeline":
"description":"...",
"processors":[

"set":
"field":"specific.docCount",
"value":0

,

"foreach":
"field":"data.status.transactions",
"processor":
"foreach":
"field":"_ingest._value.documents",
"processor":
"script":
"lang":"painless",
"inline":"if (ctx.count) ctx.specific.docCount += 1"






]
,
"docs":[

"_source":
"data":
"status":
"transactions":[

"id":"123",
"documents":[

"count": true
,

"count": false

]

]




]



I'm getting the following error :




"docs": [

"error":
"root_cause": [

"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"header":
"processor_type": "foreach"


],
"type": "exception",
"reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
"caused_by":
"type": "illegal_argument_exception",
"reason": "ScriptException[runtime error]; nested: NullPointerException;",
"caused_by":
"type": "script_exception",
"reason": "runtime error",
"script_stack": [
"if (ctx.count) ",
" ^---- HERE"
],
"script": "if (ctx.count) ctx.stats.docCount += 1",
"lang": "painless",
"caused_by":
"type": "null_pointer_exception",
"reason": null


,
"header":
"processor_type": "foreach"



]



This foreach pipeline doc suggests to use ctx to refer to the ingest document, but I am not sure how to use this in my case.



How can I retrieve the current "foreach entry" in my painless script ?










share|improve this question




























    0















    I am trying to count JSON objects based on a flag.
    To do so, I created two foreach pipelines iterating over my objects.



    I want to count all the objects in the documents array for which the field "count" is set to true.



    POST _ingest/pipeline/_simulate

    "pipeline":
    "description":"...",
    "processors":[

    "set":
    "field":"specific.docCount",
    "value":0

    ,

    "foreach":
    "field":"data.status.transactions",
    "processor":
    "foreach":
    "field":"_ingest._value.documents",
    "processor":
    "script":
    "lang":"painless",
    "inline":"if (ctx.count) ctx.specific.docCount += 1"






    ]
    ,
    "docs":[

    "_source":
    "data":
    "status":
    "transactions":[

    "id":"123",
    "documents":[

    "count": true
    ,

    "count": false

    ]

    ]




    ]



    I'm getting the following error :




    "docs": [

    "error":
    "root_cause": [

    "type": "exception",
    "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
    "header":
    "processor_type": "foreach"


    ],
    "type": "exception",
    "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
    "caused_by":
    "type": "illegal_argument_exception",
    "reason": "ScriptException[runtime error]; nested: NullPointerException;",
    "caused_by":
    "type": "script_exception",
    "reason": "runtime error",
    "script_stack": [
    "if (ctx.count) ",
    " ^---- HERE"
    ],
    "script": "if (ctx.count) ctx.stats.docCount += 1",
    "lang": "painless",
    "caused_by":
    "type": "null_pointer_exception",
    "reason": null


    ,
    "header":
    "processor_type": "foreach"



    ]



    This foreach pipeline doc suggests to use ctx to refer to the ingest document, but I am not sure how to use this in my case.



    How can I retrieve the current "foreach entry" in my painless script ?










    share|improve this question


























      0












      0








      0








      I am trying to count JSON objects based on a flag.
      To do so, I created two foreach pipelines iterating over my objects.



      I want to count all the objects in the documents array for which the field "count" is set to true.



      POST _ingest/pipeline/_simulate

      "pipeline":
      "description":"...",
      "processors":[

      "set":
      "field":"specific.docCount",
      "value":0

      ,

      "foreach":
      "field":"data.status.transactions",
      "processor":
      "foreach":
      "field":"_ingest._value.documents",
      "processor":
      "script":
      "lang":"painless",
      "inline":"if (ctx.count) ctx.specific.docCount += 1"






      ]
      ,
      "docs":[

      "_source":
      "data":
      "status":
      "transactions":[

      "id":"123",
      "documents":[

      "count": true
      ,

      "count": false

      ]

      ]




      ]



      I'm getting the following error :




      "docs": [

      "error":
      "root_cause": [

      "type": "exception",
      "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
      "header":
      "processor_type": "foreach"


      ],
      "type": "exception",
      "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
      "caused_by":
      "type": "illegal_argument_exception",
      "reason": "ScriptException[runtime error]; nested: NullPointerException;",
      "caused_by":
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
      "if (ctx.count) ",
      " ^---- HERE"
      ],
      "script": "if (ctx.count) ctx.stats.docCount += 1",
      "lang": "painless",
      "caused_by":
      "type": "null_pointer_exception",
      "reason": null


      ,
      "header":
      "processor_type": "foreach"



      ]



      This foreach pipeline doc suggests to use ctx to refer to the ingest document, but I am not sure how to use this in my case.



      How can I retrieve the current "foreach entry" in my painless script ?










      share|improve this question
















      I am trying to count JSON objects based on a flag.
      To do so, I created two foreach pipelines iterating over my objects.



      I want to count all the objects in the documents array for which the field "count" is set to true.



      POST _ingest/pipeline/_simulate

      "pipeline":
      "description":"...",
      "processors":[

      "set":
      "field":"specific.docCount",
      "value":0

      ,

      "foreach":
      "field":"data.status.transactions",
      "processor":
      "foreach":
      "field":"_ingest._value.documents",
      "processor":
      "script":
      "lang":"painless",
      "inline":"if (ctx.count) ctx.specific.docCount += 1"






      ]
      ,
      "docs":[

      "_source":
      "data":
      "status":
      "transactions":[

      "id":"123",
      "documents":[

      "count": true
      ,

      "count": false

      ]

      ]




      ]



      I'm getting the following error :




      "docs": [

      "error":
      "root_cause": [

      "type": "exception",
      "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
      "header":
      "processor_type": "foreach"


      ],
      "type": "exception",
      "reason": "java.lang.IllegalArgumentException: ScriptException[runtime error]; nested: NullPointerException;",
      "caused_by":
      "type": "illegal_argument_exception",
      "reason": "ScriptException[runtime error]; nested: NullPointerException;",
      "caused_by":
      "type": "script_exception",
      "reason": "runtime error",
      "script_stack": [
      "if (ctx.count) ",
      " ^---- HERE"
      ],
      "script": "if (ctx.count) ctx.stats.docCount += 1",
      "lang": "painless",
      "caused_by":
      "type": "null_pointer_exception",
      "reason": null


      ,
      "header":
      "processor_type": "foreach"



      ]



      This foreach pipeline doc suggests to use ctx to refer to the ingest document, but I am not sure how to use this in my case.



      How can I retrieve the current "foreach entry" in my painless script ?







      elasticsearch elasticsearch-painless






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 14 '18 at 13:23







      Louis

















      asked Nov 14 '18 at 13:08









      LouisLouis

      557620




      557620






















          1 Answer
          1






          active

          oldest

          votes


















          0














          I ended up doing the whole thing in the painless script.



          POST _ingest/pipeline/_simulate

          "pipeline":
          "description":"...",
          "processors":[

          "set":
          "field":"stats.docCount",
          "value":0

          ,

          "script":
          "lang":"painless",
          "inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) def documents = transaction.documents; for (def document : documents)if (document.count != null && document.count)ctx.stats.docCount += 1"


          ]
          ,
          "docs":[

          "_source":
          "data":
          "status":
          "transactions":[

          "id":"123",
          "documents":[

          "count":true
          ,

          "count":false

          ]

          ]



          ,

          "_source":
          "data":
          "status":
          "transactions":[

          "id":"234",
          "documents":[

          "count":true
          ,

          "count":true

          ]

          ]



          ,

          "_source":
          "data":
          "status":
          "transactions":[

          "id":"345",
          "documents":[


          ,

          "count":true

          ]

          ]




          ]



          Output:




          "docs": [

          "doc":
          "_id": "_id",
          "_index": "_index",
          "_type": "_type",
          "_source":
          "data":
          "status":
          "transactions": [

          "documents": [

          "count": true
          ,

          "count": false

          ],
          "id": "123"

          ]

          ,
          "stats":
          "docCount": 1

          ,
          "_ingest":
          "timestamp": "2018-11-14T13:46:29.963Z"


          ,

          "doc":
          "_id": "_id",
          "_index": "_index",
          "_type": "_type",
          "_source":
          "data":
          "status":
          "transactions": [

          "documents": [

          "count": true
          ,

          "count": true

          ],
          "id": "234"

          ]

          ,
          "stats":
          "docCount": 2

          ,
          "_ingest":
          "timestamp": "2018-11-14T13:46:29.963Z"


          ,

          "doc":
          "_id": "_id",
          "_index": "_index",
          "_type": "_type",
          "_source":
          "data":
          "status":
          "transactions": [

          "documents": [
          ,

          "count": true

          ],
          "id": "345"

          ]

          ,
          "stats":
          "docCount": 1

          ,
          "_ingest":
          "timestamp": "2018-11-14T13:46:29.963Z"



          ]






          share|improve this answer






















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300992%2fhow-to-get-the-current-entry-of-a-foreach-pipeline-instruction-using-painless-sc%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown

























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I ended up doing the whole thing in the painless script.



            POST _ingest/pipeline/_simulate

            "pipeline":
            "description":"...",
            "processors":[

            "set":
            "field":"stats.docCount",
            "value":0

            ,

            "script":
            "lang":"painless",
            "inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) def documents = transaction.documents; for (def document : documents)if (document.count != null && document.count)ctx.stats.docCount += 1"


            ]
            ,
            "docs":[

            "_source":
            "data":
            "status":
            "transactions":[

            "id":"123",
            "documents":[

            "count":true
            ,

            "count":false

            ]

            ]



            ,

            "_source":
            "data":
            "status":
            "transactions":[

            "id":"234",
            "documents":[

            "count":true
            ,

            "count":true

            ]

            ]



            ,

            "_source":
            "data":
            "status":
            "transactions":[

            "id":"345",
            "documents":[


            ,

            "count":true

            ]

            ]




            ]



            Output:




            "docs": [

            "doc":
            "_id": "_id",
            "_index": "_index",
            "_type": "_type",
            "_source":
            "data":
            "status":
            "transactions": [

            "documents": [

            "count": true
            ,

            "count": false

            ],
            "id": "123"

            ]

            ,
            "stats":
            "docCount": 1

            ,
            "_ingest":
            "timestamp": "2018-11-14T13:46:29.963Z"


            ,

            "doc":
            "_id": "_id",
            "_index": "_index",
            "_type": "_type",
            "_source":
            "data":
            "status":
            "transactions": [

            "documents": [

            "count": true
            ,

            "count": true

            ],
            "id": "234"

            ]

            ,
            "stats":
            "docCount": 2

            ,
            "_ingest":
            "timestamp": "2018-11-14T13:46:29.963Z"


            ,

            "doc":
            "_id": "_id",
            "_index": "_index",
            "_type": "_type",
            "_source":
            "data":
            "status":
            "transactions": [

            "documents": [
            ,

            "count": true

            ],
            "id": "345"

            ]

            ,
            "stats":
            "docCount": 1

            ,
            "_ingest":
            "timestamp": "2018-11-14T13:46:29.963Z"



            ]






            share|improve this answer



























              0














              I ended up doing the whole thing in the painless script.



              POST _ingest/pipeline/_simulate

              "pipeline":
              "description":"...",
              "processors":[

              "set":
              "field":"stats.docCount",
              "value":0

              ,

              "script":
              "lang":"painless",
              "inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) def documents = transaction.documents; for (def document : documents)if (document.count != null && document.count)ctx.stats.docCount += 1"


              ]
              ,
              "docs":[

              "_source":
              "data":
              "status":
              "transactions":[

              "id":"123",
              "documents":[

              "count":true
              ,

              "count":false

              ]

              ]



              ,

              "_source":
              "data":
              "status":
              "transactions":[

              "id":"234",
              "documents":[

              "count":true
              ,

              "count":true

              ]

              ]



              ,

              "_source":
              "data":
              "status":
              "transactions":[

              "id":"345",
              "documents":[


              ,

              "count":true

              ]

              ]




              ]



              Output:




              "docs": [

              "doc":
              "_id": "_id",
              "_index": "_index",
              "_type": "_type",
              "_source":
              "data":
              "status":
              "transactions": [

              "documents": [

              "count": true
              ,

              "count": false

              ],
              "id": "123"

              ]

              ,
              "stats":
              "docCount": 1

              ,
              "_ingest":
              "timestamp": "2018-11-14T13:46:29.963Z"


              ,

              "doc":
              "_id": "_id",
              "_index": "_index",
              "_type": "_type",
              "_source":
              "data":
              "status":
              "transactions": [

              "documents": [

              "count": true
              ,

              "count": true

              ],
              "id": "234"

              ]

              ,
              "stats":
              "docCount": 2

              ,
              "_ingest":
              "timestamp": "2018-11-14T13:46:29.963Z"


              ,

              "doc":
              "_id": "_id",
              "_index": "_index",
              "_type": "_type",
              "_source":
              "data":
              "status":
              "transactions": [

              "documents": [
              ,

              "count": true

              ],
              "id": "345"

              ]

              ,
              "stats":
              "docCount": 1

              ,
              "_ingest":
              "timestamp": "2018-11-14T13:46:29.963Z"



              ]






              share|improve this answer

























                0












                0








                0







                I ended up doing the whole thing in the painless script.



                POST _ingest/pipeline/_simulate

                "pipeline":
                "description":"...",
                "processors":[

                "set":
                "field":"stats.docCount",
                "value":0

                ,

                "script":
                "lang":"painless",
                "inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) def documents = transaction.documents; for (def document : documents)if (document.count != null && document.count)ctx.stats.docCount += 1"


                ]
                ,
                "docs":[

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"123",
                "documents":[

                "count":true
                ,

                "count":false

                ]

                ]



                ,

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"234",
                "documents":[

                "count":true
                ,

                "count":true

                ]

                ]



                ,

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"345",
                "documents":[


                ,

                "count":true

                ]

                ]




                ]



                Output:




                "docs": [

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [

                "count": true
                ,

                "count": false

                ],
                "id": "123"

                ]

                ,
                "stats":
                "docCount": 1

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"


                ,

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [

                "count": true
                ,

                "count": true

                ],
                "id": "234"

                ]

                ,
                "stats":
                "docCount": 2

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"


                ,

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [
                ,

                "count": true

                ],
                "id": "345"

                ]

                ,
                "stats":
                "docCount": 1

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"



                ]






                share|improve this answer













                I ended up doing the whole thing in the painless script.



                POST _ingest/pipeline/_simulate

                "pipeline":
                "description":"...",
                "processors":[

                "set":
                "field":"stats.docCount",
                "value":0

                ,

                "script":
                "lang":"painless",
                "inline":"def transactions = ctx.data.status.transactions; for (def transaction : transactions) def documents = transaction.documents; for (def document : documents)if (document.count != null && document.count)ctx.stats.docCount += 1"


                ]
                ,
                "docs":[

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"123",
                "documents":[

                "count":true
                ,

                "count":false

                ]

                ]



                ,

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"234",
                "documents":[

                "count":true
                ,

                "count":true

                ]

                ]



                ,

                "_source":
                "data":
                "status":
                "transactions":[

                "id":"345",
                "documents":[


                ,

                "count":true

                ]

                ]




                ]



                Output:




                "docs": [

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [

                "count": true
                ,

                "count": false

                ],
                "id": "123"

                ]

                ,
                "stats":
                "docCount": 1

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"


                ,

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [

                "count": true
                ,

                "count": true

                ],
                "id": "234"

                ]

                ,
                "stats":
                "docCount": 2

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"


                ,

                "doc":
                "_id": "_id",
                "_index": "_index",
                "_type": "_type",
                "_source":
                "data":
                "status":
                "transactions": [

                "documents": [
                ,

                "count": true

                ],
                "id": "345"

                ]

                ,
                "stats":
                "docCount": 1

                ,
                "_ingest":
                "timestamp": "2018-11-14T13:46:29.963Z"



                ]







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 14 '18 at 13:47









                LouisLouis

                557620




                557620



























                    draft saved

                    draft discarded
















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid


                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.

                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300992%2fhow-to-get-the-current-entry-of-a-foreach-pipeline-instruction-using-painless-sc%23new-answer', 'question_page');

                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Top Tejano songwriter Luis Silva dead of heart attack at 64

                    ReactJS Fetched API data displays live - need Data displayed static

                    政党