PHP_elasticsearch搜索引擎的安装与使用
2017-02-07 14:48
375 查看
1、下载解压
https://www.elastic.co/downloads/elasticsearch
2、启动,要用非root用户,新建一个用户组elasticsearch,再新建一个用户mraz,将目录elasticsearch-2.3.0的归属用户组和用户更改下
cd elasticsearch-2.3.0
chmod -R 777 .
bin/elasticsearch -d
ps -ef |grep java查看是否启动
访问http://IP:9200/,如果公网IP访问不了,则
修改配置文件 config/elasticsearch.yml
network.host: 0.0.0.0
3、安装elasticsearch-head插件,用于监控
3.1、es5版本(因为兼容问题,单独安装)
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
grunt server(如果没有grunt,执行npm install -g grunt-cli安装)
open http://localhost:9100/
3.2、es5版本以下
直接在elasticsearch-2.3.0/bin目录下执行
./plugin -install mobz/elasticsearch-head
访问http://172.16.36.130:9200/_plugin/head/成功
4、新建一个test目录,在test目录下,安装Composer,用Composer来生成php版的elasticsearch框架,(https://getcomposer.org/download/)
执行以下三行命令
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo
PHP_EOL;"
php composer-setup.php
5、在test目录里新建composer.json,内容为
{
"require":{
"elasticsearch/elasticsearch" : "~2.0”
}
}
6、在test目录下执行命令,生成php版的elasticsearch框架
php composer.phar install --no-dev
7、在test目录新建elasticsearch_test.php,内容为
8、建立索引成功,可以看到“create index done!”
9、查询成功,可以看到返回的结果数组
10、更多elasticsearch2.0有关php的操作请看官方文档
https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_quickstart.html
https://www.elastic.co/downloads/elasticsearch
2、启动,要用非root用户,新建一个用户组elasticsearch,再新建一个用户mraz,将目录elasticsearch-2.3.0的归属用户组和用户更改下
cd elasticsearch-2.3.0
chmod -R 777 .
bin/elasticsearch -d
ps -ef |grep java查看是否启动
访问http://IP:9200/,如果公网IP访问不了,则
修改配置文件 config/elasticsearch.yml
network.host: 0.0.0.0
3、安装elasticsearch-head插件,用于监控
3.1、es5版本(因为兼容问题,单独安装)
git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
grunt server(如果没有grunt,执行npm install -g grunt-cli安装)
open http://localhost:9100/
3.2、es5版本以下
直接在elasticsearch-2.3.0/bin目录下执行
./plugin -install mobz/elasticsearch-head
访问http://172.16.36.130:9200/_plugin/head/成功
4、新建一个test目录,在test目录下,安装Composer,用Composer来生成php版的elasticsearch框架,(https://getcomposer.org/download/)
执行以下三行命令
php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
php -r "if (hash_file('SHA384', 'composer-setup.php') === '55d6ead61b29c7bdee5cccfb50076874187bd9f21f65d8991d46ec5cc90518f447387fb9f76ebae1fbbacf329e583e30') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo
PHP_EOL;"
php composer-setup.php
5、在test目录里新建composer.json,内容为
{
"require":{
"elasticsearch/elasticsearch" : "~2.0”
}
}
6、在test目录下执行命令,生成php版的elasticsearch框架
php composer.phar install --no-dev
7、在test目录新建elasticsearch_test.php,内容为
<?php require_once('vendor/autoload.php'); use Elasticsearch\ClientBuilder; function get_conn(){ $host = 'localhost'; $dbname = 'mraz'; $user = 'root'; $passwd = '111111'; $conn = new PDO("mysql:dbname=$dbname;host=$host",$user,$passwd); return $conn; } function create_index(){ //Elastic search php client $client = Elasticsearch\ClientBuilder::create()->build(); $sql = "SELECT * FROM emp"; $conn = get_conn(); $stmt = $conn->query($sql); $rtn = $stmt->fetchAll(); //delete index which already created $params = array(); $params['index'] = 'emp_index'; $client->indices()->delete($params); //create index on log_date,src_ip,dest_ip $rtnCount = count($rtn); for($i=0;$i<$rtnCount;$i++){ $params = array(); $params['body'] = array( 'id' => $rtn[$i]['id'], 'fdName' => $rtn[$i]['fdName'], 'fdAge' => $rtn[$i]['fdAge'], 'fdStatus' => $rtn[$i]['fdStatus'] ); $params['index'] = 'emp_index'; $params['type'] = 'emp_type'; //Document will be indexed to log_index/log_type/autogenerate_id $client->index($params); } echo 'create index done!'; } function search(){ //Elastic search php client $client = Elasticsearch\ClientBuilder::create()->build(); $params = array(); $params['index'] = 'emp_index'; $params['type'] = 'emp_type'; $params['body']['query']['match']['fdStatus'] = '1'; $params['body']['sort'] = array('fdAge'=>array('order'=>'desc')); $params['size'] = 3; $params['from'] = 1; $rtn = $client->search($params); var_dump($rtn); } set_time_limit(0); // create_index(); search(); ?>
8、建立索引成功,可以看到“create index done!”
9、查询成功,可以看到返回的结果数组
10、更多elasticsearch2.0有关php的操作请看官方文档
https://www.elastic.co/guide/en/elasticsearch/client/php-api/2.0/_quickstart.html
相关文章推荐
- PHP扩展之针对搜索引擎的扩展(二)—— Sphinx简介、安装及使用
- elasticsearch在ubuntu16下的安装及php中的使用
- 二十四、ES(elasticsearch)搜索引擎安装和使用
- php elasticsearch安装、插件使用及与Yii2框架集成
- ES(elasticsearch)搜索引擎安装和使用
- windows下elasticSearch以及elasticSearch-php安装及使用
- PHP 开发环境的选择、建立及使用(4)--Apache 的安装和配置
- WINDOWS 2000下使用ISAPI方式安装PHP
- memcached安装+php使用手记
- 在CentOs 5.1中使用rpm安装NGINX+php+mysql(一)
- 开源搜索引擎Nutch 0.9的安装使用
- 如何使用动态共享对象的模式来安装PHP
- PHP网页服务实现注册和商店NPC管理(安装+搭建+使用+问题处理)
- PHP文档利器 PEAR之PHPDOC安装使用笔记
- PHP Pear 安装及使用
- PHP Pear 安装及使用
- 在CentOs 5.1中使用rpm安装NGINX+php+mysql
- WINDOWS 2000下使用ISAPI方式安装PHP
- memcached安装+php使用手记
- 在CentOs 5.1中使用rpm安装NGINX+php+mysql(二)