1. 程式人生 > >ES入門REST API

ES入門REST API

在ES中存在4種資料物件,分別是 index  ,  type ,  document  , field .   其跟我們熟悉的關係型資料庫得二維表得對應關係為:

index -> table表 ,  document -> row行 , field -> column列, type無對應得關係,它為index得一種邏輯分類.

ES使用 index 為單元來組織資料(document),一個index可以有一個或者多個type,document為最基礎得資料單元,

document中得資訊儲存在欄位(field)中.

下面梳理出幾個入門級得簡單得curl簡單使用。

1、檢視叢集情況網頁地址: 

http://master_node_ip:9100/

2、檢視叢集得健康狀態:

curl -XGET 'localhost:9200/_cat/health?v'

3、檢視叢集的節點數目和主節點等資訊

curl -XGET localhost:9200/_cat/nodes?v'

4、新建一個索引

curl -XPUT 'localhost:9200/jim/?pretty'

5、檢視索引得setting及mapping

curl -XGET 'localhost:9200/jim/_settings?pretty'
curl -XGET 'localhost:9200/jim/_mappings?pretty'

6、新增document

curl -XPUT 'localhost:9200/jim/firstme/1?pretty' -d '{
    "firstname":        "LoadL",
    "lastname":         "Lee",
    "age":              27,
    "on_line_date":    "2018-11-11",
    "hometown":         "DB",
    "nowlive":          "BeiJing",
    "married":          false,
    "about":            "I love Beijing Opera"
}'

7、檢視是否存在某個document

curl -i -XHEAD 'localhost:9200/jim/firstme/1'
返回200為存在,返回404為不存在

8、獲取一個document

curl -XGET 'localhost:9200/jim/firstme/1?pretty'
"_source"欄位中儲存的是Document內部的資料

9、更新document

curl -XPUT 'localhost:9200/jim/firstme/1?pretty' -d '{
    "firstname":        "LoadL",
    "lastname":         "Lee",
    "age":              27,
    "on_line_date":    "2018-11-11",
    "hometown":         "HeiLongJiang",
    "nowlive":          "BeiJing",
    "married":          false,
    "about":            "I love Beijing Opera"
}'
更新完成後,該document得version會加1

10、刪除document

curl -XDELETE 'localhost:9200/jim/firstme/1?pretty'

11、刪除index

curl -XDELETE 'localhost:9200/jim/?pretty'