yii2-elasticsearch(3)yii2 elasticsearch 的初步尝试

配置

return [
    //....
    'components' => [
        'elasticsearch' => [
            'class' => 'yii\elasticsearch\Connection',
            'nodes' => [
                ['http_address' => '127.0.0.1:9200'],
                // configure more hosts if you have a cluster
            ],
        ],
    ]
];

创建模型

namespace api\models;

class CcIndex extends \yii\elasticsearch\ActiveRecord
{
    public static function index()
    {
        return 'shiliucrm';
    }
    public static function primaryKey()
    {
        return ['t_id'];
    }
    public static function type()
    {
        return 'user';
    }
    public function attributes()
    {
        return ['t_id', 't_nickname', 't_add_time', 'compnay_id'];
    }
    public static function mapping()
    {
      return [
            static::type() => [
                "properties" => [
                    "t_id"        =>   ["type" => "long"],
                    "t_nickname" =>  ["type" => "text","index" => "analyzed",],
                    "t_add_time" =>  ["type" => "long","index" => "not_analyzed"],
                    "compnay_id" =>  ["type" => "long"],
                ]
            ]
      ];
    }
    public static function updateMapping()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->setMapping(static::index(), static::type(), static::mapping());
    }
    public static function createIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->createIndex(static::index(), [
            'settings' => [ 'index' => ['refresh_interval' => '1s'] ],
            'mappings' => static::mapping(),
            //'warmers' => [ /* ... */ ],
            //'aliases' => [ /* ... */ ],
            //'creation_date' => '...'
        ]);
    }
    public static function deleteIndex()
    {
        $db = static::getDb();
        $command = $db->createCommand();
        $command->deleteIndex(static::index(), static::type());
    }

}

操作模型

插入几条测试数据吧

$model = new CcIndex();
$model->t_id=6;
$model->setAttributes(['t_nickname' => '[email protected]', 't_add_time' => 0, 'company_id' => 2,], false);
$model->save(false);

$model = new CcIndex();
$model->t_id=7;
$model->setAttributes(['t_nickname' => '[email protected]', 't_add_time' => 0, 'company_id' => 2,], false);
$model->save(false);

$model = new CcIndex();
$model->t_id=8;
$model->setAttributes(['t_nickname' => '[email protected]', 't_add_time' => 0, 'company_id' => 3,], false);
$model->save(false);

$model = new CcIndex();
$model->t_id=9;
$model->setAttributes(['t_nickname' => '[email protected]', 't_add_time' => 0, 'company_id' => 3,], false);
$model->save(false);
      

报错

 Cluster autodetection did not find any active nodes.
 //集群自动检测没有发现任何活动节点。 
 

看来配置还不行
找到Connection.php看了一下
$autodetectCluster这个变量默认是true,也就是说默认自动监测集群,我们要改成false。

 'elasticsearch' => [
        'class' => 'yii\elasticsearch\Connection',
        'autodetectCluster' => false,
        'nodes' => [
            ['http_address' => '127.0.0.1:9200'],
            //或者填['http_address' => 'inet[/127.0.0.1:9200]'],
            // configure more hosts if you have a cluster
        ],
    ],

这次没有报错,我们查询一下吧

$data = CcIndex::find()
->query(["match" => ["t_nickname" => "example.com"]])
->all();

结果大家可以自己打印一下,这里我就不贴了

集群配置

如果还是想默认$autodetectCluster=true,那集群怎配置呢
假如我们有两个服务器,内网ip分别为10.170.224.67,10.170.224.68

以10.170.224.67举例配置

 vim /usr/local/elasticsearch/config/elasticsearch.yml

加上

cluster.name: young-application  
node.name: node-2 
network.host: 10.170.224.67
discovery.zen.ping.unicast.hosts: ["10.170.224.68"]

其中cluster.name 是集群名称,这个不要使用默认的,要修改,去掉注释,如果有多个机器,加入同一个集群,那么这个值必须一样

noide.name 是集群里面每个节点的值,也就是当前机器的节点的值,这个值,每个节点要不一样。

network host 改成当前的内网ip

discovery.zen.ping.unicast.hosts里的的ip就是其他的节点的ip,如果我有5台机器,那么,这里需要把其他四台机器的ip写上。

同理,对于其他的节点,需要把其他的节点协商,用逗号隔开

elasticSearch会找到对应的节点,自动分片和做复制集。

启动elasticsearch报错

ERROR: bootstrap checks failed
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

第一个问题

max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]

因为现在不是root,请先切换到root用户名下进行以下操作

vi /etc/sysctl.conf 

添加下面配置:

vm.max_map_count=655360

保存好并退出,执行sysctl -p命令

第二个问题

 max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]

也是要在root下操作

 vi /etc/security/limits.conf 

添加配置

 elasticsearch soft nofile 65536
 elasticsearch hard nofile 65536
 

退出账号,重新登陆账号elasticsearch
执行ulimit -Hn查看是否生效

重新启动elasticsearch

其他的服务器也按照以上的操作配置即可

yii2的http_address配置也得改一下

 ['http_address' => '10.170.224.67:9200']

集群这里我没有测试成功,因为我只有一台服务器可以用,虽然启动成功,但yii2访问集群的时候还是报错了,直接killed了,我是参考其他文章,结合自己捣鼓集群是遇到的问题写的,有错误欢迎纠正,别把别人越带越糊涂

参考文章:
http://blog.csdn.net/xiegh201...
http://www.fancyecommerce.com...
http://blog.csdn.net/xxxxxx91...
http://blog.csdn.net/u0123714...

你可能感兴趣的:(全文检索,yii2,elasticsearch)