0

我正在使用 Elasticsearch 2.4,我试图获得一个行为类似于以下 SQL 语句的查询:

SELECT * FROM countries WHERE continent='Europe' and (country='Andorra' OR cities in ['Madrid'])

在 Elasticsearch 1.5中,我使用以下查询使其工作:

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

但似乎在 2.x 版本中,参数 "filtered"已被弃用。我尝试使用新方法使用过滤器来构建查询,但它没有正确找到嵌套值。这是结果查询:

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

这是我试图取回的数据:

{
    "_index":"countries",
    "_type":"item", 
    "_id":"123",
    "_version":1,
    "found":true,
    "_source":{
        "country": "Spain",
        "cities": [
                    {
                        "id_city": 2133, 
                        "name": "Madrid"
                    }, 
                    {
                        "id_city": 8382, 
                        "name": "Barcelona"
                    }
                  ] 
    }
}

有人知道实现这一目标的正确方法吗?

4

3 回答 3

0

实际上我是这样工作的:

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

我知道.. 很奇怪,但是如果查询与在主查询中使用“should”而不是“must”的任何术语不匹配,则会从Europe.

于 2016-09-22T00:55:44.627 回答
0

如果您的bool查询有filterormust子句,则该should子句不需要任何匹配项即可获得结果。如果它是独立的,那么它需要有 1 个匹配项。它在文档中:https ://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

所以,试一试并强制至少 1 应该匹配 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
    }
  }
}
于 2016-09-22T03:11:35.877 回答
0

看看这是否有效:

 {
        "query" : {
            "bool" : {
                "should" : [{
                        "nested" : {
                            "path" : "cities",
                            "query" : {
                                "match" : {
                                    "cities.name" : "Madrid"
                                }
                            }
                        }
                    }, {
                        "match" : {
                            "country" : "Spain"
                        }
                    }
                ],
                "filter" :
                [{
                        "term" : {
                            "continent" : "Europe"
                        }
                    }
                ]
            }
        }
    }
于 2016-09-22T00:16:24.170 回答