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

從2011年開(kāi)始使用coreseek(基于sphinx)作為全文檢索的解決方案,對(duì)于中小型應(yīng)用使用還不錯(cuò),但coreseek的新版一直跳票,而且在實(shí)際生產(chǎn)環(huán)境的表現(xiàn)不太穩(wěn)定(單索引文件超過(guò)1G),缺少相應(yīng)的運(yùn)維監(jiān)控工具,考慮替換其他的檢索引擎。

在Yii 2.x 官方擴(kuò)展中集成了Elasticsearch,于是開(kāi)始學(xué)習(xí)關(guān)于Elasticsearch的資料,但由于版本更新太快,大部分資料都已經(jīng)過(guò)時(shí),所以用本文稍作記錄,當(dāng)然與sphinx 對(duì)比,Elasticsearch并不是完全對(duì)等的替代。

簡(jiǎn)介


概括來(lái)說(shuō)是支持中文分詞(重要)、多種數(shù)據(jù)源的分布式全文檢索引擎,更多詳細(xì) 看官網(wǎng):https://www.elastic.co/products/elasticsearch/

版本說(shuō)明


  • 服務(wù)器:Linux(centos 6.x)
  • java環(huán)境:JDK 1.8.0
  • elasticsearch:2.3.1
  • elasticsearch-jdbc(數(shù)據(jù)源插件):2.3.1
  • IK Analysis(中文分詞插件):1.9.1

安裝


JDK 環(huán)境
  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,根據(jù)查詢的舊版本包名
    <code>yum remove java-1.6.0</code>
Elasticsearch

1.添加yum源

創(chuàng)建.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>

導(dǎo)入key:
<code>
rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
</code>

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

基本配置


創(chuàng)建相應(yīng)服務(wù)目錄&目錄權(quán)限

<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 配置

默認(rèn)路徑:/etc/elasticsearch/elasticsearch.yml
<pre>

集群名(同一個(gè)集群,名稱必須相同)

cluster.name: my-application

服務(wù)節(jié)點(diǎn)名(每個(gè)服務(wù)節(jié)點(diǎn)不一樣)

node.name: node-1

數(shù)據(jù)存儲(chǔ)路徑

path.data: /data/elasticsearch/data

服務(wù)日志路徑

path.logs: /data/elasticsearch/logs

服務(wù)ip地址

network.host: 0.0.0.0

服務(wù)端口

http.port: 9200
</pre>

Elasticsearch 環(huán)境變量配置

默認(rèn)路徑:/etc/sysconfig/elasticsearch
<pre>

設(shè)置為可用內(nèi)存的50%

ES_HEAP_SIZE = 1g

其余參數(shù)參考說(shuō)明進(jìn)行調(diào)優(yōu)(JVM)

</pre>

中文分詞


采用 ik 分詞插件進(jìn)行中文分詞,支持web熱更新詞庫(kù),項(xiàng)目github地址:https://github.com/medcl/elasticsearch-analysis-ik

安裝

elasticsearch plugin安裝方式失敗,采用手動(dòng)安裝

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.配置詞庫(kù)(ik自帶搜狗詞庫(kù))
配置:/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>

數(shù)據(jù)源(JDBC importer for Elasticsearch)


插件官網(wǎng):https://github.com/jprante/elasticsearch-jdbc

1. 安裝:(一般 解壓即可使用)
強(qiáng)烈建議下載與Elasticsearch 版本對(duì)應(yīng)的源碼包:https://github.com/jprante/elasticsearch-jdbc/tree/2.3.1.0

2. mysql數(shù)據(jù)源配置(默認(rèn)腳本目錄/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

通過(guò)statefile.json 里面的時(shí)間進(jìn)行查詢,需相應(yīng)的數(shù)據(jù)表有相應(yīng)的字段
<pre>
...
"sql" : [
{
"statement" : "select cid as _id,title,content,from news where utime > unix_timestamp(?)",
"parameter" : [ "$metrics.lastexecutionstart" ]
}
],
...
</pre>

3. shell 運(yùn)行相應(yīng)的腳本

如果出現(xiàn)運(yùn)行問(wèn)題,通過(guò)查看/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 監(jiān)控臺(tái)

elasticsearch集群監(jiān)控平臺(tái)

1. 安裝kibana
1).添加yum源
創(chuàng)建.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>

導(dǎo)入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. 啟動(dòng)kibana
<code>
service elasticseach restart
service kibana restart
</code>

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

kibana監(jiān)控臺(tái)界面

集群配置


服務(wù)節(jié)點(diǎn)的基本參數(shù),參考 Elasticsearch 配置 進(jìn)行設(shè)置,為了方便,集群機(jī)器放在同一網(wǎng)段下(非強(qiáng)制)

默認(rèn)路徑:/etc/elasticsearch/elasticsearch.yml
<pre>

允許多播節(jié)點(diǎn)自動(dòng)發(fā)現(xiàn)

discovery.zen.ping.multicast.enabled: true

單播節(jié)點(diǎn)ip配置,用于集群組件(可不設(shè))

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

注意集群所有服務(wù)節(jié)點(diǎn)的插件,最好保持一致

與sphinx索引速度對(duì)比


[LNMP]全文檢索引擎sphinx 與 Elasticsearch 索引速度對(duì)比

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容