1.下载jdk并安装(elasticsearch所依赖)
- jdk-8u131-windows-x64(链接地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html)
2.下载elasticsearch并解压
- elasticsearch-5.4.0(链接地址:https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.0.zip)
3.下载elasticsearch可视化的软件,并解压
- kibana-5.4.0-windows-x86(链接地址:https://artifacts.elastic.co/downloads/kibana/kibana-5.4.0-windows-x86.zip)
4.安装composer
- cmd下依次执行。注意:php要加入到环境变量,否则请cd到php目录下,再执行当前命令。详细教程:https://pkg.phpcomposer.com/#how-to-install-composer
php -r “copy(‘https://install.phpcomposer.com/installer‘, ‘composer-setup.php’);”
php composer-setup.php
php -r “unlink(‘composer-setup.php’);”
5.当前项目里安装elasticsearch-php
- 复制composer.json到当前项目,修改内容
内容如下:
{
"repositories": {
"packagist": {
"type": "composer",
"url": "https://packagist.phpcomposer.com"
}
},
"require": {
"elasticsearch/elasticsearch": "~2.0"
}
}
官方教程没有repositories,这样就会产生问题
问题:composer安装报错
[Composer\Downloader\TransportException]
Content-Length mismatch
原因:默认请求的是国外的数据库源
解决:composer.json中添加composer扩展包中国镜像。如上代码操作
cmd下cd到当前目录,执行:php composer.phar install
6.测试Elasticsearch-PHP是否安装成功
当前项目创建test.php文件,代码如下
require 'vendor/autoload.php';
use Elasticsearch\ClientBuilder;
$hosts = array('localhost:9200');
$client = ClientBuilder::create()->setHosts($hosts)->build();
echo '<pre>';
var_dump($client);
访问test文件,若显示object(Elasticsearch\Client)#18 (9) {…}则表示成功