How to find records matching the result of a previous search using ElasticSearch Painless scripting

Hi,

I have the index I attached below.

Each doc in the index holds the name and height of Alice and the age at which the height was measured. Measurements taken at the age of 10 are flagged as "baseline_height_at_age_10": true

I need to do the following:

  1. Find the height of Alice at age 10.

Then, using the result from search #1:

  1. Return for Alice, all the records where the height is lower than her height at age 10.

So my question is: Can Painless do such type of search? and if it can, I'd appriciate if you could point me at a good example for that.

Also: Is ElasticSearch Painless even a good approach for this problem? Can you suggest another method?

I saw this thread [Search using result of previous search as parameter](http://Search using result of previous search as parameter) which, according to my understanding, suggests to do the 1st search, process the data and do an additional search. This basically translates to:

  1. Search the record where first_name=Alice and baseline_height_at_age_10=True

Process externally, to extract the value of height for Alice at age 10

  1. Search for Alice's records where her height is lower than the value calculated externally.

Is that the correct process to follow?

Thanks.

POST /test/_doc/alice_green_8_110
{
  "first_name": "Alice",
  "surname": "Green",
  "age": 8,
  "height": 110,
  "baseline_height_at_age_10": false
}

POST /test/_doc/alice_green_10_120
{
  "first_name": "Alice",
  "surname": "Green",
  "age": 10,
  "height": 120,
  "baseline_height_at_age_10": true
}

POST /test/_doc/alice_green_13_140
{
  "first_name": "Alice",
  "surname": "Green",
  "age": 13,
  "height": 140,
  "baseline_height_at_age_10": false
}

POST /test/_doc/alice_green_23_170
{
  "first_name": "Alice",
  "surname": "Green",
  "age": 23,
  "height": 170,
  "baseline_height_at_age_10": false
}

I don't think the Painless approach will work here. You cannot use the results of one query to execute a second query with Painless.

The two-step approach that you outline at the end of your post is the way to go.

1 Like

Thanks Abdon.

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