Cenots上elasticsearch安装与使用
安装java环境
懒人模式:yum install java*
简单配置下:
vim /etc/profile
尾部加上:
export JAVA_HOME=/usr/lib/jvm/java-1.8.0-openjdk-1.8.0.171-8.b10.el7_5.x86_64 export CLASSPATH=.:$JAVA_HOME/jre/lib/rt.jar:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar export PATH=$PATH:$JAVA_HOME/bin
保存退出后使用命令更新配置
source /etc/profile
2.安装elasticsearch
由于ES不在yum的本地源,所以我们需要添加ES的yum配置。
下载并安装ES的yum公钥
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
配置ES的yum源
vim /etc/yum.repos.d/elasticsearch.repo
输入下面的内容:
[elasticsearch-2.x] name=Elasticsearch repository for 2.x packages baseurl=http://packages.elastic.co/elasticsearch/2.x/centosgpgcheck=1gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearchenabled=1
二、yum安装ES
更新yum的缓存
yum makecache
安装ES
yum install elasticsearch
三、测试ES
1、配置和启动ES服务器进程
/sbin/chkconfig --add elasticsearch
systemctl start elasticsearch
2、查看状态(可能遇到启动失败,如果遇到内存不足情况,弄点swap虚拟内存凑一下)
systemctl status elasticsearch
3、运行测试
curl -X GET localhost:9200
返回的json结果如下:
{
"name": "Mister One",
"cluster_name": "elasticsearch",
"cluster_uuid": "HGBnQTTDTpKlCJnMSXyHBg",
"version": {
"number": "2.4.6",
"build_hash": "5376dca9f70f3abef96a77f4bb22720ace8240fd",
"build_timestamp": "2017-07-18T12:17:44Z",
"build_snapshot": false,
"lucene_version": "5.5.4"
},
"tagline": "You Know, for Search"
}四、通过IP访问ES的配置
四、通过IP访问ES的配置
1、打开/etc/elasticsearch/elasticsearch.yml
vim /etc/elasticsearch/elasticsearch.yml
55行的network.host,把后面改为0.0.0.0或者虚拟机ip地址,这样就可以在window系统用浏览器通过访问虚拟机的ip,正确访问效果如下

安装elasticsearch-head插件,用于监控
cd /usr/share/elasticsearch/bin
执行:
./plugin install mobz/elasticsearch-head
安装完成后,即可通过浏览器访问:
http://192.168.32.128:9200/_plugin/head/

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()->setHosts(['192.168.32.128'])->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();
?>