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。坐享其成就可以了。最后得到的就是下面的这个样子:

以上是属于安装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;
<?php
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($responce);
    }
结果:
array:8 [▼
  "_index"
 
                   
                   
                   
                   本文是关于Elasticsearch-PHP的学习介绍,包括PHP如何操作Elasticsearch,基本操作如创建、获取、搜索和删除文档,以及常用配置如连接地址、端口、HTTP认证、重连次数和日志设置等。还提到了连接池、选择器、序列化器的配置,以及根据需求定制异常处理和查询参数。
本文是关于Elasticsearch-PHP的学习介绍,包括PHP如何操作Elasticsearch,基本操作如创建、获取、搜索和删除文档,以及常用配置如连接地址、端口、HTTP认证、重连次数和日志设置等。还提到了连接池、选择器、序列化器的配置,以及根据需求定制异常处理和查询参数。
           最低0.47元/天 解锁文章
最低0.47元/天 解锁文章
                           
                       
       
           
                 
                 
                 
                 
                 
                
               
                 
                 
                 
                 
                
               
                 
                 扫一扫
扫一扫
                     
              
             
                   1715
					1715
					
 被折叠的  条评论
		 为什么被折叠?
被折叠的  条评论
		 为什么被折叠?
		 
		  到【灌水乐园】发言
到【灌水乐园】发言                                
		 
		 
    
   
    
   
             
            


 
            