简体   繁体   中英

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). So I have written code like this using Elasticsearch documentation 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

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

{
    "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. 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'. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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