第一、ElasticSearch 簡介
1、Solr 利用 Zookeeper 進行分佈式管理,而 Elasticsearch 自身帶有分佈式協調管理功能;
2、 Solr 支持更多格式的數據,而 Elasticsearch 僅支持json文件格式;
3、 Solr 官方提供的功能更多,而 Elasticsearch 本身更注重於核心功能,高級功能多有第三方插件提供
4、Solr 在傳統的搜索應用中表現好於 Elasticsearch,但在處理實時搜索應用時效率明顯低於 Elasticsearch
第二、Windows 安裝ElasticSearch 服務
第三、安裝ElasticSearch的圖形化界面插件
ElasticSearch不同於Solr自帶圖形化界面,我們可以通過安裝ElasticSearch的head插件,完成圖形化界面的效 果,完成索引數據的查看。安裝插件的方式有兩種,在線安裝和本地安裝。本文檔採用本地安裝方式進行head插 件的安裝。elasticsearch-5-*以上版本安裝head需要安裝node和grunt
安裝完畢,可以通過cmd控制檯輸入:node -v 查看版本號
npm install ‐g grunt‐cli
http.cors.enabled: true
http.cors.allow-origin: "*"
第四、ElasticSearch相關概念(術語)
4.1 概述
4.2 ElasticSearch 核心概念
第五、ElasticSearch客戶端操作之Postman
1、ElasticSearch的接口語法
curl ‐X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' ‐d '<BODY>'
參數說明:
2、Postman 操作之創建索引庫index = 新增索引庫blog1
3、Postman 操作之創建索引庫index 添加映射mapping =爲bolg1索引庫添加hello 的映射
請求URL: post http://localhost:9200/blog1/hello/_mapping
請求體:
{
"hello": {
"properties": {
"id":{
"type":"long",
"store":true
},
"title":{
"type":"text",
"store":true,
"index":true,
"analyzer":"standard"
},
"content":{
"type":"text",
"store":true,
"index":true,
"analyzer":"standard"
}
}
}
}
postman 截圖:
4、postman 操作之刪除索引庫
請求URL: delete http://localhost:9200/blog1
postman截圖:
5、postman 操作之創建index 索引庫和映射mapping = 創建blog2 索引庫並指定article 的相關映射
請求URL: put http://localhost:9200/blog2
請求體:
{
"mappings": {
"article": {
"properties": {
"id": {
"type": "long",
"store": true
},
"title": {
"type": "text",
"store": true
},
"content": {
"type": "text",
"store": true
}
}
}
}
}
postman 截圖:
6、postman 操作之新增文檔
請求URL: post http://localhost:9200/blog2/article/1
請求體:
{
"id":1,
"title":"ElasticSearch是一個基於Lucene的搜索服務器",
"content":"它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java 開發的,並作爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠達到實時 搜索,穩定,可靠,快速,安裝使用方便。"
}
postman 截圖:
7、postman 操作之更新文檔
請求URL: post http://localhost:9200/blog2/article/1
請求體:
{
"id":1,
"title":"【修改】ElasticSearch是一個基於Lucene的搜索服務器",
"content":"【修改】它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch 是用Java開發的,並作爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠 達到實時搜索,穩定,可靠,快速,安裝使用方便。"
}
postman 截圖:
8、postman 操作之刪除文檔
請求URL: delete http://localhost:9200/blog2/article/1
postman 截圖:
9、postman 操作之刪除文檔
請求URL: delete http://localhost:9200/blog2/article/1
postmane 截圖:
10、postman 操作之根據文檔ID進行查詢
請求URL: get localhost:9200/blog2/article/1
postmane 截圖:
11、postman 操作之根據文檔querystring進行查詢
請求URL: post localhost:9200/blog2/article/_search
請求體:
{ "query": { "query_string": { "default_field": "title", "query": "搜索服務器" } } }
postmane 截圖:
12、postmane 操作之 根據文檔term進行查詢
請求URL: post localhost:9200/blog2/article/_search
請求體:
{ "query": { "term": { "title": "搜索" } } }
posmane 截圖:
第六、ElasticSearch服務端集成IK分詞
6.1 IK分詞器簡介:
6.3 IK分詞功能測試
{
"analyzer":"ik_smart",
"text":"我是程序員"
}
postmane 截圖:
2)最細切分:在瀏覽器地址欄輸入地址
請求URL: post http://127.0.0.1:9200/_analyze?pretty=true
請求體:
{
"analyzer":"ik_max_word",
"text":"我是程序員"
}
postman 截圖:
6.4 修改索引庫的mapping 映射
1、刪除原有的blog2 索引庫
delete http://localhost:9200/blog2
put http://localhost:9200/blog2
請求體:
{
"mappings": {
"article": {
"properties": {
"id": {
"type": "long",
"store": true
},
"title": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
},
"content": {
"type": "text",
"store": true,
"index":true,
"analyzer":"ik_max_word"
}
}
}
}
}
3、創建文檔
post http://localhost:9200/blog2/article/1
請求體:
{
"id":1,
"title":"ElasticSearch是一個基於Lucene的搜索服務器",
"content":"它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java 開發的,並作爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,能夠達到實時 搜索,穩定,可靠,快速,安裝使用方便。"
}
5、再次執行trem 測試
檢索結果: