我正在使用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進行交互,如果它有任何區別。
只是爲了澄清:這種方法到目前爲止工作 - 我只是不確定是否是正確方式,或者任何人都可以看到我可能遇到的問題,這種方式做事。