• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

PHP 使用 Elasticsearch 的 索引 API 接口

武飞扬头像
菜鸟实战
帮助2

目录

一、实战场景

二、知识点

PHP

Elasticsearch

索引 index

MySQL

三、菜鸟实战


一、实战场景

如何在 PHP 中使用 Elasticsearch 的索引 API 接口

二、知识点

  • PHP

  • Elasticsearch

  • 索引 index

  • MySQL

Elasticsearch 本质上是一个数据库,但并不是 MySQL 这种关系型数据库,查询语言也不是 SQL,而是 Elasticsearch 自己的一套查询语言。既然是数据库,有一些概念是互通的,如下表:

学新通

三、菜鸟实战

基础环境准备可参考之前文章。

创建索引

发送创建请求

创建索引返回结果

  1.  
    PHP
  2.  
     
  3.  
    // 创建索引
  4.  
     
  5.  
    public function create(Request $request){
  6.  
     
  7.  
    // 获取索引名称
  8.  
     
  9.  
    $testIndex = $request->get("index_name", "test");
  10.  
     
  11.  
     
  12.  
    // 执行
  13.  
     
  14.  
    $client = EsHelper::getEsClient();
  15.  
     
  16.  
    $params = [
  17.  
     
  18.  
    'index' => $testIndex,
  19.  
     
  20.  
    ];
  21.  
     
  22.  
    $response = $client->indices()->create($params);
  23.  
     
  24.  
     
  25.  
    // 返回
  26.  
     
  27.  
    $data = [
  28.  
     
  29.  
    'es_info' => $response->asArray(),
  30.  
     
  31.  
    ];
  32.  
     
  33.  
    $this->success($data);
  34.  
     
  35.  
    }
学新通
  1.  
    JSON
  2.  
     
  3.  
    {
  4.  
     
  5.  
    "code": 0,
  6.  
     
  7.  
    "message": "ok",
  8.  
     
  9.  
    "data": {
  10.  
     
  11.  
    "es_info": {
  12.  
     
  13.  
    "acknowledged": true,
  14.  
     
  15.  
    "shards_acknowledged": true,
  16.  
     
  17.  
    "index": "test"
  18.  
     
  19.  
    }
  20.  
     
  21.  
    }
  22.  
     
  23.  
    }
学新通

更加复杂的参数

可指定主分片个数:number_of_shards

可指定副分片个数:number_of_replicas

  1.  
    PHP
  2.  
     
  3.  
    $params = [
  4.  
     
  5.  
    'index' => 'my_index',
  6.  
     
  7.  
    'body' => [
  8.  
     
  9.  
    'settings' => [
  10.  
     
  11.  
    'number_of_shards' => 3,
  12.  
     
  13.  
    'number_of_replicas' => 2
  14.  
     
  15.  
    ],
  16.  
     
  17.  
    'mappings' => [
  18.  
     
  19.  
    'my_type' => [
  20.  
     
  21.  
    '_source' => [
  22.  
     
  23.  
    'enabled' => true
  24.  
     
  25.  
    ],
  26.  
     
  27.  
    'properties' => [
  28.  
     
  29.  
    'first_name' => [
  30.  
     
  31.  
    'type' => 'string',
  32.  
     
  33.  
    'analyzer' => 'standard'
  34.  
     
  35.  
    ],
  36.  
     
  37.  
    'age' => [
  38.  
     
  39.  
    'type' => 'integer'
  40.  
     
  41.  
    ]
  42.  
     
  43.  
    ]
  44.  
     
  45.  
    ]
  46.  
     
  47.  
    ]
  48.  
     
  49.  
    ]
  50.  
     
  51.  
    ];
学新通

查询索引详情

发送查询请求

  1.  
    PHP
  2.  
     
  3.  
    // 查询索引
  4.  
     
  5.  
    public function detail(Request $request){
  6.  
     
  7.  
    // 获取索引名称
  8.  
     
  9.  
    $queryIndexName = $request->get("index_name", "test");
  10.  
     
  11.  
    $params = [
  12.  
     
  13.  
    'index' => $queryIndexName,
  14.  
     
  15.  
    ];
  16.  
     
  17.  
    // 执行
  18.  
     
  19.  
    $client = EsHelper::getEsClient();
  20.  
     
  21.  
     
  22.  
    $result = "";
  23.  
     
  24.  
    try {
  25.  
     
  26.  
    $response = $client->indices()->get($params);
  27.  
     
  28.  
    $result = $response->asArray();
  29.  
     
  30.  
    }catch (\Exception $e){
  31.  
     
  32.  
    $result = $e->getMessage();
  33.  
     
  34.  
    }
  35.  
     
  36.  
     
  37.  
    // 返回
  38.  
     
  39.  
    $data = [
  40.  
     
  41.  
    'es_info' => $result,
  42.  
     
  43.  
    ];
  44.  
     
  45.  
    $this->success($data);
  46.  
     
  47.  
    }
学新通

