一、elasticsearch 安装
1.1安装Java环境
首先检测是否安装java
- java -version
- echo $JAVA_HOME
如果java的版本过低,建议安装高版本,下面安装的是java 1.8
- cd /opt/
- wget --no-cookies --no-check-certificate --header"Cookie: gpw_e24=http%3A%2F%2Fwww.oracle.com%2F; oraclelicense=accept-securebackup-cookie""http://download.oracle.com/otn-pub/java/jdk/8u66-b17/jdk-8u66-linux-x64.tar.gz"
- tar xzf jdk-8u66-linux-x64.tar.gz
- cd /opt/jdk1.8.0_66/
- alternatives --install /usr/bin/java java /opt/jdk1.8.0_66/bin/java2
- alternatives --config java
运行了上面的,会初选一个选择的地方,我的机器显示:
- There are3 programs which provide'java'.
- Selection Command
- -----------------------------------------------
- *1 /opt/jdk1.7.0_71/bin/java
- +2 /opt/jdk1.8.0_45/bin/java
- 3 /opt/jdk1.8.0_51/bin/java
- 4 /opt/jdk1.8.0_66/bin/java
- Enter to keep the current selection[+], or type selection number: 4
我们安装的是jdk1.8.0.66 所以,我选择的是4,这个看具体情况,jdk1.8.0.66 是第几个,就选择那个数字。
- alternatives --install /usr/bin/jar jar /opt/jdk1.8.0_66/bin/jar2
- alternatives --install /usr/bin/javac javac /opt/jdk1.8.0_66/bin/javac2
- alternatives --set jar /opt/jdk1.8.0_66/bin/jar
- alternatives --set javac /opt/jdk1.8.0_66/bin/javac
安装完成,检查版本
- java -version
保存到文件 /etc/environment中,当服务器重启的时候加载:
- vi /etc/profile
- export JAVA_HOME=/opt/jdk1.8.0_66
- export JRE_HOME=/opt/jdk1.8.0_66/jre
- export PATH=$PATH:/opt/jdk1.8.0_66/bin:/opt/jdk1.8.0_66/jre/bin
重启linux
- reboot
查看是否安装成功
- java -version
- echo $JAVA_HOME
2.1安装ElasticSearch
- cd /tools
- wgethttps://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.4.2.zip
- unzip elasticsearch-5.4.2.zip
- mv ./elasticsearch-5.4.2 /usr/local/elasticsearch
- cd /usr/local
- groupadd elasticsearch
- useradd -g elasticsearch elasticsearch
- chown elasticsearch:elasticsearch -R elasticsearch
2.2 设置开机启动: 我是本地,直接把iptables 关掉了
- vim /etc/rc.d/rc.local
- service iptables stop
- su elasticsearch -c"/usr/local/elasticsearch/bin/elasticsearch -d"
一定要注意,elasticSearch不能用root账户启动,elasticSearch不能用root账户启动,elasticSearch不能用root账户启动,重要的说三遍。
我这里用的是我新建的elasticsearch账户开机启动
2.3 配置
vim /usr/local/elasticsearch/config/elasticsearch.yml
修改如下:
cluster.name: elasticsearch-1node.name: node-1
node.master: true
node.data: true
bootstrap.memory_lock: false
bootstrap.system_call_filter: false
network.host: 120.26.223.3
http.port: 9200
http.cors.enabled: true
http.cors.allow-origin: "*"
discovery.zen.ping.unicast.hosts: ["120.26.223.3"]
2.4 集群设置
如果想要建立一个elasticSearch集群,可以按照下面的步骤,非常的简单,首先,想说明的是:对于elasticSearch,他隐藏了分布式的复杂性,分片和复制集,都是他自动完成,你只需要配置好ip就可以了,下面是配置的步骤:
我有两台机器 192.169.0.210 192.168.0.199
我的两台机器都按照上面的步骤配置完成,下面配置集群
首先是192.168.0.210
vim /usr/local/elasticsearch/config/elasticsearch.yml
#找到行 , 修改如下:
- discovery.zen.ping.unicast.hosts:["192.168.0.199"]
上面的ip就是其他的节点的ip,如果我有5台机器,那么,这里需要把其他四台机器的ip写上。
同理,对于其他的节点,需要把其他的节点协商,用逗号隔开
elasticSearch会找到对应的节点,自动分片和做复制集。
3.1 #安装head 插件
Git clone git://github.com/mobz/elasticsearch-head.git
cd elasticsearch-head
npm install
在elasticsearch-head目录下node_modules/grunt下如果没有grunt二进制程序,需要执行
cd elasticsearch-head
npm install grunt --save
3.2 修改配置
修改elasticsearch-head下Gruntfile.js文件,默认监听在你配置的elasticsearch 的IP下9200端口
修改/elasticsearch-head/_site/app.js
this.base_uri = this.config.base_uri || this.prefs.get("app-base_uri") || "http://120.26.223.1:9200";
3.3 启动服务
/usr/local/elasticsearch-head/node_modules/grunt/bin/grunt server
4.1安装中文分词 Ik
4.2 执行命令 git clone https://github.com/medcl/elasticsearch-analysis-ik/releases
4.3 进入下载的目录 mvn package (需要安装maven)
4.4 copytarget/releases/elasticsearch-analysis-ik-5.4.1.zip
到 your-elasticsearch-root/plugins/ik (如果没有ik目录先创建)
参考(https://github.com/medcl/elasticsearch-analysis-ik)
5. Yii2 使用elasticSearch
5.1进入yii2项目目录执行
首先需要安装yii2的elasticSearch插件
https://github.com/yiisoft/yii2-elasticsearch
配置config
return [ //.... 'components' => [ 'elasticsearch' => [ 'class' => 'yii\elasticsearch\Connection', 'nodes' => [ ['http_address' => '127.0.0.1:9200'], // configure more hosts if you have a cluster ], ], ] ];
在YII中使用
model:
namespace app\models;
use yii\elasticsearch\ActiveRecord;
class Customer extends ActiveRecord{
/*@desc @return array 返回elasticsearch 映射关系
*
*/
public static function mapping()
{
return [
static::type() => [
"_all"=>[
"analyzer"=>"ik_max_word",
"search_analyzer"=>"ik_max_word",
"term_vector"=>"no",
"store"=>"false",
],
'properties' => [
'id' => ['type' => 'long'],
'name' => ['type' => 'string'],
'address' => ['type' => 'string'],
'registration_date' => ['type' => 'date'],
]
],
];
}
/*@desc 更新elasticsearch 映射关系
*
*/
public static function updateMapping()
{
$db = static::getDb();
$command = $db->createCommand();
$command->setMapping(static::index(), static::type(), static::mapping());
}
/**
* 创建索引
*/
public static function createIndex()
{
$db = static::getDb();
$command = $db->createCommand();
$command->createIndex(static::index(), [
'settings' => [
'index' => [
'number_of_replicas'=>1,
'number_of_shards'=>5,
]
],
'mappings' => static::mapping(),
//'warmers' => [ /* ... */ ],
//'aliases' => [ /* ... */ ],
//'creation_date' => '...'
]);
}
public static function index(){
return 'customers';
}
public static function type(){
return 'customer';
}
/**
* 删除索引
*/
public static function deleteIndex()
{
$db = static::getDb();
$command = $db->createCommand();
$command->deleteIndex(static::index(), static::type());
}
public function attributes()
{
// '_id' に対するパスマッピングis setup to field 'id'
return ['id', 'name', 'address', 'registration_date'];
}
控制器中的方法:
public function actionCustomer(){
$customer=new Customer();
//创建索引
//$index=$customer::createIndex();
//创建文档
/*$customer->primaryKey = 6;
$customer->id=1;
$customer->name='王麻子';
$customer->address='携程信息技术大厦';
$customer->registration_date="2017-06-04";
//$customer->attributes = ['id'=>1, 'name'=>'张三', 'address'=>"携程信息技术大厦", 'registration_date'=>"2017-06-01"];
$value=$customer->save();*/
//匹配查询(www.pmtown.com)
//$value = $customer::find()->query(["match" => ["name" => "张三"]])->asArray()->all();
/*$aggData = Customer::find()->addAggregation('customers_by_date', 'terms', [
'field' => 'registration_date',
'order' => ['_count' => 'desc'],
'size' => 10, // 登録日の上位 10
])->search();
$customersByDate = ArrayHelper::map($aggData['aggregations']['customers_by_date']['buckets'], 'key', 'doc_count');
var_dump($customersByDate);*/
$value=$customer::find()->offset(0)->limit(2)->asArray()->orderby(" registration_date desc")->all();
var_dump($value);
}
文章来至www.codexueyuan.com的实践