I'd like to iterate over a set of child elements on a search in order to calculate a value using Painless and then return that information with the rest of the search result. I decided to hijack the score calculation.
Basic index:
{
"mappings": {
"book": {
"properties": {
"title": { "type": "string", "index": "not_analyzed" },
"topics": {
"type": "nested",
"properties": {
"weight": { "type": "int" },
"title": { "type": "string" }
}
}
}
}
}
}
Assuming a sample document like this in /books/book/
:
{
"title": "Title 1",
"topics": [
{ "weight": 123, "title": "foo" },
{ "weight": 4, "title": "bar" },
{ "weight": 72, "title": "baz" }
]
}
I formatted my query with a script like this:
{
"query": {
"function_score": {
"query": {
"term": { "title": "QT-0003" }
},
"script_score": {
"script": {
"lang": "painless",
"inline": "int score = 0; for(int i = 0; i < doc['topics'].values.length; i++) { score += doc['topics'][i].weight; } return score;",
"params": {
"default_return_value": 100
}
}
}
}
}
}
My problem is this:
No field found for [topics] in mapping with types [book]