快速开始

本章节会给你一个客户端的主要功能(函数)是如何工作的快速概述。

安装

  • 引入(包含)elasticsearch-php 在你的 composer.json 文件:
  1. {
  2. "require": {
  3. "elasticsearch/elasticsearch": "~1.0"
  4. }
  5. }
  • 使用composer安装客户端:
  1. curl -s http://getcomposer.org/installer | php
  2. php composer.phar install
  • 在主项目(一般是index.php)中引入autoloader.php文件(如果你还没有引入的话),并且要实例化Elasticsearch的客户端:
  1. require 'vendor/autoload.php';
  2. $client = new Elasticsearch\Client();

索引一个文档

在elasticsearch-php中,几乎所有的东西都是通过数组配置的。REST 的端点(终结点),文档和可选参数,一切都是一个关联数组。

去索引一个文档,我们简单的指定一个主体(body)来包含我们希望索引的文档。文档中的每一个字段都由一个关联数组的键/值对表示。

索引(index),类型(type)和 ID 也被指定在数组参数中,数组如下:

  1. $params = array();
  2. $params['body']  = array('testField' => 'abc');
  3. $params['index'] = 'my_index';
  4. $params['type']  = 'my_type';
  5. $params['id']    = 'my_id';
  6. $ret = $client->index($params);

获取一个文档

让我们来获取我们刚刚索引的文档:

  1. $getParams = array();
  2. $getParams['index'] = 'my_index';
  3. $getParams['type']  = 'my_type';
  4. $getParams['id']    = 'my_id';
  5. $retDoc = $client->get($getParams);

搜索一个文档

搜索是 elasticsearch 的一个标志,所以让我们执行搜索。我们打算使用匹配查询作为示范:

  1. $searchParams['index'] = 'my_index';
  2. $searchParams['type']  = 'my_type';
  3. $searchParams['body']['query']['match']['testField'] = 'abc';
  4. $retDoc = $client->search($searchParams);

删除一个文档

好的,让我们继续删除一个我们之前添加的文档:

  1. $deleteParams = array();
  2. $deleteParams['index'] = 'my_index';
  3. $deleteParams['type'] = 'my_type';
  4. $deleteParams['id'] = 'my_id';
  5. $retDelete = $client->delete($deleteParams);

删除一个索引

由于 elasticsearch 的动态性质,我们添加第一个文档的时候自动创建了索引和一些默认设置。让我们删除这个索引,因为我们以后想要指定自己的设置:

  1. $deleteParams = array();
  2. $deleteParams['index'] = 'my_index';
  3. $client->indices()->delete($deleteParams);

创建一个索引

好吧,我们的索引被清空了,现在我们开始添加一个新的索引和一些自定义设置:

  1. $indexParams['index'] = 'my_index';
  2. $indexParams['body']['settings']['number_of_shards'] = 2;
  3. $indexParams['body']['settings']['number_of_replicas'] = 0;
  4. $client->indices()->create($indexParams);

总结

那些只是在客户端速成课程和语法上的概述。如果你熟悉elasticsearch, 你会注意到,这些方法的命名就像 REST 的端点(终结点)。

你还会发现客户端的配置方式使你发现通过你的IDE配置会非常方便。所有的核心操作都在 $client 对象(索引,搜索,获取等)下。索引和集群管理分别位于 $client->indices() 和 $client->cluster() 对象下。

查看剩下的文档去了解整个客户端是如何工作的。

