一、關於Elasticsearch-PHP
雖然Elasticsearch是Java開發的,但是官方推出一個PHP 客戶端:Elasticsearch-PHP
,讓我這些phper
使用起來非常萬多火
(wonderful)。
二、安裝Elasticsearch-PHP
1、首先需要安裝Elasticsearch
傳送門:Elasticsearch安裝
安裝完成後啓動Elasticsearch
,不然即使安裝完Elasticsearch-PHP
也不能用。
2、安裝Elasticsearch-PHP
要求
- PHP 7.0.0 或更高版本
- Composer
- ext-curl:PHP 的 Libcurl 擴展(詳情查看下方注意事項)
- 原生 JSON 擴展 (ext-json) 1.3.7或更高版本
我php環境是
7.0
,然後開啓的是php_curl擴展
然後我的JSON擴展爲1.4.0
:
3、composer下載Elasticsearch-PHP
composer require elasticsearch/elasticsearch
安裝完成後:
三、使用Elasticsearch-PHP
我把項目放到了網站目錄,並且新建了一個Test.php
。
1、測試是否成功連接
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:測試連接
* author:php狗腎
* time:2020/5/26 16:37
*/
public function testClient(){
print_r($this->client);
}
}
$obj = new Test();
$obj->testClient();
瀏覽器訪問:http://localhost/elasticserach/Test.php
沒問題,成功連接!
2、創建一個索引:
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:創建一個索引
* author:php狗腎
* time:2020/5/26 16:54
*/
public function add_index(){
$params = [
'index' => 'my_index'
];
$response = $this->client->indices()->create($params);
print_r($response);
}
}
$obj = new Test();
$obj->add_index();
打印結果:
從elasticsearch-head
裏面查看:
3、給索引添加一個文檔:
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:給某個索引下添加文檔
* author:php狗腎
* time:2020/5/26 16:52
*/
public function add_document(){
$params = [
'index' => 'newindex',
'type' => 'newtype',
'body' => [
'name' => '光頭強',
]
];
$response = $this->client->index($params);
print_r($response);
}
}
$obj = new Test();
$obj->add_document();
打印結果:
從elasticsearch-head
裏面查看:
4、獲取某個索引下的全部數據
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:獲取某個索引下的全部數據
* author:php狗腎
* time:2020/5/22 15:46
*/
public function getAllDoucment(){
$params = [
'index' => 'newindex',
];
$response = $this->client->search($params);
echo "<pre>";
print_r($response);
echo "<pre>";
}
}
$obj = new Test();
$obj->add_document();
打印結果:
5、對某個索引下的全部數據進行搜索
事先準備點數據
:
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:對某個索引下的數據進行簡單搜索
* author:php狗腎
* time:2020/5/26 17:13
*/
public function to_search(){
$params = [
'index' => 'newindex',
'type' => 'newtype',
'body' => [
'query' => [
'match' => [
'name' => '強'
]
]
]
];
$repos = $this->client->search($params);
echo "<pre>";
print_r($repos);
echo "<pre>";
}
}
$obj = new Test();
$obj->add_document();
打印結果:
此時,帶強的數據全部出現
6、通過id獲取一條文檔
先看一下name
爲光頭強
的id:
Test.php
:
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
class Test
{
public $client;
public function __construct()
{
$hosts = [
'127.0.0.1:9200',
];
$this->client = ClientBuilder::create()
->setHosts($hosts)
->build();
}
/**
* title:通過id獲取一條文檔
* author:php狗腎
* time:2020/5/26 17:16
*/
public function getOneDocument(){
$params = [
'index' => 'newindex',
'type' => 'newtype',
'id' => '9aw_UHIBXzM3-W004PaS',
];
$response = $this->client->get($params);
echo "<pre>";
print_r($response);
echo "<pre>";
}
}
$obj = new Test();
$obj->add_document();
打印結果:
四、總結
-
以上提供的只是幾種常用的
Elasticsearch-PHP
的方法,具體請參照官網文檔Elasticsearch-PHP -
以上測試,
文檔
、索引
這些均爲術語,對這點還不理解的請參考文章Elasticsearch入門,此瓜保熟,看不懂順着網線砍我!
-
推薦在測試的時候結合着
Elasticsearch-Head
,這樣測起來更清晰直觀,對於Elasticsearch-Head
的使用請參考我的另一篇文章Elasticsearch-Head的安裝和使用,此瓜也保熟,看不懂順着網線砍我!