I am starting with the painless, have not much experience, and would need a bit assistance. For my painless script below, I am getting the following error in the Pinless Lab and do not really know how to correct it. Can anyone help please?
Error:
Unhandled Exception illegal_argument_exception invalid sequence of tokens near ['{']. Stack: [ "... e['c0108_trace*'].search({\n "query": {\n ...", " ^---- HERE" ]
Script:
def params = params['_source'];
def traceId = params['TraceId'];
def endpoint = params['endpoint'];
def signalFxToken = params['SignalFx_Token'];
// Step 1: Search for entries with the given TraceId
def searchResponse = ctx._source['c0108_trace*'].search({
"query": {
"term": {
"TraceId": traceId
}
},
"_source": ["_id"]
});
// Step 2: Loop over the _id and forward the data to the endpoint
for (def hit : searchResponse['hits']['hits']) {
def id = hit['_source']['_id'];
// Step 3: Search for entries with the given _id
def dataResponse = ctx['c0108_trace*'].search({
"query": {
"term": {
"_id": id
}
}
});
// Remove metadata from the data
def data = dataResponse['hits']['hits'][0]['_source'];
data.remove('_id');
data.remove('_index');
data.remove('_score');
data.remove('_type');
// Forward the data to the external endpoint
def response = endpoint(data, {
"headers": {
"X-SF-Token": signalFxToken
}
});
}
Also, the second question would be, how to call this script, after I store it, with the parameters, out of the Dev Console. I tried the following, where a) and b) did not really work. The c) worked, but then I would hope there is a way to make it less complicated, without contructing a search over the index, which is known to the script already:
a)
POST _scripts/get_trace_spans/_execute
{
"params": {
"TraceId": "cf510c436d7e043e23060c4b22e5dd8f",
"endpoint": "https://ingest.eu0.signalfx.com/v2/trace/otlp",
"SignalFx_Token": "xxxxxxx"
}
}
b)
POST _scripts/get_trace_spans/_execute
{
"script": {
"lang": "painless",
"source": "get_trace_spans",
"params": {
"TraceId": "cf510c436d7e043e23060c4b22e5dd8f",
"endpoint": "https://ingest.eu0.signalfx.com/v2/trace/otlp",
"SignalFx_Token": "xxxxxxx"
}
}
}
c)
GET /c0108_trace*/_search
{
"query": {
"script_score": {
"query": {
"match": {
"message": "some message"
}
},
"script": {
"id": "get_trace_spans",
"params": {
"TraceId": "cf510c436d7e043e23060c4b22e5dd8f",
"endpoint": "https://ingest.eu0.signalfx.com/v2/trace/otlp",
"SignalFx_Token": "xxxxxxxxx"
}
}
}
}
}
The problem is in the following code block here. You have to create hashes/objects using square brackets:
However, this is not going to work like you expect as you cannot issue a search from within a script, i.e.,
ctx['c0108_trace*'].search(...)The logic of querying, retrieving the results, acting on them and querying again, needs to be done inside your own client logic. Painless is just meant to act in the context of the indexed document (e.g., update values, add/remove fields, etc)