ElasticSearch-命令列客戶端操作

LemonSquash發表於2021-01-25

1.引言

實際開發中,主要有三種方式可以作為elasticsearch服務的客戶端:
第一種,elasticsearch-head外掛(視覺化工具)

第二種,使用elasticsearch提供的Restful介面直接訪問(僅僅使用者學習測試)

第三種,使用elasticsearch提供的API進行訪問,使用JavaAPI去訪問ES!(實際開發)

2.Postman工具

Postman中文版是postman這款強大網頁除錯工具的windows客戶端,提供功能強大的Web API & HTTP 請求除錯。軟體功能非常強大,介面簡潔明晰、操作方便快捷,設計得很人性化。Postman中文版能夠傳送任何型別的HTTP 請求 (GET, HEAD, POST, PUT..),且可以附帶任何數量的引數。

3.安裝Postman工具

Postman官網:https://www.getpostman.com
課程資料中已經提供了安裝包
在這裡插入圖片描述
在這裡插入圖片描述

4.使用Postman工具進行Restful介面訪問

4.1.ElasticSearch的介面語法

curl ‐X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' ‐d '<BODY>

其中:

引數 解釋
VERB 適當的 HTTP 方法謂詞 : GETPOSTPUTHEAD 或者 DELETE
PROTOCOL http 或者 https(如果你在 Elasticsearch 前面有一個 https 代理)
HOST Elasticsearch 叢集中任意節點的主機名,或者用 localhost 代表本地機器上的節點。
PORT 執行 Elasticsearch HTTP 服務的埠號,預設是 9200
PATH API 的終端路徑(例如 _count 將返回叢集中文件數量)。Path 可能包含多個元件,例如:_cluster/stats_nodes/stats/jvm
QUERY_STRING 任意可選的查詢字串引數 (例如 ?pretty 將格式化地輸出 JSON 返回值,使其更容易閱讀)
BODY 一個 JSON 格式的請求體 (如果請求需要的話)

4.2 建立索引index和對映mapping

請求url:

PUT	localhost:9200/blog1

請求體:

如果ES的版本不同,那麼以下的請求題需要同步更新~~~~,新版本ES改動比較大。但是JavaAPI的使用方式沒什麼變化~
{
    "mappings": {
        "article": {
            "properties": {
                "id": {
                	"type": "long",
                    "store": true,
                    "index":"not_analyzed" 
                },
                "title": {
                	"type": "text",       # 欄位型別
                    "store": true,        # 是否在索引中存貯
                    "index":"analyzed",   # 是否需要被索引
                    "analyzer":"standard" # 標準分詞器 內建的
                },
                "content": {
                	"type": "text",
                    "store": true,
                    "index":"analyzed",   
                    "analyzer":"standard"
                }
            }
        }
    }
}

postman截圖:
在這裡插入圖片描述
elasticsearch-head檢視:
在這裡插入圖片描述

4.3 建立索引後設定Mapping

我們可以在建立索引時設定mapping資訊,當然也可以先建立索引然後再設定mapping。
在上一個步驟中不設定maping資訊,直接使用put方法建立一個索引,然後設定mapping資訊。
在這裡插入圖片描述
請求的url:

POST	http://127.0.0.1:9200/blog2/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.4 刪除索引index

請求url:

DELETE		localhost:9200/blog1

postman截圖:
在這裡插入圖片描述
elasticsearch-head檢視:
在這裡插入圖片描述

4.5 建立文件document

請求url:

POST	localhost:9200/blog1/article/1

請求體:

{
	"id":1,
	"title":"ElasticSearch是一個基於Lucene的搜尋伺服器",
	"content":"它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放原始碼釋出,是當前流行的企業級搜尋引擎。設計用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。"
}

postman截圖:
在這裡插入圖片描述elasticsearch-head檢視:
在這裡插入圖片描述

4.6 修改文件document

請求url:

POST	localhost:9200/blog1/article/1

請求體:

{
	"id":1,
	"title":"【修改】ElasticSearch是一個基於Lucene的搜尋伺服器",
	"content":"【修改】它提供了一個分散式多使用者能力的全文搜尋引擎,基於RESTful web介面。Elasticsearch是用Java開發的,並作為Apache許可條款下的開放原始碼釋出,是當前流行的企業級搜尋引擎。設計用於雲端計算中,能夠達到實時搜尋,穩定,可靠,快速,安裝使用方便。"
}

postman截圖:
在這裡插入圖片描述
elasticsearch-head檢視:
在這裡插入圖片描述

4.7 刪除文件document

請求url:

DELETE	localhost:9200/blog1/article/1

postman截圖:
在這裡插入圖片描述
elasticsearch-head檢視:
在這裡插入圖片描述

4.8 查詢文件-根據id查詢

請求url:

GET	localhost:9200/blog1/article/1

postman截圖:

在這裡插入圖片描述

4.9 查詢文件-querystring查詢

請求url:

POST	localhost:9200/blog1/article/_search

請求體:

{
    "query": {
        "query_string": {
            "default_field": "title",
            "query": "搜尋伺服器"
        }
    }
}

postman截圖:
在這裡插入圖片描述
注意:
將搜尋內容"搜尋伺服器"修改為"鋼索",同樣也能搜尋到文件,該原因會在下面講解中得到答案

{
	"query": {
		"query_string": {
		"default_field": "title",
		"query": "鋼索"
		}
	}
}

4.10 查詢文件-term查詢

請求url:

POST	localhost:9200/blog1/article/_search

請求體:

{
    "query": {
        "term": {
            "title": "搜尋"
        }
    }
}

postman截圖:
在這裡插入圖片描述

term是代表完全匹配,也就是精確查詢,搜尋前不會再對搜尋詞進行分詞,所以我們的搜尋詞必須是文件分詞集合中的一個。

相關文章