Elasticsearch(ES)的基本使用
1. 概述
之前聊了一下 Elasticsearch 的安裝,今天我們來說說 Elasticsearch 的基本使用。
2.?Elasticsearch索引的使用
索引(index)相當于是mysql中的表。
2.1 創建索引
1)Head插件方式
選擇 索引 頁簽,點擊【新建索引】按鈕,輸入索引名稱、分片數、副本數,點擊【OK】
?之所以集群健康值呈現黃色,是因為目前是用單服務器跑的Elasticsearch,而副本是要存儲在不同的服務器上的,之后會聊一下 Elasticsearch 集群的搭建。
2)RESTFUL接口方式
PUT? http://192.168.1.11:9200/index_user
參數:
{"settings":{"index":{"number_of_shards":5, // 分片數"number_of_replicas":0 // 副本數}} }2.2 查看集群健康狀況
RESTFUL接口方式
GET? http://192.168.1.11:9200/_cluster/health
響應:
{"cluster_name": "zhuifengren-es","status": "yellow","timed_out": false,"number_of_nodes": 1,"number_of_data_nodes": 1,"active_primary_shards": 6,"active_shards": 6,"relocating_shards": 0,"initializing_shards": 0,"unassigned_shards": 5,"delayed_unassigned_shards": 0,"number_of_pending_tasks": 0,"number_of_in_flight_fetch": 0,"task_max_waiting_in_queue_millis": 0,"active_shards_percent_as_number": 54.54545454545454 }2.3 刪除索引
1)Head插件方式
在 概覽 頁簽,找到需要刪除的索引,選擇 動作 —> 刪除...
?2)RESTFUL接口方式
DELETE? http://192.168.1.11:9200/index_user
2.4 查看集群整體信息
RESTFUL接口方式
GET? http://192.168.1.11:9200/_cat/indices?v
響應:
health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open .geoip_databases pE4IpIAeSA2AiJzdDdviYA 1 0 42 64 65.1mb 65.1mb green open index_user 2z4cELBeQeijTagp86ShbQ 5 0 0 0 1kb 1kb3.?Elasticsearch映射的使用
映射(mapping)相當于是mysql中的表結構定義。
3.1 Elasticsearch中的主要數據類型
文本類型:text,keyword
整型:long,integer,short,byte
浮點型:double,float
布爾型:boolean
日期型:date
對象型:object
3.2 創建索引并創建映射
RESTFUL接口方式
PUT??http://192.168.1.11:9200/index_user
參數:
{"settings":{"index":{"number_of_shards":5,"number_of_replicas":0}},"mappings" : {"properties":{"name":{"type":"text", // 數據類型"index":true // 是否索引},"loginName":{"type":"keyword","index":false},"age":{"type":"integer","index":false}}} }3.3?在已有的索引上維護mapping
RESTFUL接口方式
POST? http://192.168.1.11:9200/index_user/_mapping
參數:
{"properties":{"nickname":{"type":"keyword","index":false}} }注意:mapping中的屬性,只能添加,不能修改。如果屬性設置需要變更,需要刪除索引重建。
3.4 查看索引的分詞效果?
?RESTFUL接口方式
?GET??http://192.168.1.11:9200/index_user/_analyze
?參數:
{"field": "name","text": "lisa brown" }4.?Elasticsearch文檔的使用
文檔(document)相當于是mysql中的數據行。
?4.1 新增文檔
RESTFUL接口方式
POST? http://192.168.1.11:9200/index_user/_doc/1?
注:url中最后的1是文檔在Elasticsearch中的ID,與業務ID無關,如果不寫,則會自動生成一個隨機字符串作為文檔的ID
參數:
{"name":"zhang san","loginName":"zs","age":30 }如果沒有手動創建 映射(mapping),則新增文檔后,Elasticsearch會根據文檔的字段類型自動創建 映射(mapping)。
4.2 刪除文檔
RESTFUL接口方式
DELETE??http://192.168.1.11:9200/index_user/_doc/1
4.3 修改文檔
RESTFUL接口方式
1)只修改部分字段
POST??http://192.168.1.11:9200/index_user/_doc/1/_update
參數:
{"doc":{"name":"zhangsan2","age":33} }2)全部替換
PUT??http://192.168.1.11:9200/index_user/_doc/1
參數:
{"name":"zhangsan","loginName":"zs","age":31 }4.4 查詢文檔
RESTFUL接口方式
1)依據文檔ID查詢
GET??http://192.168.1.11:9200/index_user/_doc/1
響應數據:
{"_index": "index_user","_type": "_doc","_id": "1","_version": 5,"_seq_no": 7,"_primary_term": 1,"found": true,"_source": {"name": "zhangsan","loginName": "zs","age": 31} }2)查詢所有
GET??http://192.168.1.11:9200/index_user/_doc/_search
響應數據:
{"took": 2,"timed_out": false,"_shards": {"total": 5,"successful": 5,"skipped": 0,"failed": 0},"hits": {"total": {"value": 2,"relation": "eq"},"max_score": 1.0,"hits": [{"_index": "index_user","_type": "_doc","_id": "_TVW-XsBNDgg-BBCeUvY","_score": 1.0,"_source": {"name": "lisi","loginName": "ls","age": 31}},{"_index": "index_user","_type": "_doc","_id": "1","_score": 1.0,"_source": {"name": "zhangsan","loginName": "zs","age": 31}}]} }?3)查詢時自定義結果集
?GET??http://192.168.1.11:9200/index_user/_doc/1?_source=name,age
?GET? http://192.168.1.11:9200/index_user/_doc/_search?_source=name,age
5. 綜述
今天簡單聊了一下 Elasticsearch 的基本使用,希望能對大家的工作有所幫助。
歡迎大家幫忙點贊、評論、加關注 :)
關注追風人聊Java,每天更新Java干貨。
總結
以上是生活随笔為你收集整理的Elasticsearch(ES)的基本使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html文字注释,css如何注释?
- 下一篇: Google's BBR拥塞控制算法模型