我正在使用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之间根本就没有互动).
只是为了澄清:这种方法到目前为止确实有效 – 我只是不确定它是否是正确的方法,或者是否有人能够看到我可能遇到的这种做法遇到的问题.