1 Elasticsearch簡介
1.1 什麼是ElasticSearch
Elasticsearch是一個實時的分布式搜尋和分析引擎。它可以幫助你用前所未有的速 度去處理大規模資料。ElasticSearch是一個基于Lucene的搜尋伺服器。它提供了一個分 布式多使用者能力的全文搜尋引擎,基于RESTful web接口。Elasticsearch是用Java開發 的,并作為Apache許可條款下的開放源碼釋出,是目前流行的企業級搜尋引擎。設計用 于雲計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用友善
1.2 ElasticSearch特點
(1)可以作為一個大型分布式叢集(數百台伺服器)技術,處理PB級資料,服務大公 司;也可以運作在單機上
(2)将全文檢索、資料分析以及分布式技術,合并在了一起,才形成了獨一無二的ES;
(3)開箱即用的,部署簡單
(4)全文檢索,同義詞處理,相關度排名,複雜資料分析,海量資料的近實時處理
1.3 ElasticSearch體系結構
索引(index) -》資料庫(databases)
類型(type) -》表(table)
文檔(document) -》 行(row)
2 安裝ES
去官網下載下傳ElasticSearch 5.6.8版本( zip 格式)
https://www.elastic.co/downloads/past-releases/elasticsearch-5-6-8
無需安裝,解壓安裝包後即可使用,進入ElasticSearch安裝目錄下的bin目錄,執行指令
elasticsearch
打開浏覽器,在位址欄輸入http://127.0.0.1:9200/ 即可看到輸出結果(預設端口号9200);傳回json格式資料
![](https://img.laitimes.com/img/__Qf2AjLwojIjJCLyojI0JCLiIXZ05WZj91YpB3IwczX0xiRGZkRGZ0Xy9GbvNGL2EzXlpXazxSP9cmTy0EVONTRE5EM4wmYwhGWhxGZzwEMW1mY1RzRapnTtxkb5ckYplTeMZTTINGMShUYfRHelRHLwEzX39GZhh2css2RkBnVHFmb1clWvB3MaVnRtp1XlBXe0xyayFWbyVGdhd3LcV2Zh1Wa9M3clN2byBXLzN3btg3Pn5GcuEzN0AjMwMTMwMDOwAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
2.2 Postman調用RestAPI
2.2.1 建立索引
建立一個叫articleindex的索引 ,就以put方式送出
http://127.0.0.1:9200/tensquare_elasticsearch
2.2.2 建立文檔
以post方式送出 http://127.0.0.1:9200/tensquare_elasticsearch/article
body:
{
"title":"spring教程",
"content":"spring架構教程"
}
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": true
}
_id是由系統自動生成的。
2.2.3 查詢全部文檔
查詢某索引某類型的全部資料,以get方式請求(_search):http://127.0.0.1:9200/tensquare_elasticsearch/article/_search
result:
{
"took": 30,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 1.0,
"hits": [
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_score": 1.0,
"_source": {
"title": "spring教程",
"content": "spring架構教程"
}
}
]
}
}
2.2.4 修改文檔
以put形式(需要攜帶id,根據id來有就修改,沒有就新增)送出以下位址:http://127.0.0.1:9200/tensquare_elasticsearch/article/AXQ92f-y6VlK-8rsjN9Q
body:
{
"title":"spring教程",
"content":"spring修改操作"
}
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "AXQ92f-y6VlK-8rsjN9Q",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"created": false
}
2.2.5 按ID查詢文檔
get帶id通路:http://127.0.0.1:9200/tensquare_elasticsearch/article/1
result:
{
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 1,
"found": true,
"_source": {
"title": "spring教程",
"content": "spring修改操作"
}
}
2.2.6 基本比對查詢
根據某列進行查詢 get方式送出下列位址:
http://127.0.0.1:9200/tensquare_elasticsearch/article/_search?q=title:spring
_search?q=title:spring(title=‘spring’)
2.2.7 模糊查詢
加*
http://127.0.0.1:9200/tensquare_elasticsearch/article/_search?q=title:*s*
2.2.8 删除文檔
根據ID删除文檔,删除ID為1的文檔 DELETE方式送出
http://127.0.0.1:9200/tensquare_elasticsearch/article/1
result:
{
"found": true,
"_index": "tensquare_elasticsearch",
"_type": "article",
"_id": "1",
"_version": 2,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
}
}
3 ik分詞器使用
浏覽器輸入:
http://localhost:9200/_analyze?analyzer=chinese&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
預設的中文分詞是将每個字看成一個詞,這顯然是不符合要求的,是以我們需要安裝[中文分詞器]來解決這個問題。
下載下傳位址:
https://github.com/medcl/elasticsearch-analysis-ik/releases
下載下傳5.6.8版本
(1)先将其解壓,将解壓後的elasticsearch檔案夾重命名檔案夾為ik
(2)将ik檔案夾拷貝到elasticsearch/plugins 目錄下。
(3)重新啟動,即可加載IK分詞器
win重新開機進入es/bin/cmd:elasticsearch
4.3 IK分詞器測試
IK提供了兩個分詞算法ik_smart 和 ik_max_word
其中 ik_smart 為最少切分,ik_max_word為最細粒度劃分
(1)ik_smart最小切分:在浏覽器位址欄輸入位址
http://localhost:9200/_analyze?analyzer=ik_smart&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
(2)ik_max_word最細切分:在浏覽器位址欄輸入位址
http://localhost:9200/_analyze?analyzer=ik_max_word&pretty=true&text=%E6%88%91%E6%98%AF%E7%A8%8B%E5%BA%8F%E5%91%98
傳回結果:
{
"tokens": [
{
"token": "我",
"start_offset": 0,
"end_offset": 1,
"type": "CN_CHAR",
"position": 0
},
{
"token": "是",
"start_offset": 1,
"end_offset": 2,
"type": "CN_CHAR",
"position": 1
},
{
"token": "程式員",
"start_offset": 2,
"end_offset": 5,
"type": "CN_WORD",
"position": 2
},
{
"token": "程式",
"start_offset": 2,
"end_offset": 4,
"type": "CN_WORD",
"position": 3
},
{
"token": "員",
"start_offset": 4,
"end_offset": 5,
"type": "CN_CHAR",
"position": 4
}
]
}
4.4 自定義詞庫
步驟:
(1)進入elasticsearch/plugins/ik/config目錄
(2)建立一個my.dic檔案,編輯内容:
我是中國人
修改IKAnalyzer.cfg.xml(在ik/config目錄下)
重新啟動elasticsearch,通過浏覽器測試自定義分詞效果
http://localhost:9200/_analyze?analyzer=ik_smart&pretty=true&text=%E6%88%91%E6%98%AF%E4%B8%AD%E5%9B%BD%E4%BA%BA