版权声明:本文为博主原创文章,转载敬请作出引用声明方便相互交流学习! https://blog.csdn.net/sweeper_freedoman/article/details/86526613
①检查依赖
本地是Ubuntu18.04环境,简易安装的lampp环境。Elasticsearch-PHP依赖于PHP7,先查看一下lampp自带的PHP版本。
root@ubuntu:~# whereis php
php: /opt/lampp/bin/php
root@ubuntu:~# ls -l /opt/lampp/bin/ | grep php
-rwxr-xr-x 1 root root 240 Oct 13 18:36 gen_php_doc.sh
-rwxr-xr-x 1 root root 107 Oct 13 18:34 html_template_phplibtool
lrwxrwxrwx 1 root root 10 Nov 19 18:34 php -> php-7.2.11
-rwxr-xr-x 1 root root 15922272 Oct 13 18:37 php-7.2.11
lrwxrwxrwx 1 root root 14 Nov 19 18:34 php-cgi -> php-cgi-7.2.11
-rwxr-xr-x 1 root root 15869312 Oct 13 18:37 php-cgi-7.2.11
lrwxrwxrwx 1 root root 17 Nov 19 18:34 php-config -> php-config-7.2.11
-rwxr-xr-x 1 root root 4396 Oct 13 18:27 php-config-7.2.11
-rwxr-xr-x 1 root root 16098880 Oct 13 18:37 phpdbg
lrwxrwxrwx 1 root root 13 Nov 19 18:34 phpize -> phpize-7.2.11
-rwxr-xr-x 1 root root 4547 Oct 13 18:27 phpize-7.2.11
-rwxr-xr-x 1 root root 1941 Oct 13 18:36 phpunit
-rwxr-xr-x 1 root root 158280 Oct 13 18:37 swftophp
root@ubuntu:~# /opt/lampp/bin/php -v
PHP 7.2.11 (cli) (built: Oct 13 2018 09:27:08) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
②配置composer
root@ubuntu:~# cd /opt/lampp/htdocs/
root@ubuntu:/opt/lampp/htdocs# vim composer.json
root@ubuntu:/opt/lampp/htdocs# cat composer.json
{
"require": {
"elasticsearch/elasticsearch": "~6.0"
}
}
③安装composer
root@ubuntu:/opt/lampp/htdocs# cd ../bin/
root@ubuntu:/opt/lampp/bin# wget http://getcomposer.org/installer
root@ubuntu:/opt/lampp/bin# ./php installer
④安装Elasticsearch-PHP
root@ubuntu:/opt/lampp/bin# cd ../htdocs/
root@ubuntu:/opt/lampp/htdocs# ../bin/php ../bin/composer.phar install --no-dev
Do not run Composer as root/super user! See https://getcomposer.org/root for details
Loading composer repositories with package information
Updating dependencies
Package operations: 5 installs, 0 updates, 0 removals
- Installing psr/log (1.1.0): Downloading (100%)
- Installing react/promise (v2.7.1): Downloading (100%)
- Installing guzzlehttp/streams (3.0.0): Downloading (100%)
- Installing guzzlehttp/ringphp (1.1.1): Downloading (100%)
- Installing elasticsearch/elasticsearch (v6.1.0): Downloading (100%)
Writing lock file
Generating autoload files
⑤测试安装
root@ubuntu:/opt/lampp/htdocs# vim es.php
root@ubuntu:/opt/lampp/htdocs# cat es.php
<?php
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$client = ClientBuilder::create()->build();
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
?>
打开浏览器,查看发现index插入成功。
参考:
扫描二维码关注公众号,回复:
5134800 查看本文章
https://www.elastic.co/guide/en/elasticsearch/client/php-api/current/_installation_2.html