例子代码

  1. <?php
  2. require 'vendor/autoload.php';
  3. $client = new Elasticsearch\Client();
  4. index($client);
  5. //get($client);
  6. // search($client);
  7. // deleteDoc($client);
  8. // deleteIndex($client);
  9. // createIndex($client);
  10. function index($client) {
  11. $params = array ();
  12. $params ['body'] = array (
  13. 'testField' => 'abc'
  14. );
  15. $params ['index'] = 'my_index';
  16. $params ['type'] = 'my_type';
  17. $params ['id'] = 'my_id';
  18. try {
  19. $ret = $client->index($params);
  20. println("create index success");
  21. } catch(Exception $e) {
  22. echo $e->getMessage();
  23. }
  24. }
  25. function get($client) {
  26. $getParams = array ();
  27. $getParams ['index'] = 'my_index';
  28. $getParams ['type'] = 'my_type';
  29. $getParams ['id'] = 'my_id';
  30. $retDoc = $client->get($getParams);
  31. println($retDoc);
  32. }
  33. function search($client) {
  34. $searchParams ['index'] = 'my_index';
  35. $searchParams ['type'] = 'my_type';
  36. $searchParams ['body'] ['query'] ['match'] ['testField'] = 'abc';
  37. $retDoc = $client->search($searchParams);
  38. println($retDoc);
  39. }
  40. function deleteDoc($client) {
  41. $deleteParams = array ();
  42. $deleteParams ['index'] = 'my_index';
  43. $deleteParams ['type'] = 'my_type';
  44. $deleteParams ['id'] = 'my_id';
  45. $retDelete = $client->delete($deleteParams);
  46. println($retDelete);
  47. }
  48. function deleteIndex($client) {
  49. $deleteParams = array ();
  50. $deleteParams ['index'] = 'my_index';
  51. $retDelete = $client->indices()->delete($deleteParams);
  52. println($retDelete);
  53. }
  54. function createIndex($client) {
  55. $indexParams ['index'] = 'my_index';
  56. $indexParams ['body'] ['settings'] ['number_of_shards'] = 2;
  57. $indexParams ['body'] ['settings'] ['number_of_replicas'] = 0;
  58. $retCreate = $client->indices()->create($indexParams);
  59. println($retCreate);
  60. }
  61. function println($var) {
  62. echo "<br>";
  63. $type = gettype($var);
  64. if ($type == "array" || $type == "object") {
  65. echo json_encode($var);
  66. } else {
  67. echo $var;
  68. }
  69. echo "<br>";
  70. }

查看每个方法的运行结果:

index():

  1. create index success

get():

  1. {
  2. "_index": "my_index",
  3. "_type": "my_type",
  4. "_id": "my_id",
  5. "_version": 1,
  6. "found": true,
  7. "_source": {
  8. "testField": "abc"
  9. }
  10. }

search():

  1. {
  2. "took": 3,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 5,
  6. "successful": 5,
  7. "failed": 0
  8. },
  9. "hits": {
  10. "total": 1,
  11. "max_score": 0.30685282,
  12. "hits": [
  13. {
  14. "_index": "my_index",
  15. "_type": "my_type",
  16. "_id": "my_id",
  17. "_score": 0.30685282,
  18. "_source": {
  19. "testField": "abc"
  20. }
  21. }
  22. ]
  23. }
  24. }

deleteDoc():

  1. {
  2. "found": true,
  3. "_index": "my_index",
  4. "_type": "my_type",
  5. "_id": "my_id",
  6. "_version": 2
  7. }

deleteIndex():

  1. {
  2. "acknowledged": true
  3. }

createIndex():

    1. {
    2. "acknowledged": true
    3. }

