search.php,Elasticsearch的php使用简单入门

虚拟机配置

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

e3728ba41787055b3d230b3ff3cefbcd.png

安装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);*/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值