Skip to main content
Phil_I_AM
Gold ⭐️
June 5, 2023
Solved

Need to plug a variable into a elasticsearch query

I am trying to roll my own search_after pagination query by saving the last sort values to a db and then retrieving them on the next time I run the query.  Thing is if there are no sort values from the db this means this is a new query and I skip the search_after clause in my query. If I do retrieve a set of sort values from the db then I need to plug them into the query. I am having trouble with the EL statement. This is what I have:

 

 

 

{
"query": {
"bool": {
"must": [
{"term": {"key1": "value1"}}
],
"must_not": [
{"exists": {"key2": "value2"}}
],
"filter": [
{"range": {"@timestamp": {"lte": "now-1w"}}}
]
}
}
${search_after:isEmpty():not():ifElse(', \"search_after\": "${search_after}"', '')},
"sort": [
{"@timestamp": "asc"},
{"value3": "asc"}
]
}

 

 

 The ${search_after} variable defined as  '[ts int milliseconds, "a key value string"]'  Including the sq brackets.

 

The error I am receiving is:
Unknown key for a VALUE_STRING in search_after line 15 col 20 type parsing exception

 

Does anyone know how to write the variable correctly?

 

Phil_I_AM
Phil_I_AMAnswer
Gold ⭐️
June 6, 2023

I solved my problem. The solution is to not wrap the variable with double quotes.

${search_after:isEmpty():not():ifElse(', \"search_after\": ${search_after}', '')},

 

2 replies

DianaTorres
Community Manager
Community Manager
June 5, 2023

@Phil_I_AM Welcome to the Cloudera Community!

To help you get the best possible solution, I have tagged our NiFi experts @SAMSAL and @MattWho  who may be able to assist you further.

Please keep us updated on your post, and we hope you find a satisfactory solution to your query.

Translate
Regards, Diana Torres, Senior Community Moderator
Phil_I_AM
Phil_I_AMAuthorAnswer
Gold ⭐️
June 6, 2023

I solved my problem. The solution is to not wrap the variable with double quotes.

${search_after:isEmpty():not():ifElse(', \"search_after\": ${search_after}', '')},

 

Translate