ES的Rest风格
什么是REST
REST是一種軟件架構(gòu)風(fēng)格,或者說是一種規(guī)范,其強(qiáng)調(diào)HTTP應(yīng)當(dāng)以資源為中心,并且規(guī)范了URI的風(fēng)格;規(guī)范了HTTP請求動(dòng)作(GET/PUT/POST/DELETE/HEAD/OPTIONS)的使用,具有對應(yīng)的語義。
核心概念包括:
| PUT | localhost:9200/索引名稱/類型名稱/文檔id | 創(chuàng)建文檔(指定文檔 id) |
| POST | localhost:9200/索引名稱/類型名稱 | 創(chuàng)建文檔(隨機(jī)文檔 id) |
| POST | localhost:9200/索引名稱/類型名稱/文檔id/_update | 修改文檔 |
| DELETE | localhost:9200/索引名稱/類型名稱/文檔id | 刪除文檔 |
| GET | localhost:9200/索引名稱/類型名稱/文檔id | 查詢文檔通過文檔 id |
| POST | localhost:9200/索引名稱/類型名稱/_search | 查詢所有數(shù)據(jù) |
基礎(chǔ)測試
創(chuàng)建一個(gè)索引
PUT /索引名/類型名/文檔 id
{
請求體
}
返回結(jié)果
#! Deprecation: [types removal] Specifying types in document index requests is deprecated, use the typeless endpoints instead (/{index}/_doc/{id}, /{index}/_doc, or /{index}/_create/{id}). {"_index" : "test1","_type" : "type1","_id" : "1","_version" : 2,"result" : "updated","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 1,"_primary_term" : 1 }數(shù)據(jù)類型
(1)字符串類型: text, keyword (2)數(shù)字類型:long, integer, short, byte, double, float, half_float, scaled_float (3)日期:date (4)日期 納秒:date_nanos (5)布爾型:boolean (6)二進(jìn)制類型 Binary:binary (7)Range: integer_range, float_range, long_range, double_range, date_range創(chuàng)建具體的索引規(guī)則
PUT /test2 {"mappings": {"properties": {"name": {"type": "text"}, "age": {"type": "long"},"birthday": {"type": "date"}}} }返回結(jié)果
{"acknowledged" : true,"shards_acknowledged" : true,"index" : "test2" }GET 獲取具體信息
獲取這個(gè)規(guī)則, 可以通過 GET 獲取具體信息
GET test2返回結(jié)果
{"test2" : {"aliases" : { },"mappings" : {"properties" : {"age" : {"type" : "long"},"birthday" : {"type" : "date"},"name" : {"type" : "text"}}},"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "test2","creation_date" : "1610936976952","number_of_replicas" : "1","uuid" : "qqzV0yIqR7CqT_liFewR4Q","version" : {"created" : "7100199"}}}} }查看默認(rèn)的信息
PUT /test3/_doc/1 {"name": "全棧自學(xué)社區(qū)","age": 18,"birth": "1997-01-25" }返回結(jié)果
{"_index" : "test3","_type" : "_doc","_id" : "1","_version" : 1,"result" : "created","_shards" : {"total" : 2,"successful" : 1,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1 } GET test3 {"test3" : {"aliases" : { },"mappings" : {"properties" : {"age" : {"type" : "long"},"birth" : {"type" : "date"},"name" : {"type" : "text","fields" : {"keyword" : {"type" : "keyword","ignore_above" : 256}}}}},"settings" : {"index" : {"routing" : {"allocation" : {"include" : {"_tier_preference" : "data_content"}}},"number_of_shards" : "1","provided_name" : "test3","creation_date" : "1610937747174","number_of_replicas" : "1","uuid" : "Hok1PejKSByKuW6ozyQpEA","version" : {"created" : "7100199"}}}} }如果自己的文檔沒有指定, 那么es就會(huì)給我們默認(rèn)配置字段類型
其他命令
獲取 es 當(dāng)前的信息
GET _cat/indices?v返回結(jié)果
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size yellow open test2 qqzV0yIqR7CqT_liFewR4Q 1 1 0 0 208b 208b yellow open test3 Hok1PejKSByKuW6ozyQpEA 1 1 1 0 4.3kb 4.3kb green open .apm-custom-link A-jNeoZWTDKtQqpf_Kqqog 1 0 0 0 208b 208b yellow open xinze Gn2y36z2Tpq2jvxBhM0ogg 5 1 0 0 1kb 1kb green open .kibana_task_manager_1 STLFw_63S6a_t5HBC5fW9w 1 0 5 157 153.5kb 153.5kb green open .apm-agent-configuration bZJ-56bwQFKQekHd5GFdgQ 1 0 0 0 208b 208b green open .kibana-event-log-7.10.1-000001 HUjThpyTQMeJr-QSwMXNLQ 1 0 3 0 16.4kb 16.4kb green open .kibana_2 hJZflBGSSkqIgWWX_15Ofg 1 0 48 69 4.2mb 4.2mb yellow open test1 a8pjIRrfTsqtTmaAaXRZKg 5 1 1 1 6.2kb 6.2kb green open .kibana_1 Gi93gw00SzSFVN5dQxrLig 1 0 1 0 5.1kb 5.1kb green open .tasks UDpsMCO1SyasUGAJTqxFgw 1 0 1 0 6.9kb 6.9kb修改索引
依舊使用 PUT 然后覆蓋
刪除索引
DELETE test1文章已上傳gitee https://gitee.com/codingce/hexo-blog
項(xiàng)目地址: https://github.com/xzMhehe/codingce-java
總結(jié)
- 上一篇: IK分词器详解
- 下一篇: ES关于文档的基本操作