configuration

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

Create the model

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()); }}

Operating model

Insert some test data

$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);
      

An error

Cluster autodetection did not find any active nodes. // Cluster autodetection did not find any active nodes.

The $autodetectCluster variable is true by default, which means it automatically monitors the cluster by default. We will change it to false.

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

There is no error this time. Let’s check

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

You can print it out, but I won’t post it here

The cluster configuration

$autodetectCluster=true $autodetectCluster=true $autodetectCluster=true $autodetectCluster=true $autodetectCluster=true $autodetectCluster=true

Take 10.170.224.67 for example

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

add

Cluster. Name: young - application node. The name: node - 2 network. Host: 10.170.224.67 discovery. Zen. Ping. Unicast. Hosts: [" 10.170.224.68 "]

The cluster.name is the name of the cluster. This is not the default, but should be changed to remove the comment

Noide. name is the value of each node in the cluster, which is the value of the node of the current machine. This value is different for each node.

Change network host to current Intranet IP

Discovery. Zen. Ping. Unicast. IP is the other nodes in the hosts IP, if I have five machines, so, here need to put the other four machine IP to write.

Similarly, for other nodes, the other nodes need to be negotiated, separated by commas

Elasticsearch will find the corresponding node, automatically sharding and make copy sets.

Start ElasticSearch to report an error

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]

First question

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

Because you are not root, please switch to root and do the following

vi /etc/sysctl.conf 

Add the following configuration:

vm.max_map_count=655360

Save it and exit, execute the sysctl -p command

Second question

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

Also operate under root

 vi /etc/security/limits.conf 

Add the configuration

 elasticsearch soft nofile 65536
 elasticsearch hard nofile 65536
 

Go back to elasticsearch and go to ulimit -hn to see if it’s working

Restart ElasticSearch

Other servers can also be configured according to the above operations

The http_address configuration for yii2 also needs to be changed

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

I didn’t test the cluster successfully, because I only have one server to use. Although I started the cluster successfully, there was an error when I accessed the cluster, which was directly killed. I wrote it by referring to other articles, combined with the problems I encountered when I started the cluster

Refer to the article: http://blog.csdn.net/xiegh201… http://www.fancyecommerce.com… http://blog.csdn.net/xxxxxx91… http://blog.csdn.net/u0123714…