Reputation: 2167
Using the data from the current version of Elasticsearch: The Definitive Guide, that is:
[{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}, {
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}, {
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}]
I'm trying to run a simple computation (I've enabled Groovy scripting) using the request data:
{
"query": {
"filtered": {
"filter": {
"range": {
"max_age": {
"gt": 150
}
}
}
}
},
"script_fields": {
"max_age": {
"script": "_source.age * 5"
}
}
}
But ES isn't returning any data. How can I search over computed fields? It's even better if I don't need to enable scripting.
Upvotes: 4
Views: 1896
Reputation: 217474
script_fields
are computed after the query phase, i.e. during the fetch phase, so you cannot reference script fields inside your queries.
What you need to achieve can still be done, though, by using a script
filter, like this:
{
"query": {
"bool": {
"must": {
"script": {
"script": {
"inline": "doc['age'].value * factor > max_age",
"params": {
"factor": 5,
"max_age": 150
}
}
}
}
}
}
}
Upvotes: 5