啟動、停止服務
默認官方版啟動:
linux:./bin/elasticsearch start
window:直接運行bin/elasticsearch.bat
默認官方版停止:
linux:kill 掉elasticsearch 進程。
window:管理啟動的elasticsearch.bat 窗口
中文集成版啟動與停止:
中文集成版集成了 elasticsearch-servicewrapper這個es插件
linux:
./elasticsearch console ------前台運行 ./elasticsearch start ------后台運行 ./elasticsearch install -------添加到系統自動啟動 ./elasticsearch remove -----取消隨系統自動啟動
./elasticsearch stop ------停止
./elasticsearch restart ------重新啟動
window:直接運行elasticsearch.bat
Tips:
ik_max_word: 會將文本做最細粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,中華人民,中華,華人,人民共和國,人民,人,民,共和國,共和,和,國國,國歌”,會窮盡各種可能的組合;
ik_smart: 會做最粗粒度的拆分,比如會將“中華人民共和國國歌”拆分為“中華人民共和國,國歌”。
Download and install the .tar.gz packageedit
The .tar.gz archive for Elasticsearch v6.2.3 can be downloaded and installed as follows:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.2.3.tar.gz.sha512
[root@VM_0_15_centos soft]# shasum -bash: shasum: command not found [root@VM_0_15_centos soft]# [root@VM_0_15_centos soft]# [root@VM_0_15_centos soft]# yum install perl-Digest-SHA -y Loaded plugins: fastestmirror, langpacks Loading mirror speeds from cached hostfile Resolving Dependencies --> Running transaction check ---> Package perl-Digest-SHA.x86_64 1:5.85-4.el7 will be installed --> Processing Dependency: perl(Digest::base) for package: 1:perl-Digest-SHA-5.85-4.el7.x86_64 --> Running transaction check ---> Package perl-Digest.noarch 0:1.17-245.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved ================================================================================================================================ Package Arch Version Repository Size ================================================================================================================================ Installing: perl-Digest-SHA x86_64 1:5.85-4.el7 os 58 k Installing for dependencies: perl-Digest noarch 1.17-245.el7 os 23 k Transaction Summary ================================================================================================================================ Install 1 Package (+1 Dependent package) Total download size: 81 k Installed size: 138 k Downloading packages: (1/2): perl-Digest-1.17-245.el7.noarch.rpm | 23 kB 00:00:00 (2/2): perl-Digest-SHA-5.85-4.el7.x86_64.rpm | 58 kB 00:00:00 -------------------------------------------------------------------------------------------------------------------------------- Total 294 kB/s | 81 kB 00:00:00 Running transaction check Running transaction test Transaction test succeeded Running transaction Installing : perl-Digest-1.17-245.el7.noarch 1/2 Installing : 1:perl-Digest-SHA-5.85-4.el7.x86_64 2/2 Verifying : perl-Digest-1.17-245.el7.noarch 1/2 Verifying : 1:perl-Digest-SHA-5.85-4.el7.x86_64 2/2 Installed: perl-Digest-SHA.x86_64 1:5.85-4.el7 Dependency Installed: perl-Digest.noarch 0:1.17-245.el7 Complete! [root@VM_0_15_centos soft]# [root@VM_0_15_centos soft]# shasum -a 512 -c elasticsearch-6.2.3.tar.gz.sha512 elasticsearch-6.2.3.tar.gz: OK [root@VM_0_15_centos soft]#
http://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html
./elasticsearch [2018-04-17T15:52:07,762][WARN ][o.e.b.ElasticsearchUncaughtExceptionHandler] [] uncaught exception in thread [main] org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:125) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Elasticsearch.execute(Elasticsearch.java:112) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.cli.EnvironmentAwareCommand.execute(EnvironmentAwareCommand.java:86) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.cli.Command.mainWithoutErrorHandling(Command.java:124) ~[elasticsearch-cli-6.2.3.jar:6.2.3] at org.elasticsearch.cli.Command.main(Command.java:90) ~[elasticsearch-cli-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:92) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Elasticsearch.main(Elasticsearch.java:85) ~[elasticsearch-6.2.3.jar:6.2.3] Caused by: java.lang.RuntimeException: can not run elasticsearch as root at org.elasticsearch.bootstrap.Bootstrap.initializeNatives(Bootstrap.java:105) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Bootstrap.setup(Bootstrap.java:172) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Bootstrap.init(Bootstrap.java:323) ~[elasticsearch-6.2.3.jar:6.2.3] at org.elasticsearch.bootstrap.Elasticsearch.init(Elasticsearch.java:121) ~[elasticsearch-6.2.3.jar:6.2.3]
Elasticsearch Reference [6.2] » Set up Elasticsearch » Installing Elasticsearch » Install Elasticsearch with .zip or .tar.gz
https://www.elastic.co/guide/en/elasticsearch/reference/current/zip-targz.html#_checking_that_elasticsearch_is_running
異常描述為不能以root權限運行Elasticsearch.
解決辦法是運行時加上參數:
bin/elasticsearch -Des.insecure.allow.root=true
或者修改bin/elasticsearch,加上ES_JAVA_OPTS屬性:
ES_JAVA_OPTS="-Des.insecure.allow.root=true"
保存之后重啟Elasticsearch。
安裝運行
1、前置安裝java8
jdk-8u112-linux-x64.rpm
下載地址:http://www.oracle.com/technetwork/java/javase/downloads/index.html
2、下載
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-5.1.1.zip
3、解壓
unzip elasticsearch-5.1.1.zip
4、運行
./bin/elasticsearch
./bin/elasticsearch -d #后台運行
tail -f logs/elasticsearch.log #查看日志
注:ES有執行腳本的能力,因安全因素,不能在root用戶下運行,強行運行會報如下錯誤:
org.elasticsearch.bootstrap.StartupException: java.lang.RuntimeException: can not run elasticsearch as root
解決方案:
groupadd es #增加es組
useradd es -g es -p pwd #增加es用戶並附加到es組
chown -R es:es elasticsearch-5.1.1 #給目錄權限
su es #使用es用戶
./bin/elasticsearch -d #后台運行es
外網訪問
vi conf/elasticsearch.yml
修改network.host: 0.0.0.0
再次啟動linux可能出現如下類似錯誤
bootstrap checks failed
max file descriptors [65535] for elasticsearch process is too low, increase to at least [65536]
解決方案
1、vi /etc/sysctl.conf
設置fs.file-max=655350
保存之后sysctl -p使設置生效
2、vi /etc/security/limits.conf 新增
* soft nofile 655350
* hard nofile 655350
3、重新使用SSH登錄,再次啟動elasticsearch即可。
外網訪問:serverip:9200/
安裝elasticsearch-head插件
elasticsearch 5以后的版本比較新,不支持直接安裝head插件,以下是github上提供的安裝方法,如下圖示
1、下載head插件
wget https://codeload.github.com/mobz/elasticsearch-head/zip/master
2、下載nodejs
nodejs官網下載地址https://nodejs.org/dist/
wget https://nodejs.org/dist/v6.9.2/node-v6.9.2-linux-x64.tar.xz
3、配置node環境變量
xz –d node-v6.9.2-linux-x64.tar.xz
tar –xvf node-v6.9.2-linux-x64.tar
mv node-v6.9.2-linux-x64 /alidata/app/node
vim /etc/profile
export NODE_HOME=/alidata/app/node
export PATH=$PATH:$NODE_HOME/bin
source /etc/profile
# node –v
v6.9.2
# npm –v
3.10.9
4、安裝grunt
cd elasticsearch-head-master
npm install -g grunt --registry=https://registry.npm.taobao.org
5、測試一下
cd elasticsearch-head-master
grunt
出現以下提示,為Gruntfile.js引用的,缺少以下包
>> Local Npm module "grunt-contrib-clean" not found. Is it installed?
>> Local Npm module "grunt-contrib-concat" not found. Is it installed?
>> Local Npm module "grunt-contrib-watch" not found. Is it installed?
>> Local Npm module "grunt-contrib-connect" not found. Is it installed?
>> Local Npm module "grunt-contrib-copy" not found. Is it installed?
>> Local Npm module "grunt-contrib-jasmine" not found. Is it installed?
Warning: Task "connect:server" not found. Use --force to continue.
Aborted due to warnings.
安裝
npm install grunt-contrib-clean --registry=https://registry.npm.taobao.org
npm install grunt-contrib-concat --registry=https://registry.npm.taobao.org
npm install grunt-contrib-watch --registry=https://registry.npm.taobao.org
npm install grunt-contrib-connect --registry=https://registry.npm.taobao.org
npm install grunt-contrib-copy --registry=https://registry.npm.taobao.org
npm install grunt-contrib-jasmine --registry=https://registry.npm.taobao.org
6、運行
grunt server
7、測試
http://serverip:9100/
修改head目錄下的Gruntfile.js配置,head默認監聽127.0.0.1
vm Gruntfile.js
hostname: '0.0.0.0',
8、為es設置跨域訪問
vi config/elasticsearch.yml #新增兩行
http.cors.enabled: true
http.cors.allow-origin: "*"
9、啟動es,啟動head插件
https://blog.csdn.net/laotoumo/article/details/53890279
一、拼音分詞的應用
拼音分詞在日常生活中其實很常見,也許你每天都在用。打開淘寶看一看吧,輸入拼音”zhonghua”,下面會有包含”zhonghua”對應的中文”中華”的商品的提示:
拼音分詞是根據輸入的拼音提示對應的中文,通過拼音分詞提升搜索體驗、加快搜索速度。下面介紹如何在Elasticsearch 5.1.1中配置和實現pinyin+iK分詞。
二、IK分詞器下載與安裝
關於IK分詞器的介紹不再多少,一言以蔽之,IK分詞是目前使用非常廣泛分詞效果比較好的中文分詞器。做ES開發的,中文分詞十有八九使用的都是IK分詞器。
下載地址:https://github.com/medcl/elasticsearch-analysis-ik
配置之前關閉elasticsearch,配置完成以后再重啟。
IK的版本要和當前ES的版本一致,README中有說明。我使用的是ES是5.1.1,IK的版本為5.1.1(你也許會奇怪為什么IK上一個版本是1.X,下一個版本一下升到5.X?是因為Elastic官方為了統一版本號,之前es的版本是2.x,logstash的版本是2.x,同時Kibana的版本是4.x,ik的版本是1.x,這樣版本很混亂。5.0之后,統一版本號,這樣你使用5.1.1的es,其它軟件的版本也使用5.1.1就好了)。
下載之后進入到elasticsearch-analysis-pinyin-master目錄,mvn打包(沒有安裝maven的自行安裝),運行命令:
mvn package
打包成功以后,會生成一個target文件夾,在elasticsearch-analysis-ik-master/target/releases目錄下,找到elasticsearch-analysis-ik-5.1.1.zip,這就是我們需要的安裝文件。解壓elasticsearch-analysis-ik-5.1.1.zip,得到下面內容:
commons-codec-1.9.jar commons-logging-1.2.jar config elasticsearch-analysis-ik-5.1.1.jar httpclient-4.5.2.jar httpcore-4.4.4.jar plugin-descriptor.properties
然后在elasticsearch-5.1.1/plugins目錄下新建一個文件夾ik,把elasticsearch-analysis-ik-5.1.1.zip解壓后的文件拷貝到elasticsearch-5.1.1/plugins/ik目錄下.截圖方便理解。
三、pinyin分詞器下載與安裝
pinyin分詞器的下載地址:
https://github.com/medcl/elasticsearch-analysis-pinyin
安裝過程和IK一樣,下載、打包、加入ES。這里不在重復上述步驟,給出最后配置截圖
四、分詞測試
IK和pinyin分詞配置完成以后,重啟ES。如果重啟過程中ES報錯,說明安裝有錯誤,沒有報錯說明配置成功。
4.1 IK分詞測試
創建一個索引:
curl -XPUT "http://localhost:9200/index"
測試分詞效果:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=ik_max_word&text=中華人民共和國"
分詞結果:
{
"tokens": [{ "token": "中華人民共和國", "start_offset": 0, "end_offset": 7, "type": "CN_WORD", "position": 0 }, { "token": "中華人民", "start_offset": 0, "end_offset": 4, "type": "CN_WORD", "position": 1 }, { "token": "中華", "start_offset": 0, "end_offset": 2, "type": "CN_WORD", "position": 2 }, { "token": "華人", "start_offset": 1, "end_offset": 3, "type": "CN_WORD", "position": 3 }, { "token": "人民共和國", "start_offset": 2, "end_offset": 7, "type": "CN_WORD", "position": 4 }, { "token": "人民", "start_offset": 2, "end_offset": 4, "type": "CN_WORD", "position": 5 }, { "token": "共和國", "start_offset": 4, "end_offset": 7, "type": "CN_WORD", "position": 6 }, { "token": "共和", "start_offset": 4, "end_offset": 6, "type": "CN_WORD", "position": 7 }, { "token": "國", "start_offset": 6, "end_offset": 7, "type": "CN_CHAR", "position": 8 }, { "token": "國歌", "start_offset": 7, "end_offset": 9, "type": "CN_WORD", "position": 9 }] }
使用ik_smart分詞:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=ik_smart&text=中華人民共和國"
分詞結果:
{
"tokens": [{ "token": "中華人民共和國", "start_offset": 0, "end_offset": 7, "type": "CN_WORD", "position": 0 }, { "token": "國歌", "start_offset": 7, "end_offset": 9, "type": "CN_WORD", "position": 1 }] }
截圖方便理解:
4.2拼音分詞測試
測試拼音分詞:
curl -XPOST "http://localhost:9200/index/_analyze?analyzer=pinyin&text=張學友"
分詞結果:
{
"tokens": [{ "token": "zhang", "start_offset": 0, "end_offset": 1, "type": "word", "position": 0 }, { "token": "xue", "start_offset": 1, "end_offset": 2, "type": "word", "position": 1 }, { "token": "you", "start_offset": 2, "end_offset": 3, "type": "word", "position": 2 }, { "token": "zxy", "start_offset": 0, "end_offset": 3, "type": "word", "position": 3 }] }
五、IK+pinyin分詞配置
5.1創建索引與分析器設置
創建一個索引,並設置index分析器相關屬性:
curl -XPUT "http://localhost:9200/medcl/" -d' { "index": { "analysis": { "analyzer": { "ik_pinyin_analyzer": { "type": "custom", "tokenizer": "ik_smart", "filter": ["my_pinyin", "word_delimiter"] } }, "filter": { "my_pinyin": { "type": "pinyin", "first_letter": "prefix", "padding_char": " " } } } } }'
創建一個type並設置mapping:
curl -XPOST http://localhost:9200/medcl/folks/_mapping -d' { "folks": { "properties": { "name": { "type": "keyword", "fields": { "pinyin": { "type": "text", "store": "no", "term_vector": "with_positions_offsets", "analyzer": "ik_pinyin_analyzer", "boost": 10 } } } } } }'
5.2索引測試文檔
索引2份測試文檔。
文檔1:
curl -XPOST http://localhost:9200/medcl/folks/andy -d'{"name":"劉德華"}'
文檔2:
curl -XPOST http://localhost:9200/medcl/folks/tina -d'{"name":"中華人民共和國國歌"}'
5.3測試(1)拼音分詞
下面四條命命令都可以匹配”劉德華”
curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:liu" curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:de" curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:hua" curl -XPOST "http://localhost:9200/medcl/folks/_search?q=name.pinyin:ldh"
5.4測試(2)IK分詞測試
curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d' { "query": { "match": { "name.pinyin": "國歌" } }, "highlight": { "fields": { "name.pinyin": {} } } }'
返回結果:
{
"took" : 2, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 1, "max_score" : 16.698704, "hits" : [ { "_index" : "medcl", "_type" : "folks", "_id" : "tina", "_score" : 16.698704, "_source" : { "name" : "中華人民共和國國歌" }, "highlight" : { "name.pinyin" : [ "<em>中華人民共和國</em><em>國歌</em>" ] } } ] } }
說明IK分詞器起到了效果。
5.3測試(4)pinyin+ik分詞測試:
curl -XPOST "http://localhost:9200/medcl/_search?pretty" -d' { "query": { "match": { "name.pinyin": "zhonghua" } }, "highlight": { "fields": { "name.pinyin": {} } } }'
返回結果:
{
"took" : 3, "timed_out" : false, "_shards" : { "total" : 5, "successful" : 5, "failed" : 0 }, "hits" : { "total" : 2, "max_score" : 5.9814634, "hits" : [ { "_index" : "medcl", "_type" : "folks", "_id" : "tina", "_score" : 5.9814634, "_source" : { "name" : "中華人民共和國國歌" }, "highlight" : { "name.pinyin" : [ "<em>中華人民共和國</em>國歌" ] } }, { "_index" : "medcl", "_type" : "folks", "_id" : "andy", "_score" : 2.2534127, "_source" : { "name" : "劉德華" }, "highlight" : { "name.pinyin" : [ "<em>劉德華</em>" ] } } ] } }
截圖如下:
使用pinyin分詞以后,原始的字段搜索要加上.pinyin后綴,搜索原始字段沒有返回結果:
六、參考資料
https://blog.csdn.net/napoay/article/details/53907921