PHP_elasticsearch搜索引擎的安装与使用

 


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,内容为

 

 

<?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
 

如想了解更多技术架构文章,扫码关注我的个人公众号以及转发分享哈~


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值