[LNMP]全文检索方案:分布式Elasticsearch+Mysql

从2011年开始使用coreseek(基于sphinx)作为全文检索的解决方案,对于中小型应用使用还不错,但coreseek的新版一直跳票,而且在实际生产环境的表现不太稳定(单索引文件超过1G),缺少相应的运维监控工具,考虑替换其他的检索引擎。

在Yii 2.x 官方扩展中集成了Elasticsearch,于是开始学习关于Elasticsearch的资料,但由于版本更新太快,大部分资料都已经过时,所以用本文稍作记录,当然与sphinx 对比,Elasticsearch并不是完全对等的替代。

简介


概括来说是支持中文分词(重要)、多种数据源的分布式全文检索引擎,更多详细 看官网:https://www.elastic.co/products/elasticsearch/

版本说明


  • 服务器:Linux(centos 6.x)
  • java环境:JDK 1.8.0
  • elasticsearch:2.3.1
  • elasticsearch-jdbc(数据源插件):2.3.1
  • IK Analysis(中文分词插件):1.9.1

安装


JDK 环境
  1. 检查java版本
    <code>java -version</code>
  2. 安装1.8+JDK
    <code>yum install java-1.8.0</code>
  3. 查询旧版 JDK
    <code>rpm -qa | grep java</code>
  4. 移除旧版JDK,根据查询的旧版本包名
    <code>yum remove java-1.6.0</code>
Elasticsearch

1.添加yum源

创建.repo文件(elasticsearch.repo):
<code>
[elasticsearch-2.x]
name=Elasticsearch repository for 2.x packages
baseurl=https://packages.elastic.co/elasticsearch/2.x/centos
gpgcheck=1
gpgkey=https://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
</code>

导入key:
<code>
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
</code>

2.安装
<code>
yum install elasticsearch
</code>

基本配置


创建相应服务目录&目录权限

<pre>
mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs
chown -R elasticsearch /data/elasticsearch/data
chown -R elasticsearch /data/elasticsearch/logs
</pre>

Elasticsearch 配置

默认路径:/etc/elasticsearch/elasticsearch.yml
<pre>

集群名(同一个集群,名称必须相同)

cluster.name: my-application

服务节点名(每个服务节点不一样)

node.name: node-1

数据存储路径

path.data: /data/elasticsearch/data

服务日志路径

path.logs: /data/elasticsearch/logs

服务ip地址

network.host: 0.0.0.0

服务端口

http.port: 9200
</pre>

Elasticsearch 环境变量配置

默认路径:/etc/sysconfig/elasticsearch
<pre>

设置为可用内存的50%

ES_HEAP_SIZE = 1g

其余参数参考说明进行调优(JVM)

</pre>

中文分词


采用 ik 分词插件进行中文分词,支持web热更新词库,项目github地址:https://github.com/medcl/elasticsearch-analysis-ik

安装

elasticsearch plugin安装方式失败,采用手动安装

1.安装maven工具
<code>wget http://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo</code>

<code>yum install apache-maven</code>
2.下载ik源码包
<code>git clone https://github.com/medcl/elasticsearch-analysis-ik</code>

<code>cd elasticsearch-analysis-ik</code>
3.生成jar插件包
<code>
mvn clean
mvn compile
mvn package
</code>
4.copy到plugins 目录(/usr/share/elasticsearch/plugins)
<code>unzip target/releases/elasticsearch-analysis-ik-*.zip</code>

<code>cp -r target/releases/ /usr/share/elasticsearch/plugins/ik</code>

5.配置词库(ik自带搜狗词库)
配置:/usr/share/elasticsearch/plugins/ik/config/ik/IKAnalyzer.cfg.xml
<pre>
<code>
<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic</entry>
</code>
</pre>

6.重启elasticsearch
<code>service elasticsearch restart</code>

数据源(JDBC importer for Elasticsearch)


插件官网:https://github.com/jprante/elasticsearch-jdbc

1. 安装:(一般 解压即可使用)
强烈建议下载与Elasticsearch 版本对应的源码包:https://github.com/jprante/elasticsearch-jdbc/tree/2.3.1.0

2. mysql数据源配置(默认脚本目录/path/bin/)

全量索引:mysql-es-all.sh

