I have a bunch of objects like this one in Elastic:
{
"id" : "b397ab9c-2bab-4cce-a419-ba6b23545772",
"name": "Alice",
// other fields omitted
"foo" : [
{
"id" : "1",
"bar" : 20
},
{
"id" : "2",
"bar" : 20
}
]
},
{
"id" : "826784cb-7dcd-4d2c-b444-a466032a7b06",
"name": "Bob",
// other fields omitted
"foo" : [
{
"id" : "1",
"bar" : 15
}
]
}
I am trying to make a score based on any elements in foo
of which the id
is 2
. So in the above objects, Alice has a score of 20 and Bob has a score of 0 (as there's no element with id 2
in his foo
array. I'm however a bit stuck in how to have my script_score look for the specific element. Here is my query:
"query": {
"function_score": {
"score_mode": "max",
"functions": [
{
"filter": {
"term": {
"foo.id": {
"value": "2"
}
}
},
"script_score": {
"script": {
"source": "doc['foo'].values /* what goes here? */",
"params": {
}
}
}
}
]
}
}