Note: I had originally posted this question a little differently and it wasn't worth updating as after reading I learned a bit more.
Requirement
Search for documents and calculate a custom score based on nested elements within the document.
Structure
{
"mappings": {
"book": {
"properties": {
"title": { "type": "string", "index": "not_analyzed" },
"topics": {
"type": "nested",
"properties": {
"title": { "type": "string", "index": "not_analyzed" },
"weight": { "type": "int" }
}
}
}
}
}
}
Sample Query
{
"query": {
"function_score": {
"query": {
"term": { "title": "The Magical World of Spittle" }
},
"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
}
}
}
}
}
}
Isolated Painless
int score = 0;
for(int i = 0; i < doc['topics'].values.length; i++) {
score += doc['topics'][i].weight;
}
return score;
The Error
No field found for [topics] in mapping with types [book]
The Questions
- What's wrong?
- What to do?
params._source
was exactly what I needed. – Dualpurpose