<pre>

!/bin/sh

DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
bin=${DIR}/../bin
lib=${DIR}/../lib

curl -XDELETE '192.168.115.115:9200/myindex'
echo '
{
"type" : "jdbc",
"jdbc" : {
"locale" : "zh_CN",
"statefile" : "statefile.json",
"timezone" : "GMT+8",
"autocommit" : true,

    "elasticsearch" : {
        "cluster" : "my-application",
        "host" : "192.168.115.115",
        "port" : "9300"
    },

    "index" : "myindex",
    "type" : "mytype",

    "url" : "jdbc:mysql://192.168.115.112:3306/search_content",
    "user" : "root",
    "password" : "",
    "sql" : "select cid as _id,title,content from news",
    
   
    "schedule": "0 0/15 0-23 ? * *",
    "metrics" : {
        "enabled" : true,
        "interval" : "5m"
    },

    "index_settings" : {
        "index" : {
            "number_of_shards" : 2,
            "number_of_replicas" : 2
        }
    },
    "type_mapping": {
        "mytype" : {
            "properties" : {
                "title" : {
                    "type" : "string",
                    "store": "no",
                    "term_vector": "with_positions_offsets",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word",
                    "include_in_all": "true"
                },
                "content" : {
                    "type" : "string",
                    "store": "no",
                    "term_vector": "with_positions_offsets",
                    "analyzer": "ik_max_word",
                    "search_analyzer": "ik_max_word",
                    "include_in_all": "true"
                }
            }
        }
    }   
}

}
' | java
-cp "${lib}/*"
-Dlog4j.configurationFile=${bin}/log4j2.xml
org.xbib.tools.Runner
org.xbib.tools.JDBCImporter

</pre>

增量索引:mysql-es-update.sh

通过statefile.json 里面的时间进行查询,需相应的数据表有相应的字段
<pre>
...
"sql" : [
{
"statement" : "select cid as _id,title,content,from news where utime > unix_timestamp(?)",
"parameter" : [ "$metrics.lastexecutionstart" ]
}
],
...
</pre>

3. shell 运行相应的脚本

如果出现运行问题,通过查看/path/bin/logs/jdbc.log 排查

常用组件


head

安装命令:
<code>
/usr/share/elasticsearch/bin/plugin install mobz/elasticsearch-head
</code>
使用:
http://localhost:9200/_plugin/head/

head 插件
license

安装命令:
<code>
/usr/share/elasticsearch/bin/plugin install license
</code>

kibana+marvel 监控台

elasticsearch集群监控平台

1. 安装kibana
1).添加yum源
创建.repo文件(kibana.repo):
<code>
[kibana]
name=Kibana
baseurl=http://packages.elastic.co/kibana/4.5/centos
gpgcheck=1
gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch
enabled=1
</code>

导入key:
<code>
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
</code>

2).安装
<code>
yum install kibana
</code>

2. 安装Marvel
在elasticsearch 安装 Marvel 插件
<code>
/usr/share/elasticsearch/bin/plugin install marvel-agent
</code>

在kibana 安装 Marvel 插件
<code>
/opt/kibana/bin/kibana plugin --install elasticsearch/marvel/latest
</code>

3. 启动kibana
<code>
service elasticseach restart
service kibana restart
</code>

4. 使用
http://localhost:5601/app/marvel

kibana监控台界面

集群配置


服务节点的基本参数,参考 Elasticsearch 配置 进行设置,为了方便,集群机器放在同一网段下(非强制)

默认路径:/etc/elasticsearch/elasticsearch.yml
<pre>

允许多播节点自动发现

discovery.zen.ping.multicast.enabled: true

单播节点ip配置,用于集群组件(可不设)

discovery.zen.ping.unicast.hosts: ["192.168.115.115","192.168.115.116"]
</pre>

注意集群所有服务节点的插件,最好保持一致

与sphinx索引速度对比


[LNMP]全文检索引擎sphinx 与 Elasticsearch 索引速度对比

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,830评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,992评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,875评论 0 331
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,837评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,734评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,091评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,550评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,217评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,368评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,298评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,350评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,027评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,623评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,706评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,940评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,349评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,936评论 2 341

推荐阅读更多精彩内容