当前位置:首页 > 技术文档 > 正文内容

Cenots上elasticsearch安装与使用

admin8年前 (2018-06-12)技术文档1783
  1. 安装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,正确访问效果如下

 

image.png

安装elasticsearch-head插件,用于监控

 cd /usr/share/elasticsearch/bin

执行:

 ./plugin install mobz/elasticsearch-head

安装完成后,即可通过浏览器访问:

http://192.168.32.128:9200/_plugin/head/

image.png

    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();
?>


分享到:

扫描二维码推送至手机访问。

版权声明:本文由云河空间发布,如需转载请注明出处。

本文链接:https://yuyunhe.cn/index.php/post/264.html

分享给朋友:

“Cenots上elasticsearch安装与使用” 的相关文章

appcan 云打包的方法

appcan 云打包的方法...

php 获取页面内容

function get_contents($url){     if(function_exists('file_get_contents')){       &nbs...

wamp下虚拟主机的配置

1  编辑httpd.conf,查找Include conf/extra/httpd-vhosts.conf,把前面注释符号“#”删掉。 2  编辑httpd-vhosts.conf,我把WAMPServer安装在D:/wamp,所以我这里的路径是D:\wamp\Apache2\...

php二维数组转换为一维数组的几种方法

在开发过程中,我们经常需要将二维数组转为一维数组,个人总结了2种方法,分享给大家如何将下面的二维数组转为一维数组。复制代码 代码如下:$msg = array(  array(    'id'=>'45',    'name'=>'...

浅谈 PHP 与手机 APP 开发(API 接口开发)

文章转载自:http://www.thinkphp.cn/topic/5023.html这个帖子写给不太了解PHP与API开发的人一、先简单回答两个问题:1、PHP 可以开发客户端?答:不可以,因为PHP是脚本语言,是负责完成 B/S架构 或 C/S架构 的S部分,即:服务端的开发。(别去纠结 GT...

php取整函数ceil,floor,round,intval函数的区别

开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下:1、ceil — 进一法取整说明float ceil ( float $value )返回不小于 ...     ...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。