2016-09-21 69 views
0

我使用Elasticsearch 2.4,我試圖讓一個行爲像下面的SQL語句的查詢:如何在過濾查詢使用或與Elasticsearch 2.X

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版的「過濾」 has been deprecated帕拉姆。我試圖使用新的方法使用過濾器來構建查詢,但是它沒有正確找到嵌套值。這是結果查詢:

{ 
    "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" 
        } 
        ] 
    } 
} 

是否有人知道正確的方式來實現這一目標?

回答

0

見,如果這個工程:

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

不,它沒有。但無論如何,使用「必須」它不會有**或**的行爲。我需要它匹配,如果它是某個國家或它是某個城市。 – Ander

+0

你可以嘗試用一個應該替換的「必須」嗎?更新了答案。 – jay

0

其實我得到它的工作是這樣的:

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

我知道..奇怪,但如果查詢不匹配任何長期使用主查詢中的「應該」而不是「必須」將從Europe返回大量隨機結果。

0

如果您的bool查詢有filtermust子句,那麼should子句不需要任何匹配即可獲得結果。如果它是獨立的,那麼它需要它有1匹配。它的文檔在:https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

所以,給這個嘗試和力量至少1應該使用"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 
    } 
    } 
} 
相關問題