Elasticsearch入門 - 簡單使用

開篇

在咱們瞭解過 Elasticsearch 的概念,而且在咱們的機器上安裝過 es 後, 咱們就能夠簡單操做一下。node

咱們前面提到 ES 是基於一個搜索庫開發的,提供了大量 RESTful API 接口,所以咱們能夠直接使用 curl 命令 或者 postman 這樣的客戶端去訪問這些接口。docker

集羣情況檢查

es 提供了一 套api,叫作 cat api,能夠查看 es 中各類各樣的數據json

GET http://localhost:9200/_cat/health?vapi

epoch timestamp cluster status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent

1580201783 08:56:23  docker-cluster yellow          1         1      3   3    0    0        3             0                  -            50.0%

上面查詢出來的信息有不少,咱們暫時只關注 status 以及其對應的值: yellow。curl

如何快速瞭解集羣的健康情況?

status 對應了三個值,分別是 green,yellow,red,他們分別表明了集羣的三種健康狀態。post

  • green

每一個索引的primary shard和replica shard都是active狀態的學習

  • yellow

每一個索引的primary shard都是active狀態的,可是部分replica shard不是active狀態,處於不可用的狀態url

  • red

不是全部索引的 primary shard 都是 active 狀態的,部分索引有數據丟失了code

索引操做

查看索引索引

GET http://localhost:9200/_cat/indices?v

建立索引

PUT http://localhost:9200/test_index?pretty

刪除索引

DELETE http://localhost:9200/test_index?pretty

商品 document 的增刪改查

  1. 新增商品

模版: PUT /index/type/id

請求體:

{
  "json數據"
}

添加一條商品數據 PUT http://localhost:9200/ecommerce/product/1

請求體:

{
    "name" : "黑人牙膏",
    "desc" :  "薄荷味",
    "price" :  30
}

提示: es 會自動創建 index 和 type,不須要提早建立,並且 es 默認會對 document 每一個field都創建倒排索引(後面會介紹),讓其能夠被搜索

  1. 查詢商品

模版: GET /index/type/id

查詢指定索引,類型,id 的商品信息 GET http://localhost:9200/ecommerce/product/1

  1. 修改商品 方式一

修改商品信息 PUT http://localhost:9200/ecommerce/product/1

請求體:

{
    "name" : "高露潔牙膏",
    "desc" :  "清潔口氣",
    "price" :  30
}

這種方式有一個缺點,必須帶上全部的field,才能去進行信息的修改

  1. 修改商品 方式二

修改商品信息 POST http://localhost:9200/ecommerce/product/1/_update

請求體:

{
  "doc": {
    "name": "雲南白藥牙膏"
  }
}
  1. 刪除商品

刪除一個商品信息 DELETE http://localhost:9200/ecommerce/product/1

總結

Elasticsearch 提供的檢索功能很是複雜,這裏介紹的是最基本的使用方法,後面會深刻講解學習。

相關文章
相關標籤/搜索