使用范围过滤器在日期字段上排序

时间:2016-03-31 05:50:47

标签: elasticsearch

我正在使用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"

                                }
               }
                }
        ]
       }
      }
      }

它没有显示我的预期输出。它显示我25-01到28-02,即,它不是analyzinf月。 自从昨晚以来,请伙计们帮我解决问题。非常感谢。

1 个答案:

答案 0 :(得分:0)

根据Elasticsearch 2.0 documentationrange查询分别适用于lteltgtgte,而不是。因此,更改范围查询应该可以解决您的问题:

"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"
                   } 
               }
            }
        }
    }
}