Elasticsearch-PHP 快速开始的更多相关文章

  1. logstash+elasticsearch+kibana快速搭建日志平台

    使用logstash+elasticsearch+kibana快速搭建日志平台   日志的分析和监控在系统开发中占非常重要的地位,系统越复杂,日志的分析和监控就越重要,常见的需求有: 根据关键字查询日 ...

  2. 使用logstash+elasticsearch+kibana快速搭建日志平台

    日志的分析和监控在系统开发中占非常重要的地位,系统越复杂,日志的分析和监控就越重要,常见的需求有: * 根据关键字查询日志详情 * 监控系统的运行状况 * 统计分析,比如接口的调用次数.执行时间.成功 ...

  3. Elasticsearch【快速入门】

    前言:毕设项目还要求加了这个做大数据搜索,正好自己也比较感兴趣,就一起来学习学习吧! Elasticsearch 简介 Elasticsearch 是一个分布式.RESTful 风格的搜索和数据分析引 ...

  4. 【转载】使用logstash+elasticsearch+kibana快速搭建日志平台

    原文链接:http://www.cnblogs.com/buzzlight/p/logstash_elasticsearch_kibana_log.html 日志的分析和监控在系统开发中占非常重要的地 ...

  5. elasticsearch的快速安装

    在阿里云服务器快速安装ElasticSearch 1.安装好java的jdk环境 2.使用wget下载elasticsearch安装包,wget的速度比较满,如果等不及的话,可以先下载好安装包再上传解 ...

  6. 3.1_springboot2.x检索之elasticsearch安装&快速入门

    1.elasticsearch简介&安装 1.1.1.elasticsearch介绍 ​ 我们的应用经常需要添加检索功能,开源的 ElasticSearch 是目前全文搜索引擎的首选.他可以快 ...

  7. 如何通过Elasticsearch Scroll快速取出数据,构造pandas dataframe — Python多进程实现

    首先,python 多线程不能充分利用多核CPU的计算资源(只能共用一个CPU),所以得用多进程.笔者从3.7亿数据的索引,取200多万的数据,从取数据到构造pandas dataframe总共大概用 ...

  8. 使用Elasticsearch Operator快速部署Elasticsearch集群

    转载自:https://www.qikqiak.com/post/elastic-cloud-on-k8s/ 随着 kubernetes 的快速发展,很多应用都在往 kubernetes 上面迁移,现 ...

  9. Elasticsearch的快速使用——Spring Boot使用Elastcisearch, 并且使用Logstash同步mysql和Elasticsearch的数据

    我主要是给出一些方向,很多地方没有详细说明.当时我学习的时候一直不知道怎么着手,花时间找入口点上比较多,你们可以直接顺着方向去找资源学习. 如果不是Spring Boot项目,那么根据Elastics ...

  10. (转)开源分布式搜索平台ELK(Elasticsearch+Logstash+Kibana)入门学习资源索引

    Github, Soundcloud, FogCreek, Stackoverflow, Foursquare,等公司通过elasticsearch提供搜索或大规模日志分析可视化等服务.博主近4个月搜 ...

随机推荐

  1. JQ 知识点集合

    数组与字符串间的转换 一.数组转字符串(将数组元素用某个字符连接成字符串) var a, b; a = new Array(0,1,2,3,4); b = a.join("-"); ...

  2. 使用 Task.Wait()?立刻死锁(deadlock)

    最近读到一篇异步转同步的文章,发现其中没有考虑到异步转同步过程中发生的死锁问题,所以特地在本文说说异步转同步过程中的死锁问题. 文章作者 林德熙 已经修复了描述: - win10 uwp 异步转同步 ...

  3. CentOS常用基础命令汇总

    1.关机 (系统的关机.重启以及登出 ) 的命令 shutdown -h now 关闭系统(1) init 0 关闭系统(2) telinit 0 关闭系统(3) shutdown -h hours: ...

  4. .Net Remoting和Web Service大比拼

    随着.NET的推出,微软引入了一套新的通讯技术:Web Services和.NET remoting..NET remoting和ASP.NET Web Services可以为建立分布式的应用提供强有 ...

  5. Centos7下安装共存版本Python

    最近遇到个问题, 本机环境已安装了Python2   已安装的应用依赖于Python2,不能替换原系统的Python环境,但新安装的应用需要依赖于Python3 需要安装两个不同版本Python,解决 ...

  6. yugabyte cloud native db 基本试用

    备注: 测试环境使用docker进行安装试用 1. 安装 a. Download mkdir ~/yugabyte && cd ~/yugabyte wget https://down ...

  7. iOS App多语言国际化

    /*************************************1*******************************************/ /*************** ...

  8. DiscuzX 3. 3搭建和学习

    Discuz!全局变量$_G详解 http://jingyan.baidu.com/article/cb5d610516048c005c2fe0c8.html UCenter uc_user_synl ...

  9. bzoj1087互不侵犯King(状压)

    题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1087 简单的状压dp.但是wa了好几发.注意long long. 注意0和0的连边.而且不能 ...

  10. Excel不同工作簿之间提取信息

    Sub 不同工作簿间提取信息() '用于单个字段信息的提取: Dim w As Workbook, wb1 As Workbook, wb2 As Workbook, wb3 As Workbook ...