先看官方教程地址如下
快速开始 | Elasticsearch-PHP | Elastic
在 composer.json 文件中引入 elasticsearch-php:官方的"~6.0"会报错,改成7就可以
{
"require": {
"elasticsearch/elasticsearch": "~7.0"
}
}
用 composer 安装客户端:
curl -s http://getcomposer.org/installer | php
php composer.phar install --no-dev
注意PHP版本必须选择7.4版本以上,不然报以下错误
D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es>curl -s http://getcomposer.org/installer | php
Downloading...
Composer (version 2.5.1) successfully installed to: D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es\composer.phar
Use it: php composer.phar
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:
The Windows OneDrive folder is not supported on PHP versions below 7.2.23 and 7.3.10.
Upgrade your PHP (7.3.4) to use this location with Composer.
Some settings on your machine may cause stability issues with Composer.
If you encounter issues, try to change the following:
The Windows OneDrive folder is not supported on PHP versions below 7.2.23 and 7.3.10.
Upgrade your PHP (7.2.1) to use this location with Composer.
超时错误要加入Packagist 镜像使用方法
1、将镜像换成腾讯云
composer config -g repo.packagist composer https://mirrors.cloud.tencent.com/composer/
2、将镜像换成阿里云
composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/
3、 修改当前项目的 composer.json 配置文件,打开命令行窗口(windows用户)或控制台(Linux、Mac 用户),进入你的项目的根目录(也就是 composer.json 文件所在目录),执行如下命令:
composer config repo.packagist composer https://packagist.phpcomposer.com
各种报错处理方法
要忽略 输入:composer install --ignore-platform-reqs
D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es>composer install --ignore-platform-reqs
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires elasticsearch/elasticsearch, it could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see for more details.
- It's a private package and you forgot to add a custom repository to find it
Read for further common problems.
D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es>php composer.phar install --no-dev
No composer.lock file present. Updating dependencies to latest instead of installing from lock file. See https://getcomposer.org/install for more information.
Loading composer repositories with package information
Updating dependencies
Your requirements could not be resolved to an installable set of packages.
Problem 1
- Root composer.json requires elasticsearch/elasticsearch, it could not be found in any version, there may be a typo in the package name.
Potential causes:
- A typo in the package name
- The package is not available in a stable-enough version according to your minimum-stability setting
see for more details.
- It's a private package and you forgot to add a custom repository to find it
Read for further common problems.
Running update with --no-dev does not mean require-dev is ignored, it just means the packages will not be installed. If dev requirements are blocking the update you have to resolve those problems.
D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es>composer update --ignore-platform-reqs
最后下载成功
composer require elasticsearch/elasticsearch="^7.0"
D:\phpStudy\PHPTutorial\WWW\mqtt\fastadmin\es>composer require elasticsearch/elasticsearch="^7.0"
./composer.json has been updated
Running composer update elasticsearch/elasticsearch
Loading composer repositories with package information
Updating dependencies
Lock file operations: 5 installs, 0 updates, 0 removals
- Locking elasticsearch/elasticsearch (v7.17.1)
- Locking ezimuel/guzzlestreams (3.0.1)
- Locking ezimuel/ringphp (1.2.2)
- Locking psr/log (1.1.4)
- Locking react/promise (v2.9.0)
Writing lock file
Installing dependencies from lock file (including require-dev)
Package operations: 5 installs, 0 updates, 0 removals
- Downloading psr/log (1.1.4)
- Downloading react/promise (v2.9.0)
- Downloading ezimuel/guzzlestreams (3.0.1)
- Downloading ezimuel/ringphp (1.2.2)
- Downloading elasticsearch/elasticsearch (v7.17.1)
- Installing psr/log (1.1.4): Extracting archive
- Installing react/promise (v2.9.0): Extracting archive
- Installing ezimuel/guzzlestreams (3.0.1): Extracting archive
- Installing ezimuel/ringphp (1.2.2): Extracting archive
- Installing elasticsearch/elasticsearch (v7.17.1): Extracting archive
1 package suggestions were added by new dependencies, use `composer suggest` to see details.
Generating autoload files
1 package you are using is looking for funding.
Use the `composer fund` command to find out more!
Failed to audit installed packages.
报如下错误 解决办法:将type修改为_doc,默认的数据类型
Fatal error: Uncaught Elasticsearch\Common\Exceptions\BadRequest400Exception: {"error":"no handler found for uri [/my_index/my_type/my_id] and method [POST]"} in D:\phpstudy_pro\WWW\mqtt\fastadmin\es\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 693
$params = [
'index' => 'test',
'type' => '_doc',
'id' => '5' // http://192.168.2.109:9200/test/_doc/5
];
错误 解决access_control_exception:因为 ES 的安装路径中存在空格,修改路径即可
Fatal error: Uncaught Elasticsearch\Common\Exceptions\ServerErrorResponseException: {"error":{"root_cause":[{"type":"access_control_exception","reason":"access denied (\"java.io.FilePermission\" \"D:\\Program%20Files%20(x86)\\elasticsearch-8.6.1\\plugins\\ik\\config\\IKAnalyzer.cfg.xml\" \"read\")"}],"type":"access_control_exception","reason":"access denied (\"java.io.FilePermission\" \"D:\\Program%20Files%20(x86)\\elasticsearch-8.6.1\\plugins\\ik\\config\\IKAnalyzer.cfg.xml\" \"read\")"},"status":500} in D:\phpstudy_pro\WWW\mqtt\fastadmin\es\vendor\elasticsearch\elasticsearch\src\Elasticsearch\Connections\Connection.php on line 726