Elasticsearch-PHP 学习了解(一)

Elasticsearch-PHP 学习了解(一)

  • Elasticsearch—PHP 的github地址

首先是安装,我的环境是在windows10上测试,PHP的版本是7.0;所以我直接安装的的Elasticsearch—PHP也是大于等于6.0的。PHP的框架也是laravel5.5。
直接在我们的composer.json中添加:

        {
   
            "require": {
   
                "elasticsearch/elasticsearch": "~6.0"
            }
        }

这个在说明文档都有,然后运行php composer.phar update | install。坐享其成就可以了。最后得到的就是下面的这个样子:

Elasticsearch-PHP 学习了解(一)_第1张图片

以上是属于安装PHP的操作API,我们还需要下载安装Elasticsearch服务端。

我在windows上测试,Elasticsearch-windows

前提是本地安装了java环境,java的安装就不在赘述了。Elasticsearch就是一个zip包,下载完成之后,在你喜欢的地方创建一个Elasticsearch文件,解压。
可以在你的环境变量里加入path = F:\Elasticsearch\elasticsearch-6.7.0\bin。然后打开cmd,输入elasticsearch.bat回车。打开浏览器,输入
localhost:9200。可以看到浏览器的输出:

{
   
  "name" : "OgXEkyd",
  "cluster_name" : "elasticsearch",
  "cluster_uuid" : "gNbzzuQ1Qd-X9zQidpkpAw",
  "version" : {
   
    "number" : "6.7.0",
    "build_flavor" : "default",
    "build_type" : "zip",
    "build_hash" : "8453f77",
    "build_date" : "2019-03-21T15:32:29.844721Z",
    "build_snapshot" : false,
    "lucene_version" : "7.7.0",
    "minimum_wire_compatibility_version" : "5.6.0",
    "minimum_index_compatibility_version" : "5.0.0"
  },
  "tagline" : "You Know, for Search"
}

代表启动elasticsearch成功。


PHP操作elasticsearch
  • 基本操作

创建一个ElasticsearchController.php文件,引入use Elasticsearch\ClientBuilder;


namespace App\Http\Controllers\Api;

use Elasticsearch\ClientBuilder;

class ElasticsearchController
{
   
    private $client = null;
    
    public function __construct() 
    {
   
        $this->client = ClientBuilder::create()->setHosts(['localhost:9200'])->build();
    }
}

redis mysql psql一样,想要操作它,第一步是连接它。打印连接信息:

Client {
   #472 ▼
  +transport: Transport {
   #470 ▶}
  #params: null
  #indices: IndicesNamespace {#473 ▶}
  #cluster: ClusterNamespace {#474 ▶}
  #nodes: NodesNamespace {#475 ▶}
  #snapshot: SnapshotNamespace {#476 ▶}
  #cat: CatNamespace {#477 ▶}
  #ingest: IngestNamespace {#478 ▶}
  #tasks: TasksNamespace {#479 ▶}
  #remote: RemoteNamespace {#480 ▶}
  #endpoints: Closure {#471 ▶}
  #registeredNamespaces: []
}

1.) 创建一个文档

    public function createDocument()
    {
   
        $params = [
            'index' => 'laravel',//数据库
            'type' => 'users',//数据表
            'id' => 'id',//主键
            'body' => ['testField' => 'abc']//每条记录
        ];

        $response = $this->client->index($params);
        dd($response);
    }

在这里,param这个数组里面,我们有四组key=>value

  • index 这个就好像我们mysql里面的数据库
  • type 这个就代表数据库表
  • id 这个就代表数据库表的主键
  • body 这个就代表数据库表里面的每一条记录

以上大概是那个意思。我觉得这样类比可能好理解一点。可能会有不对或比较片面

结果:

array:8 ["_index" => "laravel"
  "_type" => "users"
  "_id" => "id"
  "_version" => 1
  "result" => "created"
  "_shards" => array:3 ["total" => 2
    "successful" => 1
    "failed" => 0
  ]
  "_seq_no" => 0
  "_primary_term" => 1
]

2.) 获取一个文档

public function getDocument()
    {
   
        $param = [
            'index' => 'laravel',
            'type'  => 'users',
            'id'    => 'id'
        ];

        $responce = $this->client->get($param);

        dd($responce);
    }

结果:

array:8 ["_index" => "laravel"
  "_type" => "users"
  "_id" => "id"
  "_version" => 1
  "_seq_no" => 0
  "_primary_term" => 1
  "found" => true
  "_source" => array:1 ["testField" => "abc"
  ]
]

3.) 搜索一个文档

 public function searchDocument()
    {
   
        $param = [
            'index'  => 'laravel',
            'type'   => 'users',
            'body'   => [
                'query'  => [
                    'match' => [
                        'testField' => 'abc'
                    ]
                ]
            ]
        ];

        $responce = $this->client->search($param);

        dd($responce);
    }

结果:

array:4 ["took" => 8
  "timed_out" => false
  "_shards" => array:4 ["total" => 5
    "successful" => 5
    "skipped" => 0
    "failed" => 0
  ]
  "hits" => array:3 ["total" => 1
    "max_score" => 0.2876821
    "hits" => array:1 [0 => array:5 ["_index" => "laravel"
        "_type" => "users"
        "_id" => "id"
        "_score" => 0.2876821
        "_source" => array:1 ["testField" => "abc"
        ]
      ]
    ]
  ]
]

4.) 删除一个文档

public function deleteDocument()
    {
   
        $param = [
            'index' => 'laravel',
            'type'  => 'users',
            'id'    => 'id',
        ];

        $responce = $this->client->delete($param);

        dd(

你可能感兴趣的:(PHP,php,elasticsearch)