歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux綜合 >> 學習Linux >> 分布式搜索引擎Elasticsearch的簡單使用,elasticsearch

分布式搜索引擎Elasticsearch的簡單使用,elasticsearch

日期:2017/3/3 18:10:00   编辑:學習Linux

分布式搜索引擎Elasticsearch的簡單使用,elasticsearch

分布式搜索引擎Elasticsearch的簡單使用,elasticsearch


官方網址:https://www.elastic.co/products/elasticsearch/

一、特性

1、支持中文分詞

2、支持多種數據源的全文檢索引擎

3、分布式

4、基於lucene的開源搜索引擎

5、Restful api

二、資源

  • smartcn, 默認的中文分詞 :https://github.com/elasticsearch/elasticsearch-analysis-smartcn

  • mmseg :https://github.com/medcl/elasticsearch-analysis-mmseg

  • ik:https://github.com/medcl/elasticsearch-analysis-ik

  • pinyin, 拼音分詞可用於輸入拼音提示中文 :https://github.com/medcl/elasticsearch-analysis-pinyin

  • stconvert, 中文簡繁體互換 :https://github.com/medcl/elasticsearch-analysis-stconvert

  • elasticsearch-servicewrapper:https://github.com/elasticsearch/elasticsearch-servicewrapper

  • Elastic HQ,elasticsearch的監控工具:http://www.elastichq.org

  • elasticsearch-rtf :https://github.com/medcl/elasticsearch-rtf

三、安裝

  • 服務器:Linux(centos 6.x)

  • java環境: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

#創建.repo文件(elasticsearch.repo)
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

#導入key:
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. at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:93) at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:144) at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:285) at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:35) Refer to the log for complete error details.

這是出於系統安全考慮設置的條件。由於ElasticSearch可以接收用戶輸入的腳本並且執行,為了系統安全考慮, 建議創建一個單獨的用戶用來運行ElasticSearch

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
#服務ip地址
network.host: 0.0.0.0
#服務端口
http.port: 9200

四、IK的安裝

1.安裝maven工具

wget http://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.生成jar插件包

mvn clean
mvn compile
mvn package

unzip target/releases/elasticsearch-analysis-ik-*.zip
cp -r target/releases/ /usr/share/elasticsearch/plugins/ik

4.配置詞庫(ik自帶搜狗詞庫)

配置:/usr/share/elasticsearch/plugins/ik/config/ik/IKAnalyzer.cfg.xml

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

將jar包復制到Elasticsearch的plugins/analysis-ik 目錄下,再把解壓出的ik目錄(配置和詞典等),復制到Elasticsearch的config 目錄下。然後編輯配置文件elasticsearch.yml ,在後面加一行:

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

重啟service elasticsearch restart

然後錄入數據,創建索引

五、elasticsearch-jdbc

1、使用feeder方式

wget http://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=*'

2、使用river方式

#安裝elasticsearch
curl -OL https://download.elasticsearch.org/elasticsearch/elasticsearch/elasticsearch-1.4.2.zip

cd $ES_HOME
unzip path/to/elasticsearch-1.4.2.zip

#安裝JDBC插件
./bin/plugin --install jdbc --url http://xbib.org/repository/org/xbib/elasticsearch/plugin/elasticsearch-river-jdbc/1.4.0.6/elasticsearch-river-jdbc-1.4.0.6-plugin.zip

#下載mysql driver
curl -o mysql-connector-java-5.1.33.zip -L 'http://dev.mysql.com/get/Downloads/Connector-J/mysql-connector-java-5.1.33.zip/from/http://cdn.mysql.com/'
cp mysql-connector-java-5.1.33-bin.jar $ES_HOME/plugins/jdbc/ chmod 644 $ES_HOME/plugins/jdbc/*

#啟動elasticsearch
./bin/elasticsearch

#停止river
curl -XDELETE 'localhost:9200/_river/my_jdbc_river/'

JDBC插件參數

curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
    "type" : "jdbc",
    "jdbc" : {
        "url" : "jdbc:mysql://localhost:3306/test",
        "user" : "",
        "password" : "",
        "sql" : "select * from orders",
        "index" : "myindex",
        "type" : "mytype",
        ...
    }
}'

如果一個數組傳遞給jdbc字段,多個river源也是可以的

curl -XPUT 'localhost:9200/_river/my_jdbc_river/_meta' -d '{
     <river parameters>
    "type" : "jdbc",
    "jdbc" : [ {
         <river definition 1>
    }, {
         <river definition 2>
    } ]
}'

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"
     }
 }’

查看ES是否已經同步了這些數據  

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

官網地址:https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

參考

https://www.elastic.co/guide/en/elasticsearch/guide/current/empty-search.html

https://github.com/medcl/elasticsearch-analysis-ik

http://blog.csdn.net/clementad/article/details/46898013

https://endymecy.gitbooks.io/elasticsearch-guide-chinese/content/elasticsearch-river-jdbc.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/indices-aliases.html

https://github.com/jprante/elasticsearch-jdbc

http://www.voidcn.com/blog/wojiushiwo987/article/p-6058574.html

http://leotse90.com/2015/11/11/ElasticSearch%E4%B8%8EMySQL%E6%95%B0%E6%8D%AE%E5%90%8C%E6%AD%A5%E4%BB%A5%E5%8F%8A%E4%BF%AE%E6%94%B9%E8%A1%A8%E7%BB%93%E6%9E%84/

http://www.jianshu.com/p/638ff7b848cc

http://www.cnblogs.com/buzzlight/p/logstash_elasticsearch_kibana_log.html

http://xxxxxx/Linuxjc/1167516.html TechArticle

Copyright © Linux教程網 All Rights Reserved