Elasticsearch日期范围无效

时间:2017-06-20 04:37:39

标签: php elasticsearch php-7.1

我已将下面的记录编入索引

                    [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] =>
                                    )

                            )           

我想只获得给定日期之间的记录(2个日期范围和过去30天的记录之间的记录以及今天的记录等)。所以我使用Elasticsearch文档Link编写了这样的代码。但我无法获得记录。

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

在点击弹性搜索时会像这样转换

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

以下方式尝试

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

在点击弹性搜索时会像这样转换

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

请帮助我。

1 个答案:

答案 0 :(得分:1)

请您提供有关您创建的映射的更多数据。您可能已经创建了" created_at"字段为字符串,范围查询不起作用。你写的查询对我来说很好看。对于日期字段,您需要创建映射类型为' date'并格式化为' dateOptionalTime'。 请更改映射并再次尝试相同的查询。请参阅附件中的图片:enter image description here

我现在无法发表评论所以发布了这个答案。