想把自己的博客用上 ElasticSearch 搜索,用的是 Yii2-elasticsearch 扩展
搞了半天不会用,没搞明白 elasticsearch 是个怎么回事,是要把 Mysql 中的数据弄到 ES 中去吗?
难道每次都要到msql中去把数据找出来,再一个一个赋值给 ES 吗?
$posts = Post::find()->all();
$postElastic = new PostElastic();
$postElastic->attributes = ['name' => $postElastic[0]->name];
要像上面这样嘛?那也太蛋疼了吧
但是直接 Contents::find()->all()
查出来的也没有数据,
class Contents extends \yii\elasticsearch\ActiveRecord
{
public function attributes()
{
return ['id', 'title', 'slug', 'created_at', 'text', 'type', 'status'];
}
}
ringa_lee2017-04-10 16:48:58
就我目前接触的部分技术栈来看,一般的解决方案是使用类似kafka这样的消息队列来做mysql和es的数据同步,所有的mysql更改都会以binlog的形式存在,将binlog的增量数据push到kafka消息队列里,然后es那边有对应的消费脚本来消费kafka里的binlog数据,这样就能基本实现很低延迟的数据同步
至于yii2这个扩展怎么使用,也没专门研究过,首先自己去es里查询一下有没有数据进去,如果有数据在但是查不出来的话,再来排查使用方法,最好去看官方文档