前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >PHP Elasticsearch的基本使用方法

PHP Elasticsearch的基本使用方法

作者头像
双面人
发布2019-09-02 16:01:30
1.5K0
发布2019-09-02 16:01:30
举报
文章被收录于专栏:热爱IT热爱IT

对于Elasticsearch与Elasticsearch-php的安装,网上有比较多的教程,这里不再累述。只是要注意Elasticsearch、Elasticsearch-php与php的版本。这里笔者使用的是Elasticsearch 5.6.8 windows版、php 5.6 、php onethink框架(以下简称ot)、Elasticsearch-php composer如下:(PHP Composer 视频教程

1 2 3 4 5

{      "require":{          "elasticsearch/elasticsearch" : "~5.0"      }  }

一、连接Elasticsearch:

1、Elasticsearch开启之后,可以直接通过http://127.0.0.1:9200/查看基本信息。

2、将composer vendor下的文件复制到ot ThinkPHP\Library\Vendor\elasticsearch目录下。

3、连接Elasticsearch,

1 2 3 4 5 6 7 8 9 10 11 12 13 14

public $es;   /**  * 初始化  */ public function _initialize() {     Vendor('elasticsearch.autoload');     //host数组可配置多个节点     $params = array(         '127.0.0.1:9200'     );     $this->es = \Elasticsearch\ClientBuilder::create()->setHosts($params)->build(); }

其中build()方法会将ClientBuilder 对象转换为一个Client对象。

二、Elasticsearch-php使用:

1、创建index:

关于index与type,这里特别纠正一个说法,index 就像关系型数据库里的 database, type 就像 database 里的 table,这种理解是错误的。

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46

/**  * 创建索引  */ public function createIndex(){     $params = [         'index' => 'test', //索引名称         'body' => [             'settings'=> [ //配置                 'number_of_shards'=> 3,//主分片数                 'number_of_replicas'=> 1 //主分片的副本数             ],             'mappings'=> [  //映射                 '_default_' => [ //默认配置,每个类型缺省的配置使用默认配置                     '_all'=>[   //  关闭所有字段的检索                         'enabled' => 'false'                     ],                     '_source'=>[   //  存储原始文档                         'enabled' => 'true'                     ],                     'properties'=> [ //配置数据结构与类型                         'name'=> [ //字段1                             'type'=>'string',//类型 string、integer、float、double、boolean、date                             'index'=> 'analyzed',//索引是否精确值  analyzed not_analyzed                         ],                         'age'=> [ //字段2                             'type'=>'integer',                         ],                         'sex'=> [ //字段3                             'type'=>'string',                             'index'=> 'not_analyzed',                         ],                     ]                 ],                 'my_type' => [                     'properties' => [                         'phone'=> [                             'type'=>'string',                         ],                                                ]                 ],             ],         ]     ];       $res = $this->es->indices()->create($params); }

在使用Elasticsearch-php API的时候,参数$params一般是用数组来,因为数组结构能很方便的转换为json。其中

_default_是默认配置,其他配置的缺省值都与_default_的相同。

_all设置true会将所有原始文档拼接在一起额外存储,

_source设置为true会存储原始文档,设置false一般用在只需要索引出文档的标题或者Url,通过Url去访问文档,而不需要在es中保存一份文档内容的场景。

最后,注意同一index下不同type中的同名称字段的数据类型与配置也必须相同!

2、删除index:

1 2 3 4 5 6 7 8 9 10

/**  * 删除索引  */  public function deleteIndex(){     $params = [         'index' => 'test'     ];       $res = $this->es->indices()->delete($params);  }

3、查看Mappings:

1 2 3 4 5 6 7

public function getMappings(){     $params = [         'index' => 'test'     ];       $res = $this->es->indices()->getMapping($params); }

4、修改Mappings:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17

public function putMappings(){     $params = [                   'index' => 'test',         'type' => 'my_type',         'body' => [             'my_type' => [                 'properties' => [                     'idcard' => [                         'type' => 'integer'                     ]                 ]             ]         ]     ];       $res = $this->es->indices()->putMapping($params);      }

注意:修改Mappings的API必须要指明type,且只能添加,不能修改已有的属性。

5、插入单条 Document:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15

public function postSinDoc(){     $params = [         'index' => 'test',         'type' => 'my_type',         'body' => [             'age' => 17,             'name' => 'saki',             'sex' => '女性',             'idcard' => 1112,             'phone' => '1245789',         ]     ];       $res = $this->es->index($params); }

6、插入多条 Document:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

public function postBulkDoc(){     for($i = 0; $i < 5; $i++) {         $params['body'][] = [             'index' => [                 '_index' => 'test',                 '_type' => 'my_type',             ]         ];           $params['body'][] = [             'age' => 17+$i,             'name' => 'reimu'.$i,             'sex' => '女性',             'idcard' => 1112+$i,             'phone' => '1245789'.$i,         ];     }       $res = $this->es->bulk($params); }

7、通过id获取Document:

1 2 3 4 5 6 7 8 9

public function getDocById(){     $params = [         'index' => 'test',         'type' => 'my_type',         'id' => 'AWIDV5l2A907wJBVKu6k'     ];       $res = $this->es->get($params); }

8、通过id更新Document:

1 2 3 4 5 6 7 8 9 10 11 12 13 14

public function updateDocById(){     $params = [         'index' => 'test',         'type' => 'my_type',         'id' => 'AWIDV5l2A907wJBVKu6k',         'body' => [             'doc' => [ //将doc中的文档与现有文档合并                 'name' => 'marisa'             ]         ]     ];       $res = $this->es->update($params); }

9、通过id删除Document:

1 2 3 4 5 6 7 8 9

public function deleteDocById(){     $params = [         'index' => 'test',         'type' => 'my_type',         'id' => 'AWIDV5l2A907wJBVKu6k'     ];       $res = $this->es->delete($params); }

注意:以上通过id的三个操作,如果未找到id,Elasticsearch-php会直接报错!

10、搜索Document:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

public function searchDoc(){     $params = [         'index' => 'test',         'type' => 'my_type',         'body' => [             'query' => [                 'constant_score' => [ //非评分模式执行                     'filter' => [ //过滤器,不会计算相关度,速度快                         'term' => [ //精确查找,不支持多个条件                             'name' => 'reimu0'                         ]                     ]                                       ]             ]         ]     ];       $res = $this->es->search($params);

这里只是搜索的一个示例。

(adsbygoogle = window.adsbygoogle || []).push({});

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
Elasticsearch Service
腾讯云 Elasticsearch Service(ES)是云端全托管海量数据检索分析服务,拥有高性能自研内核,集成X-Pack。ES 支持通过自治索引、存算分离、集群巡检等特性轻松管理集群,也支持免运维、自动弹性、按需使用的 Serverless 模式。使用 ES 您可以高效构建信息检索、日志分析、运维监控等服务,它独特的向量检索还可助您构建基于语义、图像的AI深度应用。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档