Elasticsearch简介

目录

安装

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":"互联网的未来在人工智能"}}]}}
  • IK Analysis
# 安装方法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":"人工智能成为互联网大会焦点"}}

参考

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 一、 什么是 Elasticsearch 想查数据就免不了搜索,搜索就离不开搜索引擎,百度、谷歌都是一个非常庞大复...
    古佛青灯度流年阅读 4,818评论 1 3
  • 什么是Elasticsearch?   Elasticsearch是一个开源的分布式、RESTful 风格的搜索和...
    山阴少年阅读 193,026评论 6 137
  • 前言:这是我一边学习Elasticsearch(版本是5.2.0)一边写的博客,以后会慢慢完善 什么是全文搜索 全...
    缓慢移动的蜗牛阅读 3,414评论 0 0
  • 什么是搜索? 百度:我们比如说想找寻任何的信息的时候,就会上百度去搜索一下,比如说找一部自己喜欢的电影,或者说找一...
    不知名的蛋挞阅读 3,142评论 0 0
  • 声明:本文转自我的个人博客,有兴趣的可以查看原文。转发请注明来源。 这是一篇科普文。 1. 背景 Elastics...
    此星爷非彼星爷阅读 5,450评论 0 14

友情链接更多精彩内容