虽然Elasticsearch是Java开发的,但是官方推出一个PHP 客户端:Elasticsearch-PHP
,让我这些phper
使用起来非常万多火
(wonderful)。
1、首先需要安装Elasticsearch
传送门:Elasticsearch安装
安装完成后启动Elasticsearch
,不然即使安装完Elasticsearch-PHP
也不能用。
2、安装Elasticsearch-PHP
要求
我php环境是
7.0
,然后开启的是php_curl扩展
然后我的JSON扩展为1.4.0
:
3、composer下载Elasticsearch-PHP
composer require elasticsearch/elasticsearch
1、测试是否成功连接
Test.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
:
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
:
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
:
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 ""
;
print_r($response);
echo ""
;
}
}
$obj = new Test();
$obj->add_document();
打印结果:
Test.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 ""
;
print_r($repos);
echo ""
;
}
}
$obj = new Test();
$obj->add_document();
此时,带强的数据全部出现
Test.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 ""
;
print_r($response);
echo ""
;
}
}
$obj = new Test();
$obj->add_document();