實際開發中,主要有三種方式能夠做爲elasticsearch服務的客戶端:
第一種,elasticsearch-head插件(可視化工具)java
第二種,使用elasticsearch提供的Restful接口直接訪問(僅僅用戶學習測試)node
第三種,使用elasticsearch提供的API進行訪問,使用JavaAPI去訪問ES!(實際開發)web
Postman中文版是postman這款強大網頁調試工具的windows客戶端,提供功能強大的Web API & HTTP 請求調試。軟件功能很是強大,界面簡潔明晰、操做方便快捷,設計得很人性化。Postman中文版可以發送任何類型的HTTP 請求 (GET, HEAD, POST, PUT..),且能夠附帶任何數量的參數。json
Postman官網:https://www.getpostman.com
windows
curl ‐X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' ‐d '<BODY>
其中:api
參數 | 解釋 |
---|---|
VERB |
適當的 HTTP 方法 或 謂詞 : GET 、 POST 、 PUT 、 HEAD 或者 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 格式的請求體 (若是請求須要的話) |
請求url:服務器
PUT localhost:9200/blog1
請求體:restful
若是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查看:
app
咱們能夠在建立索引時設置mapping信息,固然也能夠先建立索引而後再設置mapping。
在上一個步驟中不設置maping信息,直接使用put方法建立一個索引,而後設置mapping信息。
請求的url:curl
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截圖
請求url:
DELETE localhost:9200/blog1
postman截圖:
elasticsearch-head查看:
請求url:
POST localhost:9200/blog1/article/1
請求體:
{ "id":1, "title":"ElasticSearch是一個基於Lucene的搜索服務器", "content":"它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並做爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,可以達到實時搜索,穩定,可靠,快速,安裝使用方便。" }
postman截圖:
elasticsearch-head查看:
請求url:
POST localhost:9200/blog1/article/1
請求體:
{ "id":1, "title":"【修改】ElasticSearch是一個基於Lucene的搜索服務器", "content":"【修改】它提供了一個分佈式多用戶能力的全文搜索引擎,基於RESTful web接口。Elasticsearch是用Java開發的,並做爲Apache許可條款下的開放源碼發佈,是當前流行的企業級搜索引擎。設計用於雲計算中,可以達到實時搜索,穩定,可靠,快速,安裝使用方便。" }
postman截圖:
elasticsearch-head查看:
請求url:
DELETE localhost:9200/blog1/article/1
postman截圖:
elasticsearch-head查看:
請求url:
GET localhost:9200/blog1/article/1
postman截圖:
請求url:
POST localhost:9200/blog1/article/_search
請求體:
{ "query": { "query_string": { "default_field": "title", "query": "搜索服務器" } } }
postman截圖:
注意:
將搜索內容"搜索服務器"修改成"鋼索",一樣也能搜索到文檔,該緣由會在下面講解中獲得答案
{ "query": { "query_string": { "default_field": "title", "query": "鋼索" } } }
請求url:
POST localhost:9200/blog1/article/_search
請求體:
{ "query": { "term": { "title": "搜索" } } }
postman截圖:
term是表明徹底匹配,也就是精確查詢,搜索前不會再對搜索詞進行分詞,因此咱們的搜索詞必須是文檔分詞集合中的一個。