2016-03-31 93 views
0

每个人我使用的是Es2.0版本。我想根据日期类型对范围中的LogDate字段进行排序。使用范围过滤器排序日期字段

下面是我的映射:

 PUT test/_mapping/device 
    { 
"device" : { 
    "_routing": {"required": true}, 
    "properties" : { 
     "activityId" : {"type" : "long", "store" : true }, 
     "userId":{"type":"long","store":true}, 
     "calories":{"type":"float","store":true}, 
     "description":{"type":"string","store":true}, 
     "distance":{"type":"float","store":true}, 
     "duration":{"type":"long","store":true}, 
     "startTime":{"type": "date","format" : "yyyy-MM-dd HH:mm:ss"}, 
     "endTime":{"type": "date","format" : "yyyy-MM-dd HH:mm:ss"}, 
     "steps":{"type":"long","store":true}, 
     "offset":{"type":"long","store":true}, 
     "lightSleep":{"type":"long","store":true}, 
     "deepSleep":{"type":"long","store":true}, 
     "almostAwake":{"type":"long","store":true}, 
     "s1":{"type":"long","store":true}, 
     "s2":{"type":"long","store":true}, 
     "s3":{"type":"long","store":true}, 
     "s4":{"type":"long","store":true}, 
     "s5":{"type":"long","store":true}, 
     "s6":{"type":"long","store":true}, 
     "s7":{"type":"long","store":true}, 
     "s8":{"type":"long","store":true}, 
     "year":{"type":"long","store":true}, 
     "month":{"type":"long","store":true}, 
     "activityType":{"type":"string","store":true}, 
    "logDate":{"type": "date","format" : "yyyy-MM-dd", 
    "index": "not_analyzed"}, 
    "createdTime":{"type": "date","format" : "yyyy-MM-dd HH:mm:ss"} 
    } 
    } 
    } 

和我的查询是我想约会的给定范围LOGDATE场进行排序。

GET test/device/_search 
    { 
    "size": 500, 
    "sort": [ 
    { 
     "logDate": { 
     "order": "asc" 
     } 
    } 
    ], 
    "fields": [ 
    "logDate" 
    ], 
    "query": { 
    "bool": { 
     "must": [ 
      { 
       "match": { 
        "userId": "305" 
       } 
      },{ 
       "range": { 

         "logDate": 
           { 
            "from": "2016-01-25", 
            "to":"2016-01-31" 

           } 
       } 
       } 
     ] 
     } 
     } 
     } 

它没有显示我的预期output.It给我看25-01至28-02即,它不是analyzinf一个月。 请大家帮我从昨天晚上敲我ti。非常感谢。

回答

0

根据Elasticsearch 2.0 documentationrange查询的工作与ltelt,从分别为gtgte,不。因此,改变你的范围查询应该解决您的问题:

"range": { 
    "logDate": 
     { 
      "gte": "2016-01-25", 
      "lte": "2016-01-31" 
     } 
} 

而且,你尝试过用一个简单的查询and而非bool? 从你的描述你不关心ES返回_score

Elasticsearch quer vs filter documentation指出应使用queryfilter。 您是否尝试过使用filtered查询(https://www.elastic.co/guide/en/elasticsearch/guide/master/_combining_queries_with_filters.html)?

GET /_search 
{ 
    "query": { 
     "filtered": { 
      "query": { "match": { "userId": "305" }}, 
      "filter": { 
       "range": { 
        "logDate": { 
         "gte": "2016-01-25", 
         "lte": "2016-01-31" 
        } 
       } 
      } 
     } 
    } 
} 
+0

谢谢...但我也有LTE和GTE –

+0

试了一下我的另一个期待您的查询和更新了答案 - 看起来像'filtered'查询可能是你想达到什么样的更好。由于ES没有从**和**到**作为'范围'查询/过滤器的选项**,所以您应该坚持使用'lte'和'gte'。 –

+0

已过滤的查询已被弃用https://www.elastic.co/blog/better-query-execution-coming-elasticsearch-2-0 – Rahul