Elasticsearch索引和文檔操作執行個體教程

來源:互聯網
上載者:User
  • Elasticsearch 版本:5.4

  • Elasticsearch快速入門 第1篇:Elasticsearch入門

  • Elasticsearch快速入門 第2篇:Elasticsearch和Kibana安裝

  • Elasticsearch快速入門 第3篇:Elasticsearch索引和文檔操作

  • Elasticsearch快速入門 第4篇:Elasticsearch文檔查詢

列出所有索引

GET /_cat/indices?v

返回內容如下:

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

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

建立索引

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

PUT /customer?prettyGET /_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.sizeyellow open   .kibana  XYZPR5XGQGWj8YlyZ1et_w   1   1          1            0      3.1kb          3.1kbyellow 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?prettyGET /_cat/indices?v

第一行返回內容以下:

{  "acknowledged": true}

第二行返回內容如下:

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

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

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

PUT /customerPUT /customer/external/1{  "name": "John Doe"}GET /customer/external/1DELETE /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

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.