Elasticsearch-PHP 快速開始

      網友投稿 712 2025-03-31

      快速開始

      這一節會概述一下客戶端以及客戶端的一些主要方法的使用規則。

      安裝

      在 composer.json 文件中引入 elasticsearch-php:

      Elasticsearch-PHP 快速開始

      {

      "require": {

      "elasticsearch/elasticsearch": "~6.0"

      }

      }

      用 composer 安裝客戶端:

      curl -s http://getcomposer.org/installer | php

      php composer.phar install --no-dev

      在項目中引入自動加載文件(如果還沒引入),并且實例化一個客戶端:

      require 'vendor/autoload.php';

      use Elasticsearch\ClientBuilder;

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

      索引一個文檔

      在 elasticsearch-php 中,幾乎一切操作都是用關聯數組來配置。REST 路徑(endpoint)、文檔和可選參數都是用關聯數組來配置。

      為了索引一個文檔,我們要指定4部分信息:index,type,id 和一個 body。構建一個鍵值對的關聯數組就可以完成上面的內容。body 的鍵值對格式與文檔的數據保持一致性。(譯者注:如 ["testField" ? "abc"] 在文檔中則為 {"testField" : "abc"}):

      $params = [

      'index' => 'my_index',

      'type' => 'my_type',

      'id' => 'my_id',

      'body' => ['testField' => 'abc']

      ];

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

      print_r($response);

      收到的響應數據表明,你指定的索引中已經創建好了文檔。響應數據是一個關聯數組,里面的內容是 Elasticsearch 返回的decoded JSON 數據:

      Array

      (

      [_index] => my_index

      [_type] => my_type

      [_id] => my_id

      [_version] => 1

      [result] => created

      [_shards] => Array

      (

      [total] => 2

      [successful] => 1

      [failed] => 0

      )

      [_seq_no] => 0

      [_primary_term] => 1

      )

      獲取一個文檔

      現在獲取剛才索引的文檔:

      $params = [

      'index' => 'my_index',

      'type' => 'my_type',

      'id' => 'my_id'

      ];

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

      print_r($response);

      響應數據包含一些元數據(如 index,type 等)和?_source?屬性, 這是你發送給 Elasticsearch 的原始文檔數據。

      Array

      (

      [_index] => my_index

      [_type] => my_type

      [_id] => my_id

      [_version] => 1

      [found] => 1

      [_source] => Array

      (

      [testField] => abc

      )

      )

      搜索一個文檔

      搜索是 elasticsearch 的一大特色,所以我們試一下執行一個搜索。我們準備用 Match 查詢來作為示范:

      $params = [

      'index' => 'my_index',

      'type' => 'my_type',

      'body' => [

      'query' => [

      'match' => [

      'testField' => 'abc'

      ]

      ]

      ]

      ];

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

      print_r($response);

      這個響應數據與前面例子的響應數據有所不同。這里有一些元數據(如?took,?timed_out?等)和一個?hits?的數組,這代表了你的搜索結果。而?hits?內部也有一個?hits?數組,內部的?hits?包含特定的搜索結果:

      Array

      (

      [took] => 16

      [timed_out] =>

      [_shards] => Array

      (

      [total] => 5

      [successful] => 5

      [skipped] => 0

      [failed] => 0

      )

      [hits] => Array

      (

      [total] => 1

      [max_score] => 0.2876821

      [hits] => Array

      (

      [0] => Array

      (

      [_index] => my_index

      [_type] => my_type

      [_id] => my_id

      [_score] => 0.2876821

      [_source] => Array

      (

      [testField] => abc

      )

      )

      )

      )

      )

      刪除一個文檔

      好了,現在我們看一下如何把之前添加的文檔刪除掉:

      $params = [

      'index' => 'my_index',

      'type' => 'my_type',

      'id' => 'my_id'

      ];

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

      print_r($response);

      你會注意到刪除文檔的語法與獲取文檔的語法是一樣的。唯一不同的是?delete?方法替代了?get?方法。下面響應數據代表文檔已被刪除:

      Array

      (

      [_index] => my_index

      [_type] => my_type

      [_id] => my_id

      [_version] => 2

      [result] => deleted

      [_shards] => Array

      (

      [total] => 2

      [successful] => 1

      [failed] => 0

      )

      [_seq_no] => 1

      [_primary_term] => 1

      )

      刪除一個索引

      由于 elasticsearch 的動態特性,我們創建的第一個文檔會自動創建一個索引,同時也會把 settings 里面的參數設定為默認參數。由于我們在后面要指定特定的 settings,所以現在要刪除掉這個索引:

      $deleteParams = [

      'index' => 'my_index'

      ];

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

      print_r($response);

      響應數據是:

      Array

      (

      [acknowledged] => 1

      )

      創建一個索引

      由于數據已被清空,我們可以重新開始了,現在要添加一個索引,同時要進行自定義 settings:

      $params = [

      'index' => 'my_index',

      'body' => [

      'settings' => [

      'number_of_shards' => 2,

      'number_of_replicas' => 0

      ]

      ]

      ];

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

      print_r($response);

      Elasticsearch會創建一個索引,并配置你指定的參數值,然后返回一個消息確認:

      Array

      (

      [acknowledged] => 1

      [shards_acknowledged] => 1

      [index] => my_index

      )

      本節結語

      這里只是概述了一下客戶端以及它的語法。如果你很熟悉 elasticsearch,你會注意到這些方法的命名跟 REST 路徑(endpoint)是一樣的。

      你也注意到了客戶端的參數配置從某種程度上講也是方便你的IDE易于搜索。$client 對象下的所有核心方法(索引,搜索,獲取等)都是可用的。索引管理和集群管理分別在?$client->indices()?和?$client->cluster()?中。

      測試實例:

      require 'vendor/autoload.php';

      use Elasticsearch\ClientBuilder;

      $hosts = [

      '127.0.0.1:9200'

      ];

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

      //1.創建一個文檔

      $params = [

      'index' => 'my_index',

      'type' => 'my_type',

      'id' => 'my_id_2',

      'body' => ['testField' => 'abc']

      ];

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

      print_r($response);

      //2.獲取一個文檔

      $params = [

      'index' => 'accounts',

      'type' => 'person',

      'id' => '2'

      ];

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

      print_r($response);

      //3.搜索一個文檔

      $params=[

      'index'=>'my_index',

      'type'=>'my_type',

      'body'=>[

      'query'=>[

      'match'=>['testField'=>'abc']

      ]

      ]

      ];

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

      print_r($response);

      //4.刪除一個文檔

      $params=[

      'index'=>'my_index',

      'type'=>'my_type',

      'id'=>'my_id'

      ];

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

      print_r($response);

      //5.刪除一個索引

      $deleteParams=[

      'index'=>'my_index'

      ];

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

      print_r($response);

      //6.創建索引

      $params=[

      'index'=>'my_index',

      'body'=>[

      'settings'=>[

      'number_of_shards'=>2,

      'number_of_replicas'=>0

      ],

      ]

      ];

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

      print_r($response);

      Elasticsearch PHP

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。

      上一篇:身份證號碼算出出生年月日(根據身份證號算出生年月日)
      下一篇:WPS2019文字怎么批量轉換為圖片? wps文字替換為圖片的技巧
      相關文章
      麻豆亚洲AV成人无码久久精品| 亚洲人成精品久久久久| 亚洲另类激情专区小说图片| 亚洲黑人嫩小videos| 亚洲区小说区图片区QVOD| 浮力影院亚洲国产第一页| 亚洲精品麻豆av| 亚洲国产精品自产在线播放| 亚洲 小说区 图片区 都市| 亚洲Av无码国产情品久久| 一区二区三区亚洲视频| 亚洲高清无码在线观看| 亚洲综合精品网站在线观看| 久久久久国产成人精品亚洲午夜 | 亚洲欧洲日韩极速播放| 亚洲 欧洲 日韩 综合在线| 亚洲AV无码无限在线观看不卡 | 中文字幕亚洲专区| 色久悠悠婷婷综合在线亚洲| 亚洲午夜福利717| 亚洲中文字幕无码久久2017| 国产亚洲成av人片在线观看| 亚洲av无码一区二区乱子伦as| 久久噜噜噜久久亚洲va久| 亚洲福利在线视频| 中文字幕亚洲免费无线观看日本| 亚洲精品电影天堂网| 亚洲娇小性色xxxx| 亚洲精品宾馆在线精品酒店| 国产午夜亚洲精品不卡电影| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 国产AV无码专区亚洲AV漫画 | 久久久久se色偷偷亚洲精品av| 亚洲入口无毒网址你懂的| 亚洲欧美中文日韩视频| 国产亚洲成在线播放va| 相泽亚洲一区中文字幕| 亚洲av无码国产精品夜色午夜| 亚洲精品午夜视频| 亚洲熟妇AV乱码在线观看| 国产午夜亚洲精品不卡免下载 |