2015-07-12 57 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" 
       } 
      }] 
     } 
    } 
}