【发布时间】:2017-03-17 14:15:00
【问题描述】:
我正在使用 ElasticSearch 作为网站的搜索组件。被索引并最终搜索的数据与保存在 MySQL DB 中的数据相同。
我的方法是在相应的 CRUD MySQL 操作发生时添加/删除/修改索引中的数据。
例如,创建操作如下所示:
public function savePost(Request $request) {
//Firstly, create the object and save it to MySQL
$post = new Post();
$post->title = $request->title;
$post->body = $request->body;
//...
//and so on
$post->save();
//Secondly, index this new data:
$elasticSearchClient = ClientBuilder::create()->build();
$params = [
'index' => 'some_index_elasticsearch',
'id' => $post->id,
'type' => 'post',
'timestamp' => time(),
'body' => [
'id' => $post->id,
'title' => $post->title,
'body' => $post->body,
//... and so on
],
];
$elasticSearchClient->index($params);
}
如果数据在 MySQL 中被删除/更新,我只需删除它或从索引中更新它。
这是将 MySQL 与 ElasticSearch(或任何其他类似技术,如 Sphinx)一起使用的正确方法吗?或者您会推荐一种更好的方法来使用 MySQL 作为 ElasticSearch 的数据源吗? (这实际上根本没有发生,因为 ElasticSearch 和 MySQL 之间根本没有交互)。
如果有任何不同,我正在使用 https://github.com/elastic/elasticsearch-php 与 ElasticSearch 进行交互。
澄清一下:到目前为止,这种方法确实有效 - 我只是不确定这是否是 正确 方式,或者是否有人能看到我在这种方式下可能遇到的问题东西。
【问题讨论】:
标签: php mysql elasticsearch