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的安装和使用,此瓜也保熟,看不懂顺着网线砍我!

    在这里插入图片描述

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