我正在使用ElasticSearch作为站点的搜索组件。被索引并最终搜索的数据与MySQL DB中保存的数据相同。使用MySQL的ElasticSearch用法
我的做法是在相应的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进行交互,如果它有任何区别。
只是为了澄清:这种方法到目前为止工作 - 我只是不确定是否是正确方式,或者任何人都可以看到我可能遇到的问题,这种方式做事。