Elasticsearch-PHP從入門到被鎖裏面

一、關於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的安裝和使用,此瓜也保熟,看不懂順着網線砍我!

    在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章