Elasticsearch-PHP(一) - 下载安装

版权声明:转载需附上本文地址 https://blog.csdn.net/weikaixxxxxx/article/details/86483408

es同步mysql是近实时的,最快每分钟同步一次。当项目的需求是实时更新的,你就得对索引进行操作了。

Elasticsearch-php 的安装需要满足以下 4 个需求:

  • PHP 7.0.0 或更高版本
  • Composer
  • ext-curl:PHP 的 Libcurl 扩展
  • 原生 JSON 扩展 (ext-json) 1.3.7或更高版本

其余的依赖会由 Composer 自动安装。

es版本对应的es-php版本
在这里插入图片描述
查看php扩展

php -m |less

esc+:q 退出
一般安装完都符合的吧。不然重新安装吧。选择第二种,编译安装

随便建一个目录作为下载目录

# cd /usr/local/
# mkdir esphp
# cd esphp/
# vi composer.json

内容

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

下载

# composer install --no-dev

--no-dev :防止 Composer 安装各种测试依赖包和开发依赖包。对于普通用户没有必要安装测试包。

测试是否安装成功

# vi test.php

代码

<?php
require 'vendor/autoload.php';

$hosts = [
    '192.168.247.140:9200' // ip和端口
];

$client = Elasticsearch\ClientBuilder::create()
                                ->setHosts($hosts)
                                ->build();

$params = [
    'index' => 'test',
    'type' => 'test',
    'id' => '5' // http://192.168.247.140:9200/test/test/5
];


$response = $client->get($params);
var_dump($response);

$hosts中其它的配置方式

$hosts = [
    '192.168.1.1:9200',         // IP + Port
    '192.168.1.2',              // Just IP
    'mydomain.server.com:9201', // Domain + Port
    'mydomain2.server.com',     // Just Domain
    'https://localhost',        // SSL to localhost
    'https://192.168.1.3:9200'  // SSL to IP + Port
];

运行

# php test.php 

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weikaixxxxxx/article/details/86483408
今日推荐