php操作数据库的一般步骤是什么(PHP操作Elasticsearch)

wufei123 发布于 2023-11-04 阅读(1006)

最新php菜鸟教程答案

一、安装以下es基于6.41、在 composer.json 文件中引入 elasticsearch-php:{ "require":{ "ela源码sticsearch/elasticsearch":"~6.0", "monolog/monolog": "~1.0" } }

2、用 composer 源码安装客户端:curl -s http://getcomposer.org/installer | php php composer.phar install --no-dev

二、快速开源码始1、创建一个test.php文件,内容如下setHosts($hosts)->build(); // Instantiate a new ClientBuilder // Set the hosts源码 $params = [ index => test_data, type => users, id => 100027, 源码 client => [ ignore => 404 ] ]; var_dump( $client->get($params));

2、浏览器访问test.php,源码结果如下(前提是你的es已经有数据)

三、基本操作1、创建索引$params = [ index => test_index ]; // Create 源码the index print_r($client->indices()->create($params));

2、创建索引(指定模板)$params = [ index源码 => test_index, body => [ settings => [ number_of_shards => 5, numbe源码r_of_replicas => 2 ], mappings => [ test_type => [ _source => [ 源码 enabled => true ], properties => [ name => [ type => text, 源码 analyzer => ik_max_word ], age => [ type => integer ] ] 源码 ] ] ] ]; // Create the index with mappings and settings now 源码 print_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, typ源码e => test_type, body => [ test_type => [ _source => [ enabled => tru源码e ], properties => [ name => [ type => text, analyzer => ik_源码max_word ], age => [ type => integer ], createtime => [ 源码 type => date //加了一个时间 ] ] ] ] ]; // Update the源码 index mapping print_r($client->indices()->putMapping($params));

7、返回索引和类型的映射细节$response = $c源码lient->indices()->getMapping(); // Get mappings for all types in my_index $params =源码 [index => my_index]; $response = $client->indices()->getMapping($params); // Get m源码appings for all types of my_type, regardless of index $params = [type => my_type ]; 源码$response = $client->indices()->getMapping($params); // Get mapping my_type in my_index 源码 $params = [ index => my_index type => my_type ]; $response = $c源码lient->indices()->getMapping($params); // Get mappings for two indexes $params = [ 源码 index => [ my_index, my_index2 ] ]; $response = $client->indices()->getMappi源码ng($params);

8、索引一个文档(提供id,则会更新对应id的记录若没有提供,则会生成一条文档)$params = [ index => test_data, 源码type => users, id => 100027, body => [ nickname => update222] ]; //源码 Document will be indexed to my_index/my_type/my_id print_r($client->index($params));。

9、获取文档源码$params = [ index => test_data, type => users, id => 100027 ]; 源码 // Get doc at /my_index/my_type/my_id print_r($client->get($params));

10、更新文档 (doc指定要更新的字源码段内容)$params = [ index => test_data, type => users, id => 100027, bod源码y => [ doc => [ nickname => abc, mobile => 13800138000 ] ] 源码 ]; // Update doc at /my_index/my_type/my_id print_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 => te源码st_data, type => users, id => 100027 ]; // Delete doc at /my_index/源码my_type/my_id print_r($client->delete($params));

13、搜索内容$json = { "query" : { 源码 "match" : { "id" : "100073" } } }; $params = [ ind源码ex => test_data, type => users, body => $json ]; print_r($client->se源码arch($params)); $params = [ index => test_data, type => users, bod源码y => [ query => [ bool => [ should => [ [ match => [ nickname => [ 源码 query => user440032, boost => 3, // 权重大 ]]] ], ], ], 源码 sort => [id=>[order=>desc]] //排序 分页 , from => 0, size => 10 ] ]; 源码 print_r($client->search($params));

以上内容希望帮助到大家,很多PHPer在进阶的时候总会遇到一些问题和瓶颈,业务代码写多了没有方向感,不知道该从那里入手去源码提升,对此我整理了一些资料,包括但不限于:分布式架构、高可扩展、高性能、高并发、服务器性能调优、TP6,laravel,YII2,Redis,Swoole、Swoft、Kafka、Mysql优化、sh源码ell脚本、Docker、微服务、Nginx等多个知识点高级进阶干货需要的可以免费分享给大家

,需要>>视频、面试文档免费获取" class="LinkCard new css-1n5wicu" dat源码a-image="https://pic4.zhimg.com/v2-2686790c46d4e5db4d4b344c23a17517_ipico.jpg" data-image-width="150源码" data-image-height="150">

或 者关注咱们下面的知乎专栏作者:chen21 来源:https://www.cnblogs.com/killer21/p/12179936.htm源码l

亲爱的读者们,感谢您花时间阅读本文。如果您对本文有任何疑问或建议,请随时联系我。我非常乐意与您交流。

发表评论:

◎欢迎参与讨论,请在这里发表您的看法、交流您的观点。