ElasticSearch引擎适配器

这个扩展是一个类ActiveRecord的包装,将ElasticSearch全文搜索引擎集成到Yii2框架中。它允许你使用任何模型数据,并使用ActiveRecord模式在ElasticSearch数据集中获取和存储数据。

准备

  1. 按照官方指南http://www.yiiframework.com/doc-2.0/guide-start-installation.html的描述,使用Composer包管理器创建一个新的应用。
  2. https://www.elastic.co/downloads/elasticsearch安装ElasticSearch服务。
  3. 使用如下命令安装扩展:
  1. composer require yiisoft/yii2-elasticsearch

如何做…

在你的应用配置中设置新的ElasticSearch连接。

  1. return [
  2. //....
  3. 'components' => [
  4. 'elasticsearch' => [
  5. 'class' => 'yii\elasticsearch\Connection',
  6. 'nodes' => [
  7. ['http_address' => '127.0.0.1:9200'],
  8. // configure more hosts if you have a cluster
  9. ],
  10. ],
  11. ]
  12. ];

使用查询类

你可以使用Query类,用于在任何数据集中进行低级查询:

  1. use \yii\elasticsearch\Query;
  2. $query = new Query;
  3. $query->fields('id, name')
  4. ->from('myindex', 'users')
  5. ->limit(10);
  6. $query->search();

你也可以创建一个命令,直接运行:

  1. $command = $query->createCommand();
  2. $rows = $command->search();

使用ActiveRecord

使用ActiveRecord是一种常用的方法来访问你的数据。只需要扩展yii\elasticsearch\ActiveRecord类,并继承attributes()方法,来定义你文档的属性。

例如,你可以写Customer模型:

  1. class Buyer extends \yii\elasticsearch\ActiveRecord
  2. {
  3. public function attributes()
  4. {
  5. return ['id', 'name', 'address', 'registration_date'];
  6. }
  7. public function getOrders()
  8. {
  9. return $this->hasMany(Order::className(), ['buyer_id' => 'id'])->orderBy('id');
  10. }
  11. }

然后写Order模型:

  1. class Order extends \yii\elasticsearch\ActiveRecord
  2. {
  3. public function attributes()
  4. {
  5. return ['id', 'user_id', 'date'];
  6. }
  7. public function getBuyer()
  8. {
  9. return $this->hasOne(Customer::className(), ['id' => 'buyer_id']);
  10. }
  11. }

你可以复写index()type()来定义这个记录的index和type。

下边是一个使用例子:

  1. $buyer = new Buyer();
  2. $buyer>primaryKey = 1; // it equivalent to $customer->id = 1;
  3. $buyer>name = 'test';
  4. $buyer>save();
  5. $buyer = Buyer::get(1);
  6. $buyer = Buyer::mget([1,2,3]);
  7. $buyer = Buyer::find()->where(['name' => 'test'])->one();

你可以使用Query DSL做指定查询:

  1. $result = Article::find()->query(["match" => ["title" => "yii"]])->all();
  2. $query = Article::find()->query([
  3. "fuzzy_like_this" => [
  4. "fields" => ["title", "description"],
  5. "like_text" => "Some search text",
  6. "max_query_terms" => 12
  7. ]
  8. ]);
  9. $query->all();

你可以添加facets到你的搜索中:

  1. $query->addStatisticalFacet('click_stats', ['field' => 'visit_count']);
  2. $query->search();

使用ElasticSearch调试板

这个扩展包含了一个特殊的面板,用于yii2-debug模块。它允许你查看所有执行的查询。你可以在你的配置文件中包含这个面板:

  1. if (YII_ENV_DEV) {
  2. // configuration adjustments for 'dev' environment
  3. $config['bootstrap'][] = 'debug';
  4. $config['modules']['debug'] = [
  5. 'class' => 'yii\debug\Module',
  6. 'panels' => [
  7. 'elasticsearch' => [
  8. 'class' => 'yii\elasticsearch\DebugPanel',
  9. ],
  10. ],
  11. ];
  12. $config['bootstrap'][] = 'gii';
  13. $config['modules']['gii'] = 'yii\gii\Module';
  14. }

工作原理…

这个扩展提供了一个低级命令构建器,以及高级ActiveRecord实现,用于从ElasticSearch中查询记录。

这个扩展的ActiveRecord的用法和数据库的ActiveRecord非常类似,后者可以参考第三章,ActiveRecord,模型和数据库,此外还有join()groupBy()having()union()ActiveQuery操作。

注意ElasticSearch默认限制返回的记录数量,如果你使用via()选项来使用关系,注意这个限制。

参考