elasticsearch/docs/reference/scripting-languages/painless/painless-debugging.md
Colleen McGinnis b7e3a1e14b
[docs] Migrate docs from AsciiDoc to Markdown (#123507)
* delete asciidoc files

* add migrated files

* fix errors

* Disable docs tests

* Clarify release notes page titles

* Revert "Clarify release notes page titles"

This reverts commit 8be688648d.

* Comment out edternal URI images

* Clean up query languages landing pages, link to conceptual docs

* Add .md to url

* Fixes inference processor nesting.

---------

Co-authored-by: Liam Thompson <32779855+leemthompo@users.noreply.github.com>
Co-authored-by: Liam Thompson <leemthompo@gmail.com>
Co-authored-by: Martijn Laarman <Mpdreamz@gmail.com>
Co-authored-by: István Zoltán Szabó <szabosteve@gmail.com>
2025-02-27 17:56:14 +01:00

2.5 KiB
Raw Blame History

mapped_pages
https://www.elastic.co/guide/en/elasticsearch/painless/current/painless-debugging.html

Painless debugging [painless-debugging]

Debug.Explain [_debug_explain]

Painless doesnt have a REPL and while itd be nice for it to have one day, it wouldnt tell you the whole story around debugging painless scripts embedded in Elasticsearch because the data that the scripts have access to or "context" is so important. For now the best way to debug embedded scripts is by throwing exceptions at choice places. While you can throw your own exceptions (throw new Exception('whatever')), Painlesss sandbox prevents you from accessing useful information like the type of an object. So Painless has a utility method, Debug.explain which throws the exception for you. For example, you can use _explain to explore the context available to a script query.

PUT /hockey/_doc/1?refresh
{"first":"johnny","last":"gaudreau","goals":[9,27,1],"assists":[17,46,0],"gp":[26,82,1]}

POST /hockey/_explain/1
{
  "query": {
    "script": {
      "script": "Debug.explain(doc.goals)"
    }
  }
}

Which shows that the class of doc.first is org.elasticsearch.index.fielddata.ScriptDocValues.Longs by responding with:

{
   "error": {
      "type": "script_exception",
      "to_string": "[1, 9, 27]",
      "painless_class": "org.elasticsearch.index.fielddata.ScriptDocValues.Longs",
      "java_class": "org.elasticsearch.index.fielddata.ScriptDocValues$Longs",
      ...
   },
   "status": 400
}

You can use the same trick to see that _source is a LinkedHashMap in the _update API:

POST /hockey/_update/1
{
  "script": "Debug.explain(ctx._source)"
}

The response looks like:

{
  "error" : {
    "root_cause": ...,
    "type": "illegal_argument_exception",
    "reason": "failed to execute script",
    "caused_by": {
      "type": "script_exception",
      "to_string": "{gp=[26, 82, 1], last=gaudreau, assists=[17, 46, 0], first=johnny, goals=[9, 27, 1]}",
      "painless_class": "java.util.LinkedHashMap",
      "java_class": "java.util.LinkedHashMap",
      ...
    }
  },
  "status": 400
}

Once you have a class you can go to Painless API Reference to see a list of available methods.