Elasticsearch索引和文檔操作


列出所有索引

GET /_cat/indices?v

返回內容如下:

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana XYZPR5XGQGWj8YlyZ1et_w   1   1          1            0      3.1kb          3.1kb

可以看到在集群中有一個索引

創建索引

現在讓我們創建一個名叫 customer 的索引,然后再次列出所有的索引

PUT /customer?pretty
GET /_cat/indices?v

執行第一行返回以下內容,這里我們使用PUT謂詞創建了一個名叫 customer 的索引,在后面跟上 pretty 表示如果有數據返回的話,用格式化后的JSON返回數據

{
  "acknowledged": true,
  "shards_acknowledged": true
}

執行第二行返回以下內容,結果告訴我們,已經創建了一個名叫 customer  的索引,它有5個主分片和1個復制分片(默認情況下是1個),在這個索引中還沒有文檔。

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana  XYZPR5XGQGWj8YlyZ1et_w   1   1          1            0      3.1kb          3.1kb
yellow open   customer M8i1ZxhsQJqk7HomOA7c_Q   5   1          0            0       650b           650b

可能你已經注意到 customer 索引的健康值被標記為 yellow ,回顧我們前面討論的內容, yellow 表示該索引的復制分片(副本)還沒有被分配。該索引出現這種情況的原因是, Elasticsearch 默認會為該索引創建1個副本,由於此時我們只有1個節點,那么這副本就沒法被分配(為了高可用),直到以后為該集群加入了另一個節點。一旦該副本分配到了另一個節點,該索引的健康狀態就會變成 green 。

索引和查詢文檔

接下來我們放一些東西到 customer  索引中。之前提過的,為了索引某個文檔,我們必須告訴 Elasticsearch  ,該文檔應該屬於該索引的哪個類型,下面我們索引一個簡單的文檔到 customer  索引,類型名稱為 external ,  並且ID為1

PUT /customer/external/1?pretty
{
  "name": "John Doe"
}

返回內容如下:

{
  "_index": "customer",
  "_type": "external",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

從以上可以看出,一個新的客戶文檔成功被索引到 customer索引的 extenal 類型中,並且我們在索引的時候指定文檔的內部id值為1。

值得注意的是, Elasticsearch 不需要在你索引文檔到某個索引之前,明確的創建一個索引。比如上一個例子,如果 customer索引不存在, Elasticsearch將自動創建該索引。

再來看下我們剛剛索引的文檔

GET /customer/external/1?pretty

返回內容如下:

{
  "_index": "customer",
  "_type": "external",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "name": "John Doe"
  }
}

這里比較特殊的是found字段,它說明我們查到了一個id為1的文檔,另一特殊的字段_source,保存了在上一個步驟索引的的文檔。

刪除索引

現在讓我們刪除剛剛已經創建的索引,並再次查看所有索引。

DELETE /customer?pretty
GET /_cat/indices?v

第一行返回內容以下:

{
  "acknowledged": true
}

第二行返回內容如下:

health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   .kibana XYZPR5XGQGWj8YlyZ1et_w   1   1          1            0      3.1kb          3.1kb

從以上內容可以看到我們的 customer索引已經被刪除了。

在繼續學習之前,讓我們快速回顧一下,本節學的API命令

PUT /customer
PUT /customer/external/1
{
  "name": "John Doe"
}
GET /customer/external/1
DELETE /customer

如果仔細學習了以上命令,應該會發現 elasticsearch 訪問數據所使用的模式,概括如下:

<REST Verb> /<Index>/<Type>/<ID>

使用REST 訪問模式,在所有的API命令中是十分普遍的,如果你可以簡單記住它,對於掌握 Elasticsearch ,那么已經開了一個好頭。

修改數據

 Elasticsearch  具有近實時的操作和查詢數據的能力,默認情況下,從你索引,更新或者刪除你的數據到用戶可以搜索到新的結果這個過程大概需要1秒(基於refresh 頻率)。它們和類似SQL這樣的平台不一樣,SQL的數據在事務完成后就馬上就生效,不會有延遲。

索引/替換文檔

之前已經演示了怎么索引單個文檔,再來回顧一下:

PUT /customer/external/1?pretty
{
  "name": "John Doe"
}

上面的命令將會索引指定文檔到 customer 索引的 external 類型,文檔的id值是1。如果我們用不同的文檔內容(或者相同)再次執行上面的命令,elasticsearch將會用一個新的文檔取代舊的文檔(即重建索引)。

PUT /customer/external/1?pretty
{
  "name": "Jane Doe"
}

上面的操作把id為1的文檔的name字段由"john doe"改成"jane doe"。另一方面,如果我們使用不同的id執行上述命令,將會創建一個新的文檔,舊的文檔會保持原樣。

PUT /customer/external/2?pretty
{
  "name": "Jane Doe"
}

以上操作索引了一個新的id為2文檔。

索引新文檔的時候,id值是可選的。如果沒有指定, elasticsearch 將會為文檔生成一個隨機的id。實際生成的id將會保存在調用api接口的返回結果中。

下面的例子展示不指定文檔id的時候是如何索引文檔的:

