简体   繁体   English

Elasticsearch日期范围不起作用

[英]Elasticsearch Date Range not working

I have indexed records like below 我有如下索引的记录

                    [0] => Array
                            (
                                [_index] => test_index
                                [_type] => cool_type
                                [_id] => AVy-s52Kahn1F1gX3B0K
                                [_score] => 1
                                [_source] => Array
                                    (
                                        [name] => TEST
                                        [name_alias] => 
                                        [webiste] => www.test.com
                                        [directions] => 
                                        [narrative] => 
                                        [office_function] => 
                                        [deleted_by] => 
                                        [created_at] => 2017-06-09 11:03:27
                                        [updated_at] => 2017-06-09 11:03:27
                                        [deleted_at] => 
                                    )

                            )
                [1] => Array
                            (
                                [_index] => test_index
                                [_type] => cool_type
                                [_id] => AVy-s52Kahn1F1gX3B0L
                                [_score] => 1
                                [_source] => Array
                                    (
                                        [name] => new
                                        [name_alias] => 
                                        [webiste] => www.new.com
                                        [directions] => 
                                        [narrative] => 
                                        [office_function] => 
                                        [deleted_by] => 
                                        [created_at] => 2017-06-18 10:00:00
                                        [updated_at] => 2017-06-18 10:00:00
                                        [deleted_at] =>
                                    )

                            )           

I want to get only records between the given days (between 2 date ranges and records for last 30 days and records of today etc). 我只想获取给定日期之间的记录(介于2个日期范围和最近30天的记录以及今天的记录等)。 So I have written code like this using Elasticsearch documentation Link . 所以我已经使用Elasticsearch文档Link编写了这样的代码。 But I am unable to get the records. 但是我无法获取记录。

$params = [
    'query' => [
        'match_all' => []
    ],
    'filter' => [
        'range' => [
            'created_at' => [
                'gte' => '2017-06-18 00:00:00',
                'lte' =>  '2017-06-19 00:00:00'
            ]
        ]
    ],
    'size' => 10
];

Which converts like this while hitting to elasticsearch 在转换为elasticsearch时会像这样转换

{
    "query": {
        "match_all": []
    },
    "filter": {
        "range": {
            "created_at": {
                "gte": "2017-06-18 00:00:00",
                "lte": "2017-06-19 00:00:00"
            }
        }
    },
    "size": 10
}

Tried below way also 也尝试以下方式

$params = [
    'query' => [
        'range' => [
            'created_at' => [
                'gte' => '2017-06-18 00:00:00',
                'lte' =>  '2017-06-19 00:00:00'
            ]
        ]
    ],
    'size' => 10
];

Which converts like this while hitting to elasticsearch 在转换为elasticsearch时会像这样转换

{
    "query": {
        "range": {
            "created_at": {
                "gte": "2017-06-18 00:00:00",
                "lte": "2017-06-19 00:00:00"
            }
        }
    },
    "size": 10
}

Kindly help me on this. 请帮助我。

Will you please give us more data on the mapping that you have created. 您能否为我们提供有关您创建的映射的更多数据。 You might have created the "created_at" field as string, where the range query won't work. 您可能已将“ created_at”字段创建为字符串,其中范围查询将不起作用。 The query you have written is looking fine to me. 您写的查询对我来说很好。 For date field you need to create mapping type as 'date' and format as 'dateOptionalTime'. 对于日期字段,您需要创建映射类型为“日期”,格式为“ dateOptionalTime”。 Please change the mapping and try again the same query.Please refer to the image attached: 请更改映射,然后重试相同的查询。请参考所附图片: 在此处输入图片说明

I cannot made the comment right now so posted this answer. 我现在无法发表评论,所以发布了此答案。

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

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