2015-09-04 199 views
0

我一直在从自动生成的时间戳移动到映射的时间戳,并且查询不再工作。当使用自动生成的时间戳,我是能够执行这种查询:在映射的时间戳上过滤

FilterBuilder filter = FilterBuilders.rangeFilter("_timestamp").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString()); 

而且通过最新表述,如“现在-1H”会的工作。现在,我已经介绍了以下映射:

"collections": { 
    "_timestamp": { 
    "enabled": true, 
    "path": "my_date", 
    "ignore_missing": false 
    }, 
    "properties": { 
    "author": { 
     "type": "string", 
     "index": "not_analyzed" 
    }, 
    "name": { 
     "type": "string", 
     "analyzer": "lowercase" 
    }, 
    "my_date": { 
     "type": "date" 
    } 
    } 
} 

我存储my_date为Unix纪元格式,我不能再查询:

FilterBuilders.rangeFilter("_timestamp").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString()); 

返回空的结果,而查询

FilterBuilders.rangeFilter("my_date").from(zonedDateTime.toLocalDate().toString()).to(zonedDateTime.plusHours(1).toLocalDate().toString()); 

与异常

from[-1],size[-1]: Parse Failure [Failed to parse source [{"query":{"filtered":{"query":{"match_all":{}},"filter":{"range":{"my_date":{"from":"2015-09-04T19:52:15.001+01:00","to":"2015-09-04T21:52:15.001+01:00","include_lower":true,"include_upper":true}}}}}}]]]; nested: NumberFormatException[For input string: "2015-09-04T19:52:15.001+01:00"]; 
失败

它看起来像我可以做的唯一的查询是通过在my_date上使用时间戳的数字值。有什么更好的,我可以希望?

回答

2

你的问题还有一些缺失。我只能猜测你可能已经发布以来的时间,而不是毫秒的数字,因为你的映射没有正确应用,所以还有一些你没有透露的错误。以下是基于您迄今为止选择显示的数据的示例,它可以很好地工作。请尝试修改示例以符合您的情况。

curl -XDELETE localhost:9200/test 
curl -XPUT localhost:9200/test -d '{ 
    "mappings": { 
     "collections": { 
      "_timestamp": { 
       "enabled": true, 
       "path": "my_date", 
       "ignore_missing": false 
      }, 
      "properties": { 
       "my_date": { 
        "type": "date" 
       } 
      } 
      } 
     } 
    } 
}' 
curl -XGET "localhost:9200/test/_mapping?pretty" 
curl -XPOST "localhost:9200/test/collections?refresh" -d '{"my_date": "1441421651000"}' 
curl -XGET "localhost:9200/test/collections/_search?pretty" -d '{ 
    "query": { 
     "filtered": { 
      "query": { 
       "match_all": {} 
      }, 
      "filter": { 
       "range": { 
        "my_date": { 
         "from": "2015-09-04T19:52:15.001+01:00", 
         "to": "2015-09-05T21:52:15.001+01:00", 
         "include_lower": true, 
         "include_upper": true 
        } 
       } 
      } 
     } 
    } 
}' 
+0

我刚刚错误地复制了一部分映射,my_date在属性节点内正确。我已经更新了这个问题,还注意到如果我使用数字时间戳查询my_date属性,它可以正常工作 – Edmondo1984

+0

更新了答案。 – imotov

+0

您正在使用哪个版本的ES?您正在运行的查询是为我返回的ParseException – Edmondo1984