I am using Elasticsearch 2.4 and I am trying to get a query that behaves like the following SQL statement:
SELECT * FROM countries WHERE continent='Europe' and (country='Andorra' OR cities in ['Madrid'])
In Elasticsearch 1.5 I got it working using the following query:
{
"query": {
"filtered": {
"filter": {
"term": {
"continent": "Europe"
}
},
"query": {
"bool": {
"should": [
{
"nested": {
"path": "cities",
"query": {
"match": {
"cities.name": "Madrid"
}
}
}
},
{
"match": {
"country": "Andorra"
}
}
]
}
}
}
}
}
But seems like in the version 2.x the param "filtered" has been deprecated. I have tried to build the query using the new approach using filter instead, but it doesn't find the nested values correctly. This was the resulting query:
{
"query": {
"bool": {
"filter": [
{
"term": {
"continent": "Europe"
}
}
],
"should": [
{
"nested": {
"path": "cities",
"query": {
"match": {
"cities.name": "Madrid"
}
}
}
},
{
"match": {
"country": "Andorra"
}
}
]
}
}
}
This is the data I am trying to get back:
{
"_index":"countries",
"_type":"item",
"_id":"123",
"_version":1,
"found":true,
"_source":{
"country": "Spain",
"cities": [
{
"id_city": 2133,
"name": "Madrid"
},
{
"id_city": 8382,
"name": "Barcelona"
}
]
}
}
Does somebody know the proper way to achieve this?