简体   繁体   English

Elasticsearch过滤日期格式为日期的文档

[英]Elasticsearch filter documents with date in epoch format

My documents in ES contain a field that stores date value in epoch format like this: 我在ES中的文档包含一个以epoch格式存储日期值的字段,如下所示:

"received": 1521055535062

I am trying to build a query where I can filter documents based on a certain date with or without UTC time difference taken into account. 我正在尝试建立一个查询,在其中我可以基于某个日期过滤文档,而无需考虑UTC时差。

Since I have over a million records in ES , I would like a way to figure out how many of those documents belonged to a specific date. 由于我在ES有超过一百万条记录,因此,我想一种方法来找出特定日期中有多少文件。

What you are looking for is the date range query with date format specification that you want. 您要查找的是具有所需日期格式规范的日期范围查询。 The following should return the docs received on first day of 2018 以下应返回2018年第一天收到的文档

GET _search
{
 "query": {
    "range" : {
        "received" : {
            "gte": "01/01/2018",
            "lt": "01/02/2018",
            "format": "dd/MM/yyyy"
        }
    }
 }
}

See here for the details https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-range-query.html 有关详细信息,请参见此处https://www.elastic.co/guide/zh-CN/elasticsearch/reference/current/query-dsl-range-query.html

Just adding a snippet of Java code (using the range query). 只需添加一段Java代码(使用范围查询)即可。 From the question, it's not clear in which language the query should be written but if it's in Java, following snippet should be helpful: 从问题来看,尚不清楚应使用哪种语言编写查询,但是如果使用Java语言编写,则以下代码段可能会有所帮助:

        BoolQueryBuilder boolQuery = new BoolQueryBuilder();
        Date asOfDate = new Date(System.currentTimeMillis() - historySeconds * 1000);
        boolQuery.must(QueryBuilders.rangeQuery("created").from(asOfDate).includeLower(true));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM