2015-07-12 58 views
0

我試圖在彈性搜索查詢中複製下面的查詢邏輯,但是某些內容不正確。SQL語句的彈性搜索DSL語法等價性

基本上下面的查詢返回一個文檔。我想要應用第一個條件:"name": "iphone"更復雜的第二個是:(username = 'gogadget' AND status_type = '1' AND created_time between 4532564 AND 64323238)。請注意,嵌套布爾必須應該會照顧更復雜的條件。如果我將"name": "iphone"的外部匹配更改爲"name": "wrong value",我仍然應該看到1個文檔。但是當我這樣做時我什麼也得不到。我不確定這是錯誤的。

下面是SQL查詢。

SELECT * from data_points 
WHERE name = 'iphone' 
    OR 
(username = 'gogadget' AND status_type = '1' AND created_time between 4532564 AND 64323238) 


{ 
    "size": 30, 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "bool": { 
         "minimum_should_match": "1", 
         "should": [ 
          { 
           "bool": { 
            "must": [ 
             { 
              "match": { 
               "username": "gogadget" 
              } 
             }, 
             { 
              "terms": { 
               "status_type": [ 
                "3", 
                "4" 
               ] 
              } 
             }, 
             { 
              "range": { 
               "created_time": { 
                "gte": 20140712, 
                "lte": 1405134711 
               } 
              } 
             } 
            ] 
           } 
          } 
         ], 
         "must": [], 
         "must_not": [] 
        } 
       }, 
       { 
        "match": { 
         "name": "iphone" 
        } 
       } 
      ] 
     } 
    } 
} 
+0

,什麼是SQL查詢返回? – eliasah

+0

sql查詢?這不存在我只是用它來解釋我想要做的彈性。 –

+0

好的。那麼預期的結果? – eliasah

回答

1

should查詢將匹配查詢並返回。

您不需要使用must來合計您的OR query

查詢想:

{ 
    "query": { 
     "bool": { 
      "should": [{ 
       "bool": { 
        "must": [{ 
         "match": { 
          "username": "gogadget" 
         } 
        }, { 
         "terms": { 
          "status_type": [ 
           "3", 
           "4" 
          ] 
         } 
        }, { 
         "range": { 
          "created_time": { 
           "gte": 20140712, 
           "lte": 1405134711 
          } 
         } 
        }] 
       } 
      }, { 
       "match": { 
        "name": "iphone" 
       } 
      }] 
     } 
    } 
}