您的位置:首页 > 编程语言 > PHP开发

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,内容为
<?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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: