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' => '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);報錯
Cluster autodetection did not find any active nodes.//集群自動檢測沒有發現任何活動節點。看來配置還不行
找到Connection.php看了一下
$autodetectCluster這個變量默認是true,也就是說默認自動監測集群,我們要改成false。
這次沒有報錯,我們查詢一下吧
$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 65536elasticsearch 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(3)yii2 elasticsearch 的初步尝试的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CAD dxf组码 Xdata 写入和读
- 下一篇: icloud 照片同步_如何在线查看iC