發文也是為了發現問題,如果文中有誤請一定指出,共同學習,不勝感激。
本篇筆記記錄較早,當時ES 版本應為5.5.0,目前應該是5.6.12。
一般這種安裝都是開發測試使用,生產環境還是推薦直接使用各大云廠商提供的在線版本。
安裝
需要java 8
1.導入ELasticsearch PGP KEY
rpm --import https://artifacts.elastic.co/GPG-KEY-elasticsearch
2.設置rpm 庫
nano /etc/yum.repos.d/elasticsearch.repo
文件內容如下
[elasticsearch-5.x]
name=Elasticsearch repository for 5.x packages
baseurl=https://artifacts.elastic.co/packages/5.x/yum
gpgcheck=1
gpgkey=https://artifacts.elastic.co/GPG-KEY-elasticsearch
enabled=1
autorefresh=1
type=rpm-md
3.安裝
yum install elasticsearch
4.添加到系統服務
chkconfig --add elasticsearch
systemctl enable elasticsearch.service
5.啟動
service elasticsearch start
6.本機測試
curl -XGET 'localhost:9200/?pretty'
# 查看打印信息
7.新建數據文件夾
cd /home
mkidr elasticsearch
cd elasticsearch
mkdir data
mkdir logs
cd ../
chown -R elasticsearch:elasticsearch elasticsearch
注意:默認Elasticsearch 的啟動用戶是 elasticsearch,修改數據存放路徑時,不能忘了chown命令,否則可能無法啟動。
8.修改配置
配置文件為/etc/elasticsearch/elasticsearch.yml,主要注意以下幾個設置:
cluster.name: esearch
node.name: node-1
path.data: /home/elasticsearch/data
path.logs: /home/elasticsearch/logs
network.host: 192.168.1.20 #注意,不改的話默認只能通過localhost訪問
http.port: 9200
9.重啟服務并驗證
service elasticsearch stop
service elasticsearch start
service elasticsearch status
curl -XGET '192.168.1.20:9200/?pretty'
10.插件安裝
方法1:
cd /usr/share/elasticsearch
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v5.5.0/elasticsearch-analysis-ik-5.5.0.zip
# 注意版本號,最新的插件版本是5.6.11,如果沒有跟ES 對應的插件版本,可能需要手動安裝插件后修改版本,否則啟動報錯。
方法2:
從https://github.com/medcl/elasticsearch-analysis-ik下載中文分詞插件(可能需要翻墻),并解壓到/usr/share/elasticsearch/plugins/ik目錄下。重啟服務。
修改插件版本的方法:
插件配置文件/usr/share/elasticsearch/plugins/ik/plugin-descriptor.properties, 修改其中的elasticsearch.version 與ES 版本對應上即可。
11.測試中文分詞
curl -XPUT http://192.168.1.20:9200/testindex?pretty curl -XPOST http://192.168.1.20:9200/testindex/testtext/_mapping?pretty -d' { "testtext": { "_all": { "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "term_vector": "no", "store": "false" }, "properties": { "content": { "type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_max_word", "include_in_all": "true", "boost": 8 } } } }' curl -XPOST http://192.168.1.20:9200/testindex/testtext/1?pretty -d' {"content":"美國留給伊拉克的是個爛攤子嗎"} ' curl -XPOST http://192.168.1.20:9200/testindex/testtext/2?pretty -d' {"content":"公安部:各地校車將享最高路權"} ' curl -XPOST http://192.168.1.20:9200/testindex/testtext/3?pretty -d' {"content":"中韓漁警沖突調查:韓警平均每天扣1艘中國漁船"} ' curl -XPOST http://192.168.1.20:9200/testindex/testtext/4?pretty -d' {"content":"中國駐洛杉磯領事館遭亞裔男子槍擊 嫌犯已自首"} ' curl -XPOST http://192.168.1.20:9200/testindex/testtext/_search?pretty -d' { "query" : { "match" : { "content" : "中國" }}, "highlight" : { "pre_tags" : ["<tag1>", "<tag2>"], "post_tags" : ["</tag1>", "</tag2>"], "fields" : { "content" : {} } } } '
12.瀏覽器測試中文分詞
http://192.168.1.20:9200/testindex/_analyze?pretty&text=中華人民共和國MN&tokenizer=ik_max_word
其他命令
1.查詢index
curl 'http://192.168.1.20:9200/_cat/indices?v'
2.查詢mapping
curl -XGET "http://192.168.1.20:9200/testindex/_mapping?pretty"
3.關閉和開啟索引
curl -XPOST 'http://192.168.1.20:9200/testindex_v1/_close?pretty' curl -XPOST 'http://192.168.1.20:9200/testindex_v1/_open?pretty'
4.terms 查詢
curl -XPOST http://192.168.1.20:9200/testindex_v1/testDocument/_search?pretty -d' { "query" : { "term" : { "myTag" : "中國" }} } ' curl -XPOST http://192.168.1.20:9200/testindex_v1/testDocument/_search?pretty -d' { "query" : { "term" : { "myTag.keyword" : "中國" }} } '
使用myTag.keyword ,與mapping 的type 屬性有關。
5.索引別名
建議程序中使用索引別名,這樣ES 有什么改動的話比較方便。
# 查詢 curl -XGET 192.168.1.20:9200/_alias?pretty # 添加別名 curl -XPOST 192.168.1.20:9200/_aliases -d ' { "actions": [ { "add": { "alias": "index_alias_name", "index": "testindex_v1" }} ] } ' # 修改別名 curl -XPOST 192.168.1.20:9200/_aliases -d ' { "actions": [ { "remove": { "alias": "index_alias_name", "index": "testindex_v1" } }, { "add": { "alias": "index_alias_name", "index": "testindex_v2" } } ] } '
6.查詢索引量
curl -X POST http://192.168.1.20:9200/index_alias_name/testDocument/_count?pretty
7.重新索引
參考文檔:https://blog.csdn.net/it_lihongmin/article/details/78601697
curl -X POST "192.168.1.20:9200/_reindex" -H 'Content-Type: application/json' -d' { "source": { "index": "testindex_v1" }, "dest": { "index": "testindex_v2" } } '
8.刪除索引