How to use Script as Stored Procedure?


(Grigorichev Denis) #1

Hi, All!

I'm novice in ES. I've read about scripts (painless) and want to use it as stored procedures in a RDBMS. It's possible, right? I want

  1. receive some docs by call my script (it should does some selection)
  2. It's important. Process all docs and to build value some fields in every doc.
    I was try to make scrip for receive string message as example. But I receive error "can't cast String to boolean" only. When I've create script by example - I receive same error:
    "caused_by": {
    "type": "class_cast_exception",
    "reason": "java.lang.Double cannot be cast to java.lang.Boolean"
    }
    What I do wrong? Help, please!

(Thiago Souza) #2

Writing scripts can be painful (regardless it is called "painless"). Can you please post here what is the query with script that you are using so we can review it?

Also, if you can, provide a sample document that this script should work with.


(Grigorichev Denis) #3

I was trying to use different ways:

curl localhost:9200/_scripts/scr2 -H 'Content-Type: application/json' -d @scr2.painless where scr2.painless contains:
{
"script": {
"lang": "painless",
"source": "Hello"
}
}

than call curl -X GET localhost:9200/_scripts/painless/scr2 and catch error.

And try to use this example:
https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#_request_examples

with same results.
The script was stored correctly, but I couldn't execute it.


(Thiago Souza) #4

The value of Hello is not a valid painless script. What were you trying to do?


(Grigorichev Denis) #5

In first it was as:

return "Hello";


(Grigorichev Denis) #6

I want

  1. Select some docs as recurcive search in the documents tree.

  2. Recurcive search and update some property a documents (to compound a names of all parents)


(Thiago Souza) #7

Can you please attach the script that returned this original error from this topic?


(Grigorichev Denis) #8

Example from manual:

POST /_scripts/calc-test
 {
  "script": {
    "lang": "painless",
    "source": "Math.log(_score * 2) + params.my_modifier"
  }
}

Response: 
{
    "acknowledged": true
}

And use script:
GET /_search

{
  "query": {
    "script": {
      "script": {
        "id": "calc-test",
        "params": {
          "my_modifier": 2
        }
      }
    }
  }
}

Response:

{
    "error": {
        "root_cause": [
            {
                "type": "script_exception",
                "reason": "runtime error",
                "script_stack": [
                    "Math.log(100500 * 2) + params.my_modifier",
                    "^---- HERE"
                ],
                "script": "calc-test",
                "lang": "painless"
            }
        ],
        "type": "search_phase_execution_exception",
        "reason": "all shards failed",
        "phase": "query",
        "grouped": true,
        "failed_shards": [
            {
                "shard": 0,
                "index": "fiasaddress",
                "node": "XJVa4hLbSImjmYD_4sKTHA",
                "reason": {
                    "type": "script_exception",
                    "reason": "runtime error",
                    "script_stack": [
                        "Math.log(100500 * 2) + params.my_modifier",
                        "^---- HERE"
                    ],
                    "script": "calc-test",
                    "lang": "painless",
                    "caused_by": {
                        "type": "class_cast_exception",
                        "reason": "java.lang.Double cannot be cast to java.lang.Boolean"
                    }
                }
            }
        ]
    },
    "status": 500
}`

(Ryan Ernst) #9

Which documentation page do you see that example on? Your search request is using a filter script, which must return a boolean value (true if the current document should match the query, false otherwise). But the stored script you are using looks like a scoring script, meant to be used within a function score query.


(Grigorichev Denis) #10

As I wrote early - https://www.elastic.co/guide/en/elasticsearch/reference/current/modules-scripting-using.html#_request_examples

I want to reach this goals:

  1. Select some docs as recurcive search in the documents tree.
  2. Recurcive search and update some property a documents (to compound a names of all parents)

(system) #11

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