目录
安装
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-6.8.3.zip
unzip elasticsearch-6.8.3.zip
cd elasticsearch-6.8.3
./bin/elasticsearch -d
curl http://localhost:9200?pretty
概念
Elasticsearch |
RDBMS |
Index |
DB |
Type |
Table |
Document |
Row |
Field |
Column |
原理
- Forward Index: 从文档的角度看其中的关键词 (即: 文档 => 关键词)
文档ID |
文档内容 |
1 |
人工智能成为互联网大会焦点 |
2 |
互联网的未来在人工智能 |
- Inverted Index: 从关键词角度看包含其的文档 (即: 关键词 => 文档)
关键词 |
倒排记录表 |
人工智能 |
1 -> 2 |
能 |
1 |
成为 |
1 |
互联网 |
1 -> 2 |
大会 |
1 |
焦点 |
1 |
的 |
2 |
未来 |
2 |
在 |
2 |
分词
curl -X PUT 'http://localhost:9200/test_index'
# curl -X DELETE 'localhost:9200/test_index'
curl -X POST 'http://localhost:9200/test_index/test_type/_mapping' -H 'Content-Type:application/json' -d '{"properties": {"content": {"type": "text"} } }'
curl -X POST 'http://localhost:9200/test_index/test_type/1' -H 'Content-Type:application/json' -d '{"content":"人工智能成为互联网大会焦点"}'
curl -X POST 'http://localhost:9200/test_index/test_type/2' -H 'Content-Type:application/json' -d '{"content":"互联网的未来在人工智能"}'
curl -X POST 'http://localhost:9200/test_index/test_type/_search' -H 'Content-Type:application/json' -d '{"query": {"match": {"content": "互联网"} } }'
# {"took":23,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.8630463,"hits":[{"_index":"test_index","_type":"test_type","_id":"2","_score":0.8630463,"_source":{"content":"互联网的未来在人工智能"}},{"_index":"test_index","_type":"test_type","_id":"1","_score":0.8630463,"_source":{"content":"人工智能成为互联网大会焦点"}}]}}
curl -X POST 'http://localhost:9200/test_index/test_type/_search' -H 'Content-Type:application/json' -d '{"query": {"match": {"content": "人工智能成为"} } }'
# {"took":37,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":1.7260926,"hits":[{"_index":"test_index","_type":"test_type","_id":"1","_score":1.7260926,"_source":{"content":"人工智能成为互联网大会焦点"}},{"_index":"test_index","_type":"test_type","_id":"2","_score":1.1507283,"_source":{"content":"互联网的未来在人工智能"}}]}}
# 安装方法1
./bin/elasticsearch-plugin install https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.3/elasticsearch-analysis-ik-6.8.3.zip
# 安装方法2
cd ~/Downloads
wget https://github.com/medcl/elasticsearch-analysis-ik/releases/download/v6.8.3/elasticsearch-analysis-ik-6.8.3.zip
unzip ~/Downloads/elasticsearch-analysis-ik-6.8.3.zip -d ~/Services/elasticsearch-6.8.3/plugins/analysis-ik
cd ~/Services/elasticsearch-6.8.3/plugins/analysis-ik
echo '人工智能成为' > config/custom_words.dic
vim config/IKAnalyzer.cfg.xml
# <entry key="ext_dict">custom_words.dic</entry>
kill -9 `lsof -t -i:9200`
cd ../..
./bin/elasticsearch -d
curl http://localhost:9200?pretty
curl -X PUT 'http://localhost:9200/ik_index'
# curl -X DELETE 'localhost:9200/ik_index'
curl -X POST 'http://localhost:9200/ik_index/ik_type/_mapping' -H 'Content-Type:application/json' -d '{"properties": {"content": {"type": "text", "analyzer": "ik_max_word", "search_analyzer": "ik_smart" } } }'
curl -X POST 'http://localhost:9200/ik_index/ik_type/1' -H 'Content-Type:application/json' -d '{"content":"人工智能成为互联网大会焦点"}'
curl -X POST 'http://localhost:9200/ik_index/ik_type/2' -H 'Content-Type:application/json' -d '{"content":"互联网的未来在人工智能"}'
curl -X POST 'http://localhost:9200/ik_index/ik_type/_search' -H 'Content-Type:application/json' -d '{"query": {"match": {"content": "互联网"} } }'
# {"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":2,"max_score":0.2876821,"hits":[{"_index":"ik_index","_type":"ik_type","_id":"2","_score":0.2876821,"_source":{"content":"互联网的未来在人工智能"}},{"_index":"ik_index","_type":"ik_type","_id":"1","_score":0.2876821,"_source":{"content":"人工智能成为互联网大会焦点"}}]}}
curl -X POST 'http://localhost:9200/ik_index/ik_type/_search' -H 'Content-Type:application/json' -d '{"query": {"match": {"content": "人工智能成为"} } }'
# {"took":5,"timed_out":false,"_shards":{"total":5,"successful":5,"skipped":0,"failed":0},"hits":{"total":1,"max_score":0.2876821,"hits":[{"_index":"ik_index","_type":"ik_type","_id":"1","_score":0.2876821,"_source":{"content":"人工智能成为互联网大会焦点"}}]}}
关于IK Analysis的更多介绍和特性 例如: 热更新IK分词 可以参考IK Analysis for Elasticsearch
备份
cnpm i -g elasticdump
elasticdump --input=http://localhost:9200/ik_index --output=./ik_index_analyzer.json --type=analyzer
cat ik_index_analyzer.json
# "{\"ik_index\":{\"settings\":{\"index\":{\"number_of_shards\":\"5\",\"number_of_replicas\":\"1\"}}}}"
elasticdump --input=http://localhost:9200/ik_index --output=./ik_index_mapping.json --type=mapping
cat ik_index_mapping.json
# {"ik_index":{"mappings":{"ik_type":{"properties":{"content":{"type":"text","analyzer":"ik_max_word","search_analyzer":"ik_smart"}}}}}}
elasticdump --input=http://localhost:9200/ik_index --output=./ik_index_data.json --type=data
cat ik_index_data.json
# {"_index":"ik_index","_type":"ik_type","_id":"2","_score":1,"_source":{"content":"互联网的未来在人工智能"}}
# {"_index":"ik_index","_type":"ik_type","_id":"1","_score":1,"_source":{"content":"人工智能成为互联网大会焦点"}}
参考