Elasticsearch 日期范围不起作用
Elasticsearch Date Range not working
我已将如下记录编入索引
[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
];
点击 elasticsearch 时会像这样转换
{
"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
];
点击 elasticsearch 时会像这样转换
{
"query": {
"range": {
"created_at": {
"gte": "2017-06-18 00:00:00",
"lte": "2017-06-19 00:00:00"
}
}
},
"size": 10
}
请帮我解决这个问题。
能否请您向我们提供有关您创建的映射的更多数据。您可能已将 "created_at" 字段创建为字符串,其中范围查询将不起作用。你写的查询对我来说很好。对于日期字段,您需要将映射类型创建为 'date' 并将格式设置为 'dateOptionalTime'。
请更改映射并重试相同 query.Please 参考附图:
我现在无法发表评论,所以发布了这个答案。
我已将如下记录编入索引
[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
];
点击 elasticsearch 时会像这样转换
{
"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
];
点击 elasticsearch 时会像这样转换
{
"query": {
"range": {
"created_at": {
"gte": "2017-06-18 00:00:00",
"lte": "2017-06-19 00:00:00"
}
}
},
"size": 10
}
请帮我解决这个问题。
能否请您向我们提供有关您创建的映射的更多数据。您可能已将 "created_at" 字段创建为字符串,其中范围查询将不起作用。你写的查询对我来说很好。对于日期字段,您需要将映射类型创建为 'date' 并将格式设置为 'dateOptionalTime'。
请更改映射并重试相同 query.Please 参考附图:
我现在无法发表评论,所以发布了这个答案。