上周开始使用elasticsearch,我们正在沙箱上实现它,我目前很难从php进行索引和映射,因为文档似乎跳过了大多数复杂的例子。我目前很难为我的记录索引日期以使用它对搜索进行排序,我尝试将字段解析为字符串,在这种情况下,弹性只是将字段映射/索引为文本而不是日期,我也尝试使用 strtotime 解析它,在这种情况下,映射显示它被映射为 long 而不是 date 。我从 MySQL 获取日期为 yyyy-MM-dd HH:mm:ss 字符串:
$mapping['index'] = $index_name; //mapping code
$mapping['body'] = array(
'mappings' => array(
'post' => array(
'_source' => array(
'enabled' => true
),
'properties' => array(
'title' => array(
'type' => 'text',
'analyzer' => 'standard'
),
'content' => array(
'type' => 'text',
'analyzer' => 'standard'
),
'site_terms' => array(
'type' => 'keyword'
),
'post_date' => array(
'format' => 'strict_date_optional_time||epoch_millis',
'type' => 'date'
)
)
)
)
);
$response = $client->indices()->create($mapping);
我使用批量导入循环:
$params['body'][] = [
'body' =>[
'title' => $post_title,
'content' => $post_content,
'site_terms' => str_replace('-','',$results[$i-1]->site_terms),
'post_date' => strtotime($results[$i-1]->post_date_gmt)
]
];
正如我在这种情况下所说的那样,它只是被映射了那么长时间,也尝试在没有 strtotime 的情况下解析日期,因为它是从 db 查询返回的,并且被映射为文本。如果有人能解释我做错了什么,我将不胜感激。
根据要求,这是一个映射示例:
Index has 100 Items!Array
(
[index] => Array
(
[mappings] => Array
(
[post] => Array
(
[properties] => Array
(
[body] => Array
(
[properties] => Array
(
[content] => Array
(
[type] => text
[fields] => Array
(
[keyword] => Array
(
[type] => keyword
[ignore_above] => 256
)
)
)
[post_date] => Array
(
[type] => text
[fields] => Array
(
[keyword] => Array
(
[type] => keyword
[ignore_above] => 256
)
)
)
[site_terms] => Array
(
[type] => text
[fields] => Array
(
[keyword] => Array
(
[type] => keyword
[ignore_above] => 256
)
)
)
[title] => Array
(
[type] => text
[fields] => Array
(
[keyword] => Array
(
[type] => keyword
[ignore_above] => 256
)
)
)
)
)
[content] => Array
(
[type] => text
[analyzer] => standard
)
[post_date] => Array
(
[type] => date
)
[site_terms] => Array
(
[type] => keyword
)
[title] => Array
(
[type] => text
[analyzer] => standard
)
)
)
)
)
)
这是将 sql 返回的日期时间解析为字符串时,当我尝试将其解析为 unix 时间戳时,我得到了:
[post_date] => Array
(
[type] => long
)