Operators on terms


(Juan Diego Calle) #1

I have an index with a type called vehicle, I am trying to create a bool query, so if the car built_year is 2001 OR 2003 and if the car has extras like radio AND air conditioner.
This is how I started
{
"query":{
"bool":{
"filter":
[
{
"terms":{
"built_year": [2001,2003]
},
"terms":{
"built_year": [2001,2003]
},
{
"match": {
"attributes.extras.id": 5
}
},
{
"match": {
"attributes.extras.id": 4
}
}
]
}
}

It works as intended, but I was wondering if there is a better way of doing this, specially if I wanna use OR between filters instead of AND, or the right ways is to use nested queries?
The basic idea is to do this programatically, so if I recieve an object filter and it comes with a property operator that has AND or OR values.


(Christoph) #2

Hi,

looks okay, only as far as I see you don't need to duplicate the terms query (might be a typo?). Unfortunately there is no operator parameter for the terms query, so if you want to be more flexible in combining them, you need individual term queries and combine them with other bool queries. This can all be done inside a parent bool query filter clause.


(Juan Diego Calle) #3

thanks a lot, yes this was a typo


(system) #4