虚拟机配置
center6.5
php7.1
java1.8
nginx1.10
虚拟机获取官网的elasticsearch安装包
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.0.rpm
tar zxvf elasticsearch-6.2.0.tar.gz
创建elasticsearch的用户和用户组,elasticsearch不能使用root启动。
groupadd esuser
useradd esuser -g esuser -p 123456
更改文件的拥有者和logs文件夹的777权限
chown -R esuser:esuser /elasticsearch-6.5.2
cd elasticsearch-6.5.2
chmod -R 777 logs
copy elasticsearch-6.5.2文件夹到esuser用户目录(/home/esuser)
mv elasticsearch-6.5.2 /home/esuser/elasticsearch
启动elasticsearch
su esuser
cd elasticsearch-6.5.2
./bin/elasticsearch
启动报错
OpenJDK 64-Bit Server VM warning: If the number of processors is expected to increase from one, then you should configure the number of parallel GC threads appropriately using -XX:ParallelGCThreads=N
解决方法:虚拟机的处理器的处理器核心数量改成2以上就能够正常运行。
unable to install syscall filter: java.lang.UnsupportedOperationException: seccomp unavailable: CONFIG_SECCOMP not compiled into kernel, CONFIG_SECCOMP and CONFIG_SECCOMP_FILTER are needed...
解决方法:修改config/elasticsearch.yml
bootstrap.memory_lock: false
启动之后,验证下。
curl localhost:9200
返回
{
"name" : "52M6-Dw",
"cluster_name" : "elasticsearch",
"cluster_uuid" : "tq4k43iFSy6CrkEXKYF8fQ",
"version" : {
"number" : "6.2.0",
"build_hash" : "37cdac1",
"build_date" : "2018-02-01T17:31:12.527918Z",
"build_snapshot" : false,
"lucene_version" : "7.2.1",
"minimum_wire_compatibility_version" : "5.6.0",
"minimum_index_compatibility_version" : "5.0.0"
},
"tagline" : "You Know, for Search"
}
或者在虚拟机的浏览器打开
http://127.0.0.1:9200
安装composer
curl -sS https://getcomposer.org/installer | php
mv composer.phar /usr/local/bin/composer
Packagist 镜像使用方法
composer config -g repo.packagist composer https://packagist.phpcomposer.com
安装elasticsearch-php客户端(https://github.com/elastic/elasticsearch-php)
composer require elasticsearch/elasticsearch
php测试代码
use Elasticsearch\ClientBuilder;
require './vendor/autoload.php';
$client = ClientBuilder::create()->build();
echo '
';
//添加索引
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id',
'body' => ['testField' => 'abc']
];
$response = $client->index($params);
print_r($response);
echo "
";
//获取索引
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
$response = $client->get($params);
print_r($response);
echo "
";
//搜索
$params = [
'index' => 'my_index',
'type' => 'my_type',
'body' => [
'query' => [
'match' => [
'testField' => 'abc'
]
]
]
];
$response = $client->search($params);
print_r($response);
/*
删除
$params = [
'index' => 'my_index',
'type' => 'my_type',
'id' => 'my_id'
];
$response = $client->delete($params);
print_r($response);*/