1、基于6.41安装以下es。在composer.json文件中引入elasticsearch-php:{"require":{"elasticsearch/elasticsearch":"~6.0","monolog/monolog":"~1.0"}}2.用composer安装客户端:curl-shttp://getcomposer.org/installer|phpphpcomposer.pharinstall--no-dev2.快速入门1.创建一个test.php文件,内容如下setHosts($hosts)->build();//实例化一个新的ClientBuilder//设置hosts$params=['index'=>'test_data','type'=>'users','id'=>100027,'client'=>['ignore'=>404]];var_dump($client->get($params));2、浏览器访问test.php,结果如下(前提你的es已经有数据了)3.基本操作1.创建索引$params=['index'=>'test_index'];//创建索引print_r($client->indices()->create($params));2.创建索引(指定模板)$params=['index'=>'test_index','body'=>['settings'=>['number_of_shards'=>5,'number_of_replicas'=>2],'mappings'=>['test_type'=>['_source'=>['enabled'=>true],'properties'=>['name'=>['type'=>'text','analyzer'=>'ik_max_word'],'age'=>['type'=>'integer']]]]]];//使用映射和设置创建索引nowprint_r($client->indices()->create($params));3、删除索引,$params=['index'=>'test_index'];print_r($client->indices()->delete($params));4.更改索引的配置参数:$params=['index'=>'test_index','body'=>['settings'=>['number_of_replicas'=>0,'refresh_interval'=>-1]]];print_r($client->indices()->putSettings($params));5.获取一个或多个索引的当前配置参数$params=['index'=>['test_index','test_data']];print_r($client->indices()->getSettings($params));6.更改或添加索引映射$params=['index'=>'test_index','type'=>'test_type','body'=>['test_type'=>['_source'=>['enabled'=>true],'properties'=>['name'=>['type'=>'text','analyzer'=>'ik_max_word'],'age'=>['type'=>'integer'],'createtime'=>['类型'=>'date'//添加时间]]]]];//更新索引映射print_r($client->indices()->putMapping($params));7.返回索引和类型的映射细节$response=$client->indices()->getMapping();//获取'my_index'中所有类型的映射$params=['index'=>'my_index'];$response=$client->indices()->getMapping($params);//获取所有类型'my_type'的映射,不管索引如何$params=['type'=>'my_type'];$response=$client->indices()->getMapping($params);//获取映射'my_type'in'my_index'$params=['index'=>'my_index''type'=>'my_type'];$response=$client->indices()->getMapping($params);//获取两个索引的映射$params=['index'=>['my_index','my_index2']];$response=$client->indices()->getMapping($params);8.索引一个文档(如果提供id,则更新id对应的记录,如果不提供,则生成一个文档)$params=['index'=>'test_data','type'=>'users','id'=>'100027','body'=>['nickname'=>'update222']];//文档将被索引到my_index/my_type/my_idprint_r($client->index($params));9.获取文档$params=['index'=>'test_data','type'=>'users','id'=>'100027'];//在/my_index/my_type/my_idprint_r($client->get($params));10、更新文档(doc指定要更新的字段内容)`$params=['index'=>'test\_data','type'=>'users','id'=>'100027','body'=>\['doc'=>\['昵称'=>'abc','mobile'=>'13800138000'\]\]]??;//更新文档在/my_index/my_type/my_idprint_r($client->update($params));`11.执行脚本更新、拼接或递增某个字段的数据$params=["index"=>"test_data","type"=>"users","id"=>"100027","body"=>["script"=>"ctx._source.nickname+='hahh'"]];print_r($client->update($params));12.删除文档$params=['index'=>'test_data','type'=>'users','id'=>'100027'];//删除位于/my_index/my_type/的文档my_idprint_r($client->delete($params));13、搜索内容$json='{"query":{"match":{"id":"100073"}}}';$params=['index'=>'test_data','type'=>'users','body'=>$json];print_r($client->search($params));$params=['index'=>'test_data','type'=>'users','body'=>['query'=>['bool'=>['should'=>[['match'=>['昵称'=>['query'=>'user440032','boost'=>3,//权重大]]]],],],'sort'=>['id'=>['order'=>'desc']]//顺序页面,'from'=>0,'size'=>10]];print_r($client->search($params));
