弹性搜索结合两个必须与OR

时间:2017-04-14 11:25:29

标签: elasticsearch

我需要在elasic搜索中实现以下逻辑

(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
                                              }
                                           }
                                        ]
                                     }
                              }

                           ]
                          }

                       }

但似乎即使在使用应用之后,它仍然在使用所有条件。什么应该是正确的查询?

1 个答案:

答案 0 :(得分:0)

我使用了嵌套查询,没有它搜索嵌套属性不能正常工作。我已将您的查询简化为 AND((ticketype = 1 AND customerid = id)OR(tickettype = 2))AND available> 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
                          }
                       }
                    ]
                 }
              }
           ]
        }
     }
   }
  }
 }