响应结果

  1.  
    JSON
  2.  
     
  3.  
    {
  4.  
     
  5.  
    "code": 0,
  6.  
     
  7.  
    "message": "ok",
  8.  
     
  9.  
    "data": {
  10.  
     
  11.  
    "es_info": {
  12.  
     
  13.  
    "test1": {
  14.  
     
  15.  
    "aliases": [],
  16.  
     
  17.  
    "mappings": [],
  18.  
     
  19.  
    "settings": {
  20.  
     
  21.  
    "index": {
  22.  
     
  23.  
    "routing": {
  24.  
     
  25.  
    "allocation": {
  26.  
     
  27.  
    "include": {
  28.  
     
  29.  
    "_tier_preference": "data_content"
  30.  
     
  31.  
    }
  32.  
     
  33.  
    }
  34.  
     
  35.  
    },
  36.  
     
  37.  
    "number_of_shards": "1",
  38.  
     
  39.  
    "provided_name": "test1",
  40.  
     
  41.  
    "creation_date": "1669179904968",
  42.  
     
  43.  
    "number_of_replicas": "1",
  44.  
     
  45.  
    "uuid": "Y62XH2Z4RC-kd2cKieOu9Q",
  46.  
     
  47.  
    "version": {
  48.  
     
  49.  
    "created": "8050199"
  50.  
     
  51.  
    }
  52.  
     
  53.  
    }
  54.  
     
  55.  
    }
  56.  
     
  57.  
    }
  58.  
     
  59.  
    }
  60.  
     
  61.  
    }
  62.  
     
  63.  
    }
学新通

更新索引

发送更新索引请求

  1.  
    PHP
  2.  
     
  3.  
    // 更新索引
  4.  
     
  5.  
    public function update(Request $request){
  6.  
     
  7.  
    // 获取索引名称
  8.  
     
  9.  
    $indexName = $request->get("index_name", "test");
  10.  
     
  11.  
    $number_of_replicas = $request->get("number_of_replicas", 1);
  12.  
     
  13.  
     
  14.  
    // 确定参数
  15.  
     
  16.  
    $params = [
  17.  
     
  18.  
    'index' => $indexName,
  19.  
     
  20.  
    'body' => [
  21.  
     
  22.  
    'settings' => [
  23.  
     
  24.  
    'number_of_replicas' => $number_of_replicas,
  25.  
     
  26.  
    ]
  27.  
     
  28.  
    ]
  29.  
     
  30.  
    ];
  31.  
     
  32.  
     
  33.  
    // 执行
  34.  
     
  35.  
    $client = EsHelper::getEsClient();
  36.  
     
  37.  
    try {
  38.  
     
  39.  
    $response = $client->indices()->putSettings($params);
  40.  
     
  41.  
    $result = $response->asArray();
  42.  
     
  43.  
    }catch (\Exception $e){
  44.  
     
  45.  
    $result = $e->getMessage();
  46.  
     
  47.  
    }
  48.  
     
  49.  
     
  50.  
    // 返回
  51.  
     
  52.  
    $data = [
  53.  
     
  54.  
    'es_info' => $result,
  55.  
     
  56.  
    ];
  57.  
     
  58.  
    $this->success($data);
  59.  
     
  60.  
    }
学新通

响应结果

  1.  
    JSON
  2.  
     
  3.  
    {
  4.  
     
  5.  
    "code": 0,
  6.  
     
  7.  
    "message": "ok",
  8.  
     
  9.  
    "data": {
  10.  
     
  11.  
    "es_info": {
  12.  
     
  13.  
    "acknowledged": true
  14.  
     
  15.  
    }
  16.  
     
  17.  
    }
  18.  
     
  19.  
    }
学新通

删除索引

发送删除索引请求

  1.  
    PHP
  2.  
     
  3.  
    // 删除索引
  4.  
     
  5.  
    public function delete(Request $request){
  6.  
     
  7.  
    // 获取索引名称
  8.  
     
  9.  
    $indexName = $request->get("index_name", "test1");
  10.  
     
  11.  
     
  12.  
    // 确定参数
  13.  
     
  14.  
    $params = [
  15.  
     
  16.  
    'index' => $indexName,
  17.  
     
  18.  
    ];
  19.  
     
  20.  
     
  21.  
    // 执行
  22.  
     
  23.  
    $client = EsHelper::getEsClient();
  24.  
     
  25.  
    try {
  26.  
     
  27.  
    $response = $client->indices()->delete($params);
  28.  
     
  29.  
    $result = $response->asArray();
  30.  
     
  31.  
    }catch (\Exception $e){
  32.  
     
  33.  
    $result = $e->getMessage();
  34.  
     
  35.  
    }
  36.  
     
  37.  
     
  38.  
    // 返回
  39.  
     
  40.  
    $data = [
  41.  
     
  42.  
    'es_info' => $result,
  43.  
     
  44.  
    ];
  45.  
     
  46.  
    $this->success($data);
  47.  
     
  48.  
    }
学新通

响应结果

  1.  
    JSON
  2.  
     
  3.  
    {
  4.  
     
  5.  
    "code": 0,
  6.  
     
  7.  
    "message": "ok",
  8.  
     
  9.  
    "data": {
  10.  
     
  11.  
    "es_info": {
  12.  
     
  13.  
    "acknowledged": true
  14.  
     
  15.  
    }
  16.  
     
  17.  
    }
  18.  
     
  19.  
    }
学新通

通过上述步骤,就将 php 与 Elasticsearch 的索引操作连通了,接下来就可以在索引上创建文档了。

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhhbajba
系列文章
更多 icon
同类精品
更多 icon
继续加载