2014-02-20 91 views
1

我是Elasticsearch的新手。我不認爲我完全理解查詢和過濾器的概念。在我的情況下,我只想使用過濾器,因爲我不想使用評分等高級功能。Elasticsearch來自SQL語句的DSL查詢

如何將以下SQL語句轉換爲elasticsearch查詢?

select * from tablename where (name="d" and time>1231312) or (name="ds" and time>21) 

回答

5
{ 
    "filter" : { 
     "or":[ 
      { "and" : [ 
        {"range": {"time": {"gt": 1231312}}}, 
       {"term" : {"name":"d"}} 
      ]}, 

      { "and" : [ 
        {"range": {"time": {"gt": 21}}}, 
       {"term" : {"name":"ds"}} 
      ]} 

     ] 

    } 
} 
1

以下是查詢DSL相當於您的SQL查詢。 query_string/query過濾器默認情況下沒有緩存,這就是爲什麼我使用_cache:true性能明智,它會工作良好。

curl -XPOST http://localhost:9200/index_name/_search '{ 
    "filter": { 
     "or": { 
     "filters": [ 
      { 
       "fquery": { 
        "query": { 
        "bool": { 
         "must": [ 
          { 
           "term": { 
           "name": "d" 
           } 
          }, 
          { 
           "range": { 
           "time": { 
            "gte":1231312 
           } 
           } 
          } 
         ] 
        } 
        }, 
        "_cache": true 
       } 
      }, 
      { 
       "fquery": { 
        "query": { 
        "bool": { 
         "must": [ 
          { 
           "term": { 
           "name": "ds" 
           } 
          }, 
          { 
           "range": { 
           "time": { 
            "gte":21 
           } 
           } 
          } 
         ] 
        } 
        }, 
        "_cache": true 
       } 
      } 
     ] 
     } 
    } 
}' 
+0

你有沒有試過這種查詢? – Roopendra