全部其餘語言可使用 RESTful API 經過端口 9200 和 Elasticsearch 進行通訊,你可使用WEB客戶端或者curl命令來和ES交互。下面介紹用curl命令來交互的具體用法:node
curl -X<VERB> '<PROTOCOL>://<HOST>:<PORT>/<PATH>?<QUERY_STRING>' -d '<BODY>'
被 < > 標記的部件含義:
VERB
適當的 HTTP 方法 或 謂詞 : GET、 POST、 PUT、 HEAD 或者 DELETE。apache
PROTOCOL
http 或者 https(若是你在 Elasticsearch 前面有一個 https 代理)dom
HOST
Elasticsearch 集羣中任意節點的主機名,或者用 localhost 表明本地機器上的節點。curl
PORT
運行 Elasticsearch HTTP 服務的端口號,默認是 9200 。jvm
PATH
API 的終端路徑(例如 _count 將返回集羣中文檔數量)。Path 可能包含多個組件,例如:_cluster/stats 和 _nodes/stats/jvm 。ide
QUERY_STRING
任意可選的查詢字符串參數 (例如 ?pretty 將格式化地輸出 JSON 返回值,使其更容易閱讀)fetch
BODY
一個 JSON 格式的請求體 (若是請求須要的話)ui
往本地ES中寫入一條數據,索引名稱爲"test_log-2018-10-24",類型名稱爲"test_log":this
curl -XPOST http://127.0.0.1:9200/test_log-2018-10-24/test_log -d '{ "date":"1540369436", "user":"test", "mesg":"this is a test message" }' #返回: {"_index":"test_log-2018-10-24","_type":"test_log","_id":"AWalLVzvZ_iZW0h4u8UH","_version":1,"result":"created","_shards":{"total":1,"successful":1,"failed":0},"created":true}
從本地ES中獲取一條數據,索引名稱爲"test_log-2018-10-24",類型名稱爲"test_log",id名稱爲"AWalLVzvZ_iZW0h4u8UH":url
curl -XGET http://127.0.0.1:9200/test_log-2018-10-24/test_log/AWalLVzvZ_iZW0h4u8UH #返回 {"_index":"test_log-2018-10-24","_type":"test_log","_id":"AWalLVzvZ_iZW0h4u8UH","_version":1, "found":true,"_source":{ "date":"1540369436", "user":"test", "mesg":"this is a test message" }}
刪除剛纔插入的數據:
curl -XDELETE http://127.0.0.1:9200/test_log-2018-10-24/test_log/AWalLVzvZ_iZW0h4u8UH #返回 {"found":true,"_index":"test_log-2018-10-24","_type":"test_log","_id":"AWalLVzvZ_iZW0h4u8UH","_version":2,"result":"deleted","_shards":{"total":1,"successful":1,"failed":0}}
注意:刪除數據,不但針對單條數據,還可使用通配符刪除整個type乃至整個索引
例如:
curl -XDELETE 'http://127.0.0.1:9200/*-${DATA}'
局部更新
curl -XPOST 'http://127.0.0.1:9200/test_log-2018-10-24/test_log/AWalXZ4cZ_iZW0h4vlQj/_update' -d '{ "doc":{ "user":"fjc" } }' #返回 {"_index":"test_log-2018-10-24","_type":"test_log","_id":"AWalXZ4cZ_iZW0h4vlQj","_version":2,"result":"updated","_shards":{"total":1,"successful":1,"failed":0}}
全文搜索
curl -XGET http://127.0.0.1:9200/test_log-2018-10-24/test_log/_search?q=test curl -XGET http://127.0.0.1:9200/test_log-2018-10-24/test_log/_search?q=mesg:test
querystring語法(上例中,?q=後面寫的就是querystring語法):
全文檢索:直接寫搜索的單詞,例如加上例中的test
單字段的全文檢索:在搜索單詞前加上字段名和冒號,好比,若是知道單詞test確定出如今mesg字段,能夠寫做mesg:test
單字段的精確檢索:在搜索單詞先後加上雙引號,好比user:"fjc"
多個檢索條件的組合:可使用NOT、AND和OR來組合檢索,注意必須是大寫
字段是否存在:exists:user表示要求user字段存在,missing:user表示要求user字段不存在
通配符:用?表示單字母,*表示任意個字母
近似搜索:用~表示搜索單詞可能有一兩個字母寫得不對,請Elasticsearch按照類似度返回結果。好比test~
範圍搜索:對數值和時間,Elasticsearch均可以使用範圍搜索,好比:rtt>300、date:["now-6h" TO "now"}等。其中,[]表示端點數值包含在範圍內,{}表示端點數值不包含在範圍內
獲取集羣健康狀態
curl -XGET 'http://127.0.0.1:9200/_cluster/health?pretty' Enter host password for user 'logview': { "cluster_name" : "idca-elk", "status" : "green", "timed_out" : false, "number_of_nodes" : 3, "number_of_data_nodes" : 3, "active_primary_shards" : 141, "active_shards" : 277, "relocating_shards" : 0, "initializing_shards" : 0, "unassigned_shards" : 0, "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" : 100.0 }
獲取節點狀態
curl -XGET 'http://127.0.0.1:9200/_cluster/state/nodes?pretty' Enter host password for user 'logview': { "cluster_name" : "idca-elk", "nodes" : { "xSTX8HulT7eLrHtx8dpVbg" : { "name" : "elk02", "ephemeral_id" : "iql7vwvBSWSyx7PrsUMAGg", "transport_address" : "172.16.1.26:9300", "attributes" : { } }, "gvlGkHa9QcyIsz31PjyZaw" : { "name" : "elk03", "ephemeral_id" : "9Gxyu9XrSSGNIPWKDAsfpw", "transport_address" : "172.16.1.27:9300", "attributes" : { } }, "GVf_Yo-9RBW9_oUrqIPOZw" : { "name" : "elk01", "ephemeral_id" : "ZjAUQHGYS0KUMT1iiGS4Jg", "transport_address" : "172.16.1.25:9300", "attributes" : { } } } }
獲取主節點狀態
Enter host password for user 'logview': { "cluster_name" : "idca-elk", "master_node" : "GVf_Yo-9RBW9_oUrqIPOZw" }
顯示集羣的詳細信息
curl -XGET 'http://127.0.0.1:9200/_cluster/stats?human&pretty'
查看主節點
curl -XGET 'http://127.0.0.1:9200/_cat/master?pretty&v' Enter host password for user 'logview': id host ip node GVf_Yo-9RBW9_oUrqIPOZw 172.16.1.25 172.16.1.25 elk01
查看全部索引
curl -XGET 'http://127.0.0.1:9200/_cat/indices?v' health status index uuid pri rep docs.count docs.deleted store.size pri.store.size green open apache-2018-10-26 bksy7QOCQQyHcBazjB5TEg 5 1 443452 0 259.4mb 129.7mb green open user_audit-2018-10-26 41PM-4WpRJ6Evhar0dREsA 5 1 95 0 575.2kb 287.6kb green open domain_log-2018-10-24 d8zG7XL3RtyvyqAIJp7GvQ 5 1 11307735 0 11.4gb 5.7gb
查看單節點健康狀態
curl -XGET 'http://127.0.0.1:9200/_cat/health?v' epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent 1540530477 13:07:57 idca-elk green 3 3 277 141 0 0 0 0 - 100.0%
查看分片信息
curl -XGET 'http://127.0.0.1:9200/_cat/shards?v' index shard prirep state docs store ip node domain_log-2018-10-22 3 r STARTED 1975611 1gb 172.16.1.27 elk03 domain_log-2018-10-22 3 p STARTED 1975611 1gb 172.16.1.25 elk01 domain_log-2018-10-22 2 p STARTED 1979052 1gb 172.16.1.26 elk02
查看插件信息
curl -XGET 'http://127.0.0.1:9200/_cat/plugins?v