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],],] ];創(chuàng)建模型
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());}}操作模型
插入幾條測(cè)試數(shù)據(jù)吧
$model = new CcIndex(); $model->t_id=6; $model->setAttributes(['t_nickname' => 'user3@example.com', 't_add_time' => 0, 'company_id' => 2,], false); $model->save(false);$model = new CcIndex(); $model->t_id=7; $model->setAttributes(['t_nickname' => 'user4@example.com', 't_add_time' => 0, 'company_id' => 2,], false); $model->save(false);$model = new CcIndex(); $model->t_id=8; $model->setAttributes(['t_nickname' => 'user4@qq.com', 't_add_time' => 0, 'company_id' => 3,], false); $model->save(false);$model = new CcIndex(); $model->t_id=9; $model->setAttributes(['t_nickname' => 'user5@qq.com', 't_add_time' => 0, 'company_id' => 3,], false); $model->save(false);報(bào)錯(cuò)
Cluster autodetection did not find any active nodes.//集群自動(dòng)檢測(cè)沒有發(fā)現(xiàn)任何活動(dòng)節(jié)點(diǎn)。看來配置還不行
找到Connection.php看了一下
$autodetectCluster這個(gè)變量默認(rèn)是true,也就是說默認(rèn)自動(dòng)監(jiān)測(cè)集群,我們要改成false。
這次沒有報(bào)錯(cuò),我們查詢一下吧
$data = CcIndex::find() ->query(["match" => ["t_nickname" => "example.com"]]) ->all();結(jié)果大家可以自己打印一下,這里我就不貼了
集群配置
如果還是想默認(rèn)$autodetectCluster=true,那集群怎配置呢
假如我們有兩個(gè)服務(wù)器,內(nèi)網(wǎng)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 是集群名稱,這個(gè)不要使用默認(rèn)的,要修改,去掉注釋,如果有多個(gè)機(jī)器,加入同一個(gè)集群,那么這個(gè)值必須一樣
noide.name 是集群里面每個(gè)節(jié)點(diǎn)的值,也就是當(dāng)前機(jī)器的節(jié)點(diǎn)的值,這個(gè)值,每個(gè)節(jié)點(diǎn)要不一樣。
network host 改成當(dāng)前的內(nèi)網(wǎng)ip
discovery.zen.ping.unicast.hosts里的的ip就是其他的節(jié)點(diǎn)的ip,如果我有5臺(tái)機(jī)器,那么,這里需要把其他四臺(tái)機(jī)器的ip寫上。
同理,對(duì)于其他的節(jié)點(diǎn),需要把其他的節(jié)點(diǎn)協(xié)商,用逗號(hào)隔開
elasticSearch會(huì)找到對(duì)應(yīng)的節(jié)點(diǎn),自動(dòng)分片和做復(fù)制集。
啟動(dòng)elasticsearch報(bào)錯(cuò)
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]第一個(gè)問題
max virtual memory areas vm.max_map_count [65530] is too low, increase to at least [262144]因?yàn)楝F(xiàn)在不是root,請(qǐng)先切換到root用戶名下進(jìn)行以下操作
vi /etc/sysctl.conf添加下面配置:
vm.max_map_count=655360保存好并退出,執(zhí)行sysctl -p命令
第二個(gè)問題
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]也是要在root下操作
vi /etc/security/limits.conf添加配置
elasticsearch soft nofile 65536elasticsearch hard nofile 65536退出賬號(hào),重新登陸賬號(hào)elasticsearch
執(zhí)行ulimit -Hn查看是否生效
重新啟動(dòng)elasticsearch
其他的服務(wù)器也按照以上的操作配置即可
yii2的http_address配置也得改一下
['http_address' => '10.170.224.67:9200']集群這里我沒有測(cè)試成功,因?yàn)槲抑挥幸慌_(tái)服務(wù)器可以用,雖然啟動(dòng)成功,但yii2訪問集群的時(shí)候還是報(bào)錯(cuò)了,直接killed了,我是參考其他文章,結(jié)合自己搗鼓集群是遇到的問題寫的,有錯(cuò)誤歡迎糾正,別把別人越帶越糊涂
參考文章:
http://blog.csdn.net/xiegh201...
http://www.fancyecommerce.com...
http://blog.csdn.net/xxxxxx91...
http://blog.csdn.net/u0123714...
總結(jié)
以上是生活随笔為你收集整理的yii2-elasticsearch(3)yii2 elasticsearch 的初步尝试的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAD dxf组码 Xdata 写入和读
- 下一篇: icloud 照片同步_如何在线查看iC