2017-06-19 70 views
1

我需要一點幫助轉移MySQL查詢到ES。查詢看起來是這樣的Elasticsearch - 或在條件下

SELECT * FROM `xyz` WHERE visibility IN (1,2) AND (active=0 OR (active=1 AND finished=1) 

這很容易,使和條件,但如何使用或長期混合和?

"query" : { 
    "bool" : { 
     "must" : [{ 
      "terms" : { "visibility" : ["1", "2"] } 
     }, { 
      "term" : { "active" : "1" } 
     }, { 
      "term" : { "active" : "0", "finished" : "1" } // OR 
     },] 
    } 
    } 

回答

2

嘗試像這樣通過嵌套一個bool/shouldbool/filter查詢主要bool/filter查詢中:

{ 
    "query": { 
    "bool": { 
     "filter": [ 
     { 
      "terms": { 
      "visibility": [ 
       "1", 
       "2" 
      ] 
      } 
     }, 
     { 
      "bool": { 
      "should": [ 
       { 
       "term": { 
        "active": "0" 
       } 
       }, 
       { 
       "bool": { 
        "filter": [ 
        { 
         "term": { 
         "active": "1" 
         } 
        }, 
        { 
         "term": { 
         "finished": "1" 
         } 
        } 
        ] 
       } 
       } 
      ] 
      } 
     } 
     ] 
    } 
    } 
} 
+0

任何運氣嗎? – Val