2017-04-14 25 views
0

我需要實現以下邏輯elasic搜索彈性搜索結合兩個必須用OR

(ticketype=1 AND customerid= id AND available >1) OR(tickettype = 2 AND available >1) 

我有下面的查詢與我

"query": { 
          "bool": { 
          "minimum_should_match": 1, 
          "should": [ 
           { 
           "bool": { 
            "must": [ 
             { 
             "range": { 
              "ticket_group.available": { 
               "gte": 1 
              } 
             } 
             }, 
             { 
             "match": { 
              "ticket_group.customer_id": (q.customer_id) ? q.customer_id : "" 
             } 
             }, 
             { 
             "match": { 
              "ticket_group.ticket_type": 1 
             } 
             } 
            ] 
           } 
          } 
          , 
           { 
           "bool":{ 
             "must":[ 
              { 
               "range":{ 
               "ticket_group.available":{ 
                "gte":1 
               } 
               } 
              }, 
              { 
               "match":{ 
               "ticket_group.ticket_type":2 
               } 
              } 
             ] 
            } 
           } 

          ] 
          } 

         } 

但它似乎正採取一切條件和即使在使用之後。什麼應該是正確的查詢?

+0

「似乎」 是很模糊。你能展示一個你在結果中找到的文件的例子,但它不應該匹配嗎? – Val

+0

您可以使用查詢字符串語法進行此類查詢。 –

回答

0

我已經使用嵌套查詢,沒有它搜索嵌套屬性不能正常工作。我簡單查詢到 AND((ticketype = 1 AND客戶id = ID)或(tickettype = 2))和可用> 1

嘗試以下查詢:

{ 
"query": { 
    "nested": { 
    "path": "ticket_group", 
    "query": { 
     "bool": { 
      "must": [ 
       { 
       "range": { 
        "ticket_group.available": { 
         "gte": 1 
        } 
       } 
       }, 
       { 
       "bool": { 
        "minimum_should_match": 1, 
        "should": [ 
         { 
          "match": { 
          "ticket_group.ticket_type": 2 
          } 
         }, 
         { 
          "match": { 
          "ticket_group.customer_id": 1 
          } 
         } 
        ] 
       } 
       } 
      ] 
     } 
    } 
    } 
    } 
}