Scripting - How to find functions / methods in documentation of Elasticsearch

Hello,
can anyone help me where I can find in documentation of Elasticsearch 7.x statements like:

"".equals(ctx['field'])

or

ctx['field'].empty

or

doc.containsKey('field.keyword')) 

Hi,

To find out which variables are available in a specific context have a look here.
To see which attributes and methods exist for a type you can check the documentation here

Best regards
Wolfram

Thank you @Wolfram_Haussig for your answer.

I'm creating script in pipeline, so the context is Ingest processor.
So the access for variable is ctx['field'].

Now I would like to know which functions can I use on this field.
I was looking in SHARED API page but I cannot find empty - ctx['field'].empty.

So I've continued with searching for "".equals(ctx['field']) and I've found out ELASTICSEACH PAINLESS API. But I'm not sure whether am I right.

Hi @vasek can you open this query in the Elastic Stack -> Elasticsearch

https://discuss.elastic.co/c/elastic-stack/elasticsearch/6

Topic, I think you will get more relevant replies there. The "Elastic Training" Topic is intended for questions about specific Trainings, rather than the underlying products themselves

Hope this helps

KR

1 Like

Hello,

According to the documentation ctx is a map:

ctx ( Map )

Contains extracted JSON in a Map and List structure for the fields that are part of the document.

So now we look for the map type in the second link which is the AbstractMap. this contains a containsKey function:

Is this what you were looking for?

Best regards
Wolfram

1 Like

Great! This is exactly what I was looking for. Thank you @Wolfram_Haussig!

I put this question to this group because I cannot find it in Study Materials (preparation for ES certification). I've read that only Elasticsearch documentation is available so I can know where to search :slight_smile:
For the next I'll query on another forum. Thank you.

1 Like

If I can add to Wolfram's excellent post, one of the more difficult things about working with Painless, in my opinion, is knowing what kind of object you are dealing with, and what the methods are that are available to you. Let me share my approach to that finding that out.

Painless has a Debug.explain method. You can pass it an object, and the method will tell you what kind of object you're dealing with in the exception that it throws as a response. For example, if you execute the following:

POST /_ingest/pipeline/_simulate
{
  "pipeline": {
    "processors": [
      {
        "script": {
          "source": "Debug.explain(ctx['field'])"
        }
      }
    ]
  },
  "docs": [
    {
      "_source": {
        "field": "abc"
      }
    }
  ]
}

The response will tell you that ctx['field'] is a java.lang.String. This is an ordinary Java string, and you can refer to the Java documentation to see what methods are available.

That's what "".equals(ctx['field']) is: it's Java syntax to compare ctx['field'] with an empty string. In other words: it checks whether ctx['field'] is an empty string.

2 Likes

Thank you @abdon for explanation. It's a good trick :wink:

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