2013-12-16 69 views
9

多個範圍elasticsearch以下range_query返回預期的結果:查詢與多個日期

{"query": { 
    "bool": { 
     "must": [ 
     { 
      "range": { 
      "created_at": { 
       "gte": "2013-12-09" 
      } 
      } 
     } 
     ] 
    } 
    } 
} 

但是和荷蘭國際集團共同幾個範圍查詢,沒有返回值:

{"query": { 
    "bool":{ 
     "must": [ 
     { 
      "and": [ 
      { 
       "range": { 
       "created_at": { 
        "gte": "2013-12-09" 
       } 
       } 
      }, 
      { 
       "range": { 
       "happens_on": { 
        "lte": "2013-12-16" 
       } 
       } 
      }, 
      { 
       "range": { 
       "created_at": { 
        "lte": "2013-12-14" 
       } 
       } 
      } 
      ] 
     } 
     ] 
    } 
    } 
} 

什麼在多個字段上使用多個range_queries的正確方法?

編輯:啊,好的,所以這是我使用range_filter而不是range_query?這聽起來很有希望,所以我只用一個範圍過濾器重新編寫了我的查詢。在這裏發佈所有內容,以防萬一我把查詢搞亂了。我執行GET和源鍵內的一切實際上是JSON的,但我刪除了逃跑的連字符的可讀性:

{ 
    "source": { 
    "filtered": { 
     "filter": { 
     "and": [ 
      { 
      "term": { 
       "restricted": false 
      } 
      }, 
      { 
      "not": { 
       "term": { 
       "deleted": true 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-16" 
       } 
      } 
      } 
     ] 
     }, 
     "query": { 
     "bool": { 
      "must": [ 
      ] 
     } 
     } 
    }, 
    "from": 0, 
    "size": 10 
    } 
} 

遺憾的是,我的問題還是一樣:我沒有收到任何命中。

EDIT2:因此,在像Njal這樣的必要條款內部的範圍內走的時候,我們建議。這給了我一個這樣的多範圍查詢:

{ 
    "source": { 
    "filter": { 
     "and": [ 
     { 
      "term": { 
      "restricted": false 
      } 
     }, 
     { 
      "not": { 
      "term": { 
       "deleted": true 
      } 
      } 
     } 
     ] 
    }, 
    "from": 0, 
    "query": { 
     "bool": { 
     "must": [ 
      { 
      "range": { 
       "happens_on": { 
       "gte": "2013-12-06" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "gte": "2013-12-15" 
       } 
      } 
      }, 
      { 
      "range": { 
       "happens_on": { 
       "lte": "2013-12-17" 
       } 
      } 
      }, 
      { 
      "range": { 
       "created_at": { 
       "lte": "2013-12-17" 
       } 
      } 
      } 
     ] 
     } 
    }, 
    "size": 10 
    } 
} 

仍然沒有結果返回。我在這裏做了什麼明顯的錯誤?

回答

12

根據您的bool查詢的must子句,無需將其包裝在and中。沒有and查詢,也許你正在考慮and filter

Example runnable play爲方便起見,捲曲的命令:

#!/bin/bash 

export ELASTICSEARCH_ENDPOINT="http://localhost:9200" 

# Create indexes 

curl -XPUT "$ELASTICSEARCH_ENDPOINT/play" -d '{ 
    "settings": {}, 
    "mappings": { 
     "type": { 
      "properties": { 
       "created_at": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       }, 
       "name": { 
        "type": "string" 
       }, 
       "happens_on": { 
        "type": "date", 
        "format": "dateOptionalTime" 
       } 
      } 
     } 
    } 
}' 


# Index documents 
curl -XPOST "$ELASTICSEARCH_ENDPOINT/_bulk?refresh=true" -d ' 
{"index":{"_index":"play","_type":"type"}} 
{"name":"foo","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-08T00:00:00.000Z","happens_on":"2013-12-16T00:00:00.000Z"} 
{"index":{"_index":"play","_type":"type"}} 
{"name":"bar","created_at":"2013-12-09T00:00:00.000Z","happens_on":"2013-12-17T00:00:00.000Z"} 
' 

# Do searches 

curl -XPOST "$ELASTICSEARCH_ENDPOINT/_search?pretty" -d ' 
{ 
    "query": { 
     "bool": { 
      "must": [ 
       { 
        "range": { 
         "created_at": { 
          "gte": "2013-12-09T00:00:00.000Z" 
         } 
        } 
       }, 
       { 
        "range": { 
         "happens_on": { 
          "lte": "2013-12-16T00:00:00.000Z" 
         } 
        } 
       } 
      ] 
     } 
    } 
} 
' 
+0

感謝您的回答和對不起,我對這個混亂。我會嘗試將我的範圍嵌套在must子句中。 – thomax

+0

好吧,我試過你的建議,看看我的EDIT2的問題。仍然沒有結果。這可能與日期格式有關嗎? – thomax

+1

Riiiight。所以原來我Njals的建議看起來不適合我的原因是我的測試搞砸了。現在測試是固定的和解決方案的岩石謝謝! – thomax