POST /customer/external?pretty
{
  "name": "Jane Doe"
}

返回內容如下:

{
  "_index": "customer",
  "_type": "external",
  "_id": "AVyc9L6dtgHksqXKpTlM",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 2,
    "successful": 1,
    "failed": 0
  },
  "created": true
}

注意,在上面的例子中,因為沒有指定id,我們需要使用POST謂詞取代之前的PUT謂詞。

更新文檔

除了可以索引和替換文檔之外,我們還可以更新文檔。注意, elasticsearch 並沒有在原來的文檔基礎上進行更新,每當進行更新時, Elasticsearch 將刪除舊的文檔,然后索引新的文檔。以下例子演示了如何更新文檔,把之前ID為1的name字段改為"Jane Doe":

POST /customer/external/1/_update?pretty
{
  "doc": { "name": "Jane Doe" }
}

以下例子演示了如何更新先前ID為1的文檔,改變name字段為"Jane Doe" 的同時添加age字段

POST /customer/external/1/_update?pretty
{
  "doc": { "name": "Jane Doe", "age": 20 }
}

也可以使用簡單的腳本來執行更新。以下示例使用腳本將年齡增加5:

POST /customer/external/1/_update?pretty
{
  "script" : "ctx._source.age += 5"
}

在以上例子中, ctx._source 指當前即將被更新的源文檔。請注意,在撰寫本文時,只能一次更新單個文檔。將來, Elasticsearch 可能會提供通過查詢條件(如SQL UPDATE-WHERE語句)更新多個文檔的功能。

刪除文檔

刪除文檔非常簡單,以下例子演示了怎么刪除 customer 索引下ID為2的文檔,查閱Delete By Query API 刪除與特定查詢匹配的所有文檔。值得注意的是,直接刪除整個索引比通過query api 刪除所有文檔更高效。

DELETE /customer/external/2?pretty

批處理

除了能夠索引,更新和刪除單個文檔之外, Elasticsearch  也提供了使用  _bulk API 批量執行上述任何操作的功能這個功能是非常重要的,因為它提供了一個非常有效的機制來盡可能快地進行多個操作,並且盡可能減少網絡的往返行程。簡單舉個例子,下面會在一個 bulk操作中索引兩個文檔:

POST /customer/external/_bulk?pretty
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }

返回內容如下:

{
  "took": 27,
  "errors": false,
  "items": [
    {
      "index": {
        "_index": "customer",
        "_type": "external",
        "_id": "1",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    },
    {
      "index": {
        "_index": "customer",
        "_type": "external",
        "_id": "2",
        "_version": 1,
        "result": "created",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "created": true,
        "status": 201
      }
    }
  ]
}

下面的例子會在一個操作內更新第一個文檔同時刪除第二個文檔:

POST /customer/external/_bulk?pretty
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}

返回內容如下:

{
  "took": 25,
  "errors": false,
  "items": [
    {
      "update": {
        "_index": "customer",
        "_type": "external",
        "_id": "1",
        "_version": 2,
        "result": "updated",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "status": 200
      }
    },
    {
      "delete": {
        "found": true,
        "_index": "customer",
        "_type": "external",
        "_id": "2",
        "_version": 2,
        "result": "deleted",
        "_shards": {
          "total": 2,
          "successful": 1,
          "failed": 0
        },
        "status": 200
      }
    }
  ]
}

注意以上的刪除操作,在它之后並沒有相應的源文檔,因為只需要文檔的ID就能刪除。

如果某個操作因某些原因執行失敗,不會影響后面的操作,它會繼續執行剩下的操作。api返回結果時,每一個操作都會提供狀態(和接收到的順序一致),你可以通過這個狀態檢查操作是否執行成功。

總結

簡單的索引操作

1、查看集群中的索引, GET /_cat/indices?v 

2、創建索引 PUT /product?pretty 。(es會自動建立index和type,不需要提前創建,而且es默認會對document每個field都建立倒排索引,讓其可以被搜索)

3、刪除索引, DELETE /test_index?pretty 

文檔的CRUD操作

1、新增商品

PUT /product/goods/1
{
    "goods_id": "10",
    "goods_name": "索愛C702c",
    "createTime": "2016-12-21",
    "goods_type": [
        "華為",
        "樂視",
        "小米"
    ]
}

2、查詢商品, GET /product/goods/1 

3、修改商品

方式1:替換文檔(和創建一樣,所有字段必須寫全)

PUT /product/goods/4
{
    "goods_id": "40",
    "goods_name": "聯想筆記本",
    "createTime": "2017-05-21",
    "goods_type": [
        "電腦"
    ]
}

字段不寫全的情況

 方式2:更新文檔

POST /product/goods/1/_update
{
  "doc":{
    "goods_name":"iphone手機"
  }
}

比較創建,更新,替換文檔返回結果:

4、刪除商品, DELETE /product/goods/4 

官方文檔

https://www.elastic.co/guide/en/elasticsearch/reference/current/_exploring_your_cluster.html

https://www.elastic.co/guide/en/elasticsearch/reference/current/_modifying_your_data.html

參考文檔

https://github.com/13428282016/elasticsearch-CN/wiki/es-gettting-started


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM