Dear Elasticsearch training experts,
I've been going through the Elasticsearch Engineer class. In the lab 2.4 "Advanced search" there is a task as follows:
Write a script query that returns all blogs where the authors.last_name.keyword field starts with the letter "K". There are multiple ways to write this code, but you can use the startsWith() function. You should get 371 hits. HINT: you will get an error is you do not check for empty values of authors.last_name.keyword. You could either check that the size() function of doc['authors.last_name.keyword'] is greater than 0, or incorporate the exists query in your bool qeury.
The suggested answer is a script like this:
if(doc['authors.last_name.keyword'].size() > 0 && doc['authors.last_name.keyword'].value.startsWith("K"))
return true;
The script returns 371 results. However, this answer is not complete. Each blog may have multiple authors. The suggested script picks the first author and checks if the name of this author starts with a K. This is not covering a case of having 2, 3 or more authors, and the one whose name starts with a K not being first in the list.
A script similar to the following would produce a more full answer. It would return 433 results:
def authors = doc["authors.last_name.keyword"];
for (int i = 0; i < authors.size(); i++) {
if (authors.get(i).startsWith("K")) {
return true;
}
}
return false;
Also, there's a couple of typos in the task text ("will get an error is you" and "qeury").
I was looking for somewhere to submit feedback / create an issue in the class environment, but I could not find anything. I emailed training@elastic.co and was advised to post in the forum here.
Thank you.