简体   繁体   中英

How to use OR in a filtered query with Elasticsearch 2.x

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?

See if this works:

 {
        "query" : {
            "bool" : {
                "should" : [{
                        "nested" : {
                            "path" : "cities",
                            "query" : {
                                "match" : {
                                    "cities.name" : "Madrid"
                                }
                            }
                        }
                    }, {
                        "match" : {
                            "country" : "Spain"
                        }
                    }
                ],
                "filter" :
                [{
                        "term" : {
                            "continent" : "Europe"
                        }
                    }
                ]
            }
        }
    }

Actually I got it working like this:

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "continent": "Europe"
          }
        }
      ],
      "must": [
        {
          "bool": {
            "should": [
              {
                "nested": {
                  "path": "cities",
                  "query": {
                    "match": {
                      "cities.name": "Madrid"
                    }
                  }
                }
              },
              {
                "match": {
                  "country": "Andorra"
                }
              }
            ]
          }
        }
      ]
    }
  }
}

I know.. weird, but if the query doesn't match any term using "should" instead "must" on the main query will return a lot of random results from Europe .

If your bool query has a filter or must clause, then the should clause doesn't need to have any matches to get results. If it's standalone, then it needs it needs to have 1 match. It's in the docs at: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

So, give this a try and force at least 1 should match using "minimum_should_match": 1 .

{
  "query": {
    "bool": {
      "filter": [
        {
          "term": {
            "continent": "Europe"
          }
        }
      ],
      "should": [
        {
          "nested": {
            "path": "cities",
            "query": {
              "match": {
                "cities.name": "Madrid"
              }
            }
          }
        },
        {
          "match": {
            "country": "Andorra"
          }
        }
      ],
      "minimum_should_match": 1
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM