首页 > 编程语言 >php-elasticsearch客户端基本使用

php-elasticsearch客户端基本使用

时间:2023-08-02 16:23:01浏览次数:40  
标签:index string int client elasticsearch array php my 客户端

php-elasticsearch客户端基本使用

标签(空格分隔): php,elasticsearch

官方文档:https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/getting-started-php.html#_searching_documents
官方中文文档(已过时):https://www.elastic.co/guide/cn/elasticsearch/php/current/_configuration.html

开始

本页将指导您完成 PHP 客户端的安装过程,向您展示如何实例化客户端,以及如何使用它执行基本的 Elasticsearch 操作。

安装

composer require elasticsearch/elasticsearch

实例化客户端

require 'vendor/autoload.php';

$client = Elastic\Elasticsearch\ClientBuilder::create()->build();

创建索引

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();
$params = [
    'index' => 'my_index',
];

$response = $client->indices()->create($params);

// response print
array(3) {
  ["acknowledged"]=>
  bool(true)
  ["shards_acknowledged"]=>
  bool(true)
  ["index"]=>
  string(8) "my_index"
}

插入一条数据

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$response = $client->index([
    'index' => 'my_index',
    'body' => [
        'name' => 'Taylor',
        'age' => 32,
        'address' => "1101 S Main St APT 203 Milpitas CA 95035",
        'sex' => '女',
        'hobby' => ["唱歌", "跳舞", "谈恋爱", "走秀"]
    ]
]);
// response print
array(8) {
  ["_index"]=>
  string(8) "my_index"
  ["_type"]=>
  string(4) "_doc"
  ["_id"]=>
  string(20) "uH5JtIkBC78u-UjcI9s-"
  ["_version"]=>
  int(1)
  ["result"]=>
  string(7) "created"
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(2)
    ["successful"]=>
    int(1)
    ["failed"]=>
    int(0)
  }
  ["_seq_no"]=>
  int(0)
  ["_primary_term"]=>
  int(1)
}

插入多条

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = ['body' => []];
for ($i = 1; $i < 6;$i++) {
    $params['body'][] = [
        'index' =>  [
            '_index' => 'my_index',
            '_id' => $i
        ]
    ];
    $params['body'][] = [
        'name' => "Swift".$i,
        'age' => rand(10, 35),
        'address' => "1101 S Main St APT 203 Milpitas CA 95035",
        'sex' => "女",
        'hobby' => ["唱歌", "跳舞", "谈恋爱", "走秀"]
    ];
}

$response = $client->bulk($params);
// response print 新增5条数据
array(3) {
  ["took"]=>
  int(8)
  ["errors"]=>
  bool(false)
  ["items"]=>
  array(5) {
        [0]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "1"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(1)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [1]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "2"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(2)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [2]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "3"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(3)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [3]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "4"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(4)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
    [4]=>
    array(1) {
            ["index"]=>
      array(9) {
                ["_index"]=>
        string(8) "my_index"
                ["_type"]=>
        string(4) "_doc"
                ["_id"]=>
        string(1) "5"
                ["_version"]=>
        int(1)
        ["result"]=>
        string(7) "created"
                ["_shards"]=>
        array(3) {
                    ["total"]=>
          int(2)
          ["successful"]=>
          int(1)
          ["failed"]=>
          int(0)
        }
        ["_seq_no"]=>
        int(5)
        ["_primary_term"]=>
        int(1)
        ["status"]=>
        int(201)
      }
    }
  }
}

更新数据

use Elasticsearch\ClientBuilder;

include "../vendor/autoload.php";

$hosts = [
    "http://192.168.33.10:9200"
];

$client = ClientBuilder::create()->setHosts($hosts)->build();

$params = [
    'index' => 'my_index',
    'id'    => 'uH5JtIkBC78u-UjcI9s-',
    'body'  => [
        'doc' => [
            'address' => '美国'
        ]
    ]
];

// Update doc at /my_index/_doc/my_id
$response = $client->update($params);
// response print
array(8) {
  ["_index"]=>
  string(8) "my_index"
  ["_type"]=>
  string(4) "_doc"
  ["_id"]=>
  string(20) "uH5JtIkBC78u-UjcI9s-"
  ["_version"]=>
  int(2)
  ["result"]=>
  string(7) "updated"
  ["_shards"]=>
  array(3) {
    ["total"]=>
    int(2)
    ["successful"]=>
    int(1)
    ["failed"]=>
    int(0)
  }
  ["_seq_no"]=>
  int(6)
  ["_primary_term"]=>
  int(1)
}

删除数据

$params = [
    'index' => 'my_index',
    'id'    => 'my_id'
];

// Delete doc at /my_index/_doc_/my_id
$response = $client->delete($params);

删除索引

$params = ['index' => 'my_index'];

$response = $client->indices()->delete($params);

查询所有

$params = [
    'index' => 'my_index',
];

$response = $client->search($params);

查询一条

$params = [
    'index' => 'my_index',
    'id'    => 'uH5JtIkBC78u-UjcI9s-',
];

$response = $client->get($params);

match查询

$client = ClientBuilder::create()->setHosts($hosts)->build();

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'match' => [
                'name' => 'taylor'
            ]
        ]
    ]
]);

term查询

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'term' => [
                'name' => 'taylor'
            ]
        ]
    ]
]);

range查询

$response = $client->search([
    'index' => 'my_index',
    'body'  => [
        'query' => [
            'range' => [
                'age' => [
                    'gte' => 10,
                    'lte' => 20
                ]
            ]
        ]
    ]
]);

bool组合查询

$params = [
    'index' => 'my_index',
    'type' => 'my_type',
    'body' => [
        'query' => [
            'bool' => [
                'filter' => [
                    'term' => [ 'address' => 'abc' ]
                ],
                'should' => [
                    'match' => [ 'name' => 'xyz' ]
                ]
            ]
        ]
    ]
];

$results = $client->search($params);

标签:index,string,int,client,elasticsearch,array,php,my,客户端
From: https://www.cnblogs.com/yanweifeng/p/17601001.html

相关文章

  • PHP反序列化例题以及Bypass总结
    unseping题目源码<?phphighlight_file(__FILE__);classease{private$method;private$args;function__construct($method,$args){$this->method=$method;$this->args=$args;}function__destruct(){......
  • 服务模型(客户端/服务器)
    服务模型创建功能包cd~/catkin_ws/srccatkin_create_pkglearning_serviceroscpprospystd_msgsgeometry_msgsturtlesim客户端Client如何实现一个客户端初始化ROS节点;创建一个Client实例;发布服务请求数据;等待Server处理之后的应答结果。客户端代码turtle_spqwn.......
  • PHP客服系统聊天页面-thinkphp加载页面源码解释
    PHPworkerman客服系统加载聊天页面的代码逻辑流程,可以进行参考。如果想要二开修改的,可以根据这个流程来修改。thinkphp的router部分Route::get('kefu/:u/:f?','index/index/chat');查看控制器加载页面逻辑application/index/controller/Index.phppublicfunctionchat函......
  • tomcat环境部署verto客户端
    tomcat环境下部署verto客户端1.generateaself-signedcertificateforTomcatusingOpenSSLStep1:Generateaprivatekeyopensslgenpkey-algorithmRSA-outprivate.keyStep2:Createacertificatesigningrequest(CSR)opensslreq-new-keyprivate.key-......
  • PHP的ICP备案信息查询接口,从网站内容中抓取
    <?phpfunctionget_icp_info($url){//使用cURL获取目标网站的HTML内容$ch=curl_init();curl_setopt($ch,CURLOPT_URL,$url);curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);$output=curl_exec($ch);curl_close($ch);//解析HTML内容,查找......
  • PHPGD库如何使用SVG格式进行图像处理
    使用PHPGD库进行图像处理是PHP编程开发中常用的技术,而将其与SVG格式结合使用可以使图像处理更加灵活、高效和美观。本篇文章将围绕PHPGD库如何使用SVG格式进行图像处理展开探讨。一、什么是SVG格式?PHPGD库如何使用SVG格式进行图像处理SVG是可缩放矢量图形(ScalableVectorGra......
  • PHP反序列化
    PHP反序列化序列化序列化的作用将对象或者数组转化为可存储/传输的字符串对象序列化O:4:"info":3:{s:4:"name";s:7:"iami233";s:6:"\x00*\x00age";s:2:"18";s:8:"\x00ctf\x00sex";s:7:"unknown";}//O:对象名的长度:"对象名"......
  • PHPJSON数据格式常见应用及实例解析
    PHPJSON数据格式常见应用及实例解析随着Web应用的兴起和普及,数据的传输和处理已经成为Web开发中不可或缺的一部分。PHP作为一种广泛使用的服务器端编程语言,对于数据的处理和传输也有着非常丰富的支持。其中,JSON数据格式已经成为Web开发中最常用的数据格式之一。本文将结合实例,介......
  • PHPGD图像复制教程
    PHPGD图像复制教程在PHP的图像处理中,复制图像是一个非常常见的操作。不仅可以用于缩略图的生成,还可以用于其他方面的图像处理。本文将教你如何使用PHPGD库来复制图像,以及如何优化复制过程以提高性能和图像质量。PHPGD图像复制教程一、使用imagecopy函数复制图像imagecopy函数......
  • PHPGrafika 如何实现圆角图片
    PHPGrafika如何实现圆角图片在网站开发中,圆角图片是非常常见的一种设计元素。使用PHPGrafika库可以很方便的实现圆角图片的制作。本文将介绍如何使用PHPGrafika库制作圆角图片的方法。PHPGrafika如何实现圆角图片PHPGrafika是一款PHP图像处理库,它提供了许多图像处理功......