日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發布時間:2023/12/14 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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。

'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 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 的初步尝试的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。