2016-04-03 72 views
6

結合要創建以下查詢等效 -我如何將多個布爾查詢在elasticsearch

(city = 'New York' AND state = 'NY') AND ((businessName='Java' and businessName='Shop') OR (category='Java' and category = 'Shop')) 

我使用必須嘗試布爾查詢的不同組合和應該,但似乎沒有奏效。這可以做到嗎?

+0

什麼是不工作 - 你可以詳細點嗎? – Sanj

+0

我無法想出正確的語法。總是得到一些解析異常。 – user1935449

回答

7

怎麼是這樣的:

{ 
    "query": { 
     "match_all": {} 
    }, 
    "filter": { 
     "bool": { 
      "must": [ 
       { 
        "term": { 
         "city": "New york" 
        } 
       }, 
       { 
        "term": { 
         "state": "NY" 
        } 
       }, 
       { 
        "bool": { 
         "should": [ 
          { 
           "bool": { 
            "must": [ 
             { 
              "term": { 
               "businessName": "Java" 
              } 
             }, 
             { 
              "term": { 
               "businessName": "Shop" 
              } 
             } 
            ] 
           } 
          }, 
          { 
           "bool": { 
            "must": [ 
             { 
              "term": { 
               "category": "Java" 
              } 
             }, 
             { 
              "term": { 
               "category": "Shop" 
              } 
             } 
            ] 
           } 
          } 
         ] 
        } 
       } 
      ] 
     } 
    } 
}