Press "Enter" to skip to content

elasticsearch ik 分词+go-mysql-elasticsearch 数据库同步

安装环境以ubuntu16为参考:

1、安装java环境

sudo add-apt-repository ppa:webupd8team/java
sudo apt-get update
sudo apt-get install oracle-java8-installer
sudo apt-get install oracle-java8-set-default

java -version 查看java是否安装成功

2、安装elasticsearch

推荐安装deb格式的,这里是5.3的版本,如果安装其他版本请切换
https://www.elastic.co/guide/en/elasticsearch/reference/5.3/deb.html

wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.3.2.deb
sudo dpkg -i elasticsearch-5.3.2.deb
日志:/var/log/elasticsearch/
配制 elasticsearch.yml: /etc/elasticsearch/elasticsearch.yml
默认配制:/etc/default/elasticsearch
data:/var/lib/elasticsearch
插件:/usr/share/elasticsearch/plugins

更换端口:vi  /etc/elasticsearch/elasticsearch.yml
http.port: 9200
启动:
sudo -i service elasticsearch start
停止
sudo -i service elasticsearch stop

设置随机启动

sudo update-rc.d elasticsearch defaults 95 10
sudo -i service elasticsearch start
sudo -i service elasticsearch stop
sudo -i service elasticsearch restart

3、安装elk  kibana

下载地址:https://www.elastic.co/guide/en/kibana/5.3/deb.html

wget https://artifacts.elastic.co/downloads/kibana/kibana-5.3.2-amd64.deb
sudo dpkg -i kibana-5.3.2-amd64.deb
启动
sudo -i service kibana start
停止
sudo -i service kibana stop

设置随系统启动

sudo update-rc.d kibana defaults 95 10
sudo -i service kibana start
sudo -i service kibana stop
sudo -i service kibana restart
kibana常用文件备注;
文件:/usr/share/kibana
bin:/usr/share/kibana/bin
kibana.yml /etc/kibana/kibana.yml
data: /var/lib/kibana
插件:/usr/share/kibana/plugins

设置端口:

vi /etc/kibana/kibana.yml
更改:server.host: "0.0.0.0"

访问:ip:5601

4、安装ik中文分词插件

https://github.com/medcl/elasticsearch-analysis-ik
提示:安装的时候设置下mvn的源为阿里云
一定要注意ik的版本要和es的版本一致

5、数据库同步到es工具

https://github.com/siddontang/go-mysql-elasticsearch

使用过程发现,如果更改数据库的表结构,同步会失败,真能重新建mapping

6、es php 客户端

推荐官方的的sdk:https://github.com/elastic/elasticsearch-php
相比 https://github.com/ruflin/Elastica
elasticsearch-php 比Elastica在插件接口方面,更好。因为es更新一致很快。
下面是 elasticsearch 的一个例子

$clientBuilder = new \Elasticsearch\ClientBuilder();

$client = $clientBuilder::create()
    ->setHosts(['localhost:9000'])
    ->build();

$query = [
    'match' => [
        'content' => '测试搜索内容',
    ],
];

$sort = [
    'id'=>['order'=>'desc'],
];

$params = [
    'index' => 'databasename', //数据库
    'type' => 'talbe_name',  //表
    'from' => 0,   //起始值
    'size' => 10,  //每页数量
    'body' => [
        'query' => $query,
        'sort' => $sort,
    ],

];

$response = $client->search($params);