Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習 Linux

Elasticsearch 快速上手:安裝、中文分詞與資料同步

官方網站:Elasticsearch

一、特性

  • 支援中文分詞
  • 支援多種資料源的全文檢索引擎
  • 分散式架構
  • 基於 lucene 的開源搜尋引擎
  • 提供 Restful API

二、常用資源

三、安裝

環境:CentOS 6.x、JDK 1.8.0、Elasticsearch 2.3.1、elasticsearch-jdbc 2.3.1、IK Analysis 1.9.1。

1. 安裝 Java
yum install java-1.8.0

2. 安裝 Elasticsearch
cat >> /etc/yum.repos.d/elasticsearch.repo << EOF
[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
EOF

rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch
yum install elasticsearch

3. 建立使用者。用 root 啟動會直接報錯:

Exception in thread "main" java.lang.RuntimeException: don't run elasticsearch as root.

這是出於安全考量:Elasticsearch 能接收並執行使用者提供的腳本,因此不應以 root 運行。

groupadd elsearch
useradd elsearch -g elsearch -p elasticsearch

4. 建立資料與日誌目錄並轉移擁有權:

mkdir -p /data/elasticsearch/data
mkdir -p /data/elasticsearch/logs
chown -R elsearch:elsearch /data/elasticsearch/data
chown -R elsearch:elsearch /data/elasticsearch/logs

5. 編輯 /etc/elasticsearch/elasticsearch.yml:

cluster.name: my-application # 同一叢集的名稱必須相同
node.name: node-1 # 每個節點名稱需不同
path.data: /data/elasticsearch/data
path.logs: /data/elasticsearch/logs
network.host: 0.0.0.0
http.port: 9200

四、安裝 IK 分詞

1. 安裝 maven
wget https://repos.fedorapeople.org/repos/dchen/apache-maven/epel-apache-maven.repo -O /etc/yum.repos.d/epel-apache-maven.repo
yum install apache-maven

2. 下載 ik 源碼
git clone https://github.com/medcl/elasticsearch-analysis-ik

3. 編譯並安裝
mvn clean
mvn compile
mvn package
unzip target/releases/elasticsearch-analysis-ik-*.zip
cp -r target/releases/ /usr/share/elasticsearch/plugins/ik

4. 設定詞庫(ik 內建搜狗詞庫),編輯 IKAnalyzer.cfg.xml:

<entry key="ext_dict">custom/mydict.dic;custom/single_word_low_freq.dic;custom/sougou.dic</entry>

把 jar 包放到 plugins/analysis-ik、ik 目錄放到 config 下,並在 elasticsearch.yml 加上:

index.analysis.analyzer.ik.type : "ik"

重啟服務後即可建立索引並寫入資料。

五、elasticsearch-jdbc 資料同步

使用 feeder 方式:

wget https://xbib.org/repository/org/xbib/elasticsearch/importer/elasticsearch-jdbc/2.3.1.0/elasticsearch-jdbc-2.3.1.0-dist.zip
unzip elasticsearch-jdbc-2.3.1.0-dist.zip

撰寫匯入腳本 import.sh:

export JDBC_IMPORTER_HOME=/elasticsearch-jdbc-2.3.1.0
bin=$JDBC_IMPORTER_HOME/bin
lib=$JDBC_IMPORTER_HOME/lib
echo '{ "type" : "jdbc", "jdbc": { "url":"jdbc:mysql://127.0.0.1:3306/dbtest", "user":"root", "password":"123456", "sql":"select * from test_tb", "index" : "customer", "type" : "external" }}' | java \
-cp "${lib}/*" \
-Dlog4j.configurationFile=${bin}/log4j2.xml \
org.xbib.tools.Runner \
org.xbib.tools.JDBCImporter

測試:

curl 'localhost:9200/customer/external/_search?pretty&q=*'

使用 river 方式(1.x 版本):

./bin/plugin --install jdbc --url https://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-river-jdbc/1.4.0.6/elasticsearch-river-jdbc-1.4.0.6-plugin.zip
cp mysql-connector-java-5.1.33-bin.jar $ES_HOME/plugins/jdbc/
chmod 644 $ES_HOME/plugins/jdbc/*
./bin/elasticsearch

設定 JDBC 插件:

curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
"type" : "jdbc",
"jdbc" : {
"driver" : "com.mysql.jdbc.Driver",
"url" : "jdbc:mysql://localhost:3306/test",
"user" : "root",
"password" : "123456",
"sql" : "select * from test.student;",
"interval" : "30",
"index" : "test",
"type" : "student"
} }'

interval 是增量同步的間隔(秒),適合需要持續把資料庫變更帶入搜尋引擎的場景。若要串接多個資料源,可把 jdbc 欄位改成陣列,放入多組定義。

確認資料是否已同步:

curl -XGET 'localhost:9200/test/student/_search?pretty&q=*'

停止 river:

curl -XDELETE 'localhost:9200/_river/my_jdbc_river/'

延伸閱讀:索引別名文件、IK 分詞專案、elasticsearch-jdbc。

Copyright © Linux教程網 All Rights Reserved