Elasticsearch 2.20入門篇:基本操作

jqvj9967 8年前發布 | 13K 次閱讀 ElasticSearch 搜索引擎

來自: http://my.oschina.net/secisland/blog/613927


    前面我們已經安裝了Elasticsearch ,下一步我們要對Elasticsearch進行一些基本的操作。基本的操作主要有,建索引庫,插入數據,查詢數據,修改數據,刪除數據,刪除索引庫。

    備注:如果沒有特殊說明,本文章及后面所有的文章都在2.20版本中進行驗證,其他版本不能確定是否可用。

    由于官方文檔都是使用curl來進行實例操作,不太直觀,我更喜歡用圖形化界面來進行驗證。在本文及以后的例子中,我都是已RESTClient3.5來作為操作的工具。下載地址為http://code.fosshub.com/WizToolsorg-RESTClient/downloads,下載的文件是restclient-ui-3.5-jar-with-dependencies.jar。

程序運行: java -jar restclient-ui-3.5-jar-with-dependencies.jar

建索引庫

執行PUT localhost:9200/customer?pretty

返回表示建庫成功:

{
  "acknowledged" : true
}

說明:http方法PUT,url為localhost:9200/customer?pretty

查詢庫

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

返回:

health status   index      pri  rep       docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          0                 0                   795b        795b 

表示已經建成了一個索引customer,主分片是5個,健康度是黃色,狀態是活動,文檔數為0。

插入數據

執行 PUT localhost:9200/customer/external/1?pretty

參數:{  "name": "John Doe" }

注意:Method選擇PUT,Body要設置成application/x-www-form-urlencoded; charset=UTF-8

返回值為:

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

界面如下:

我們再次執行庫查詢,發現文檔數是1:GET http://localhost:9200/_cat/indices?v  

health status  index      pri rep        docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          1            0      3.5kb          3.5kb 

查詢數據

執行:GET http://localhost:9200/customer/external/1?pretty

返回:

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

可以看到_source的內容就是我們剛才插入的數據。

本文由賽克藍德(secisland)原創,轉載請標明作者和出處。

修改數據

執行:POST localhost:9200/customer/external/1/_update?pretty

參數:

{
  "doc": { "name": "secisland Doe" }
}

返回結果:

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

表示執行成功。

然后我們在查詢一下數據

GET http://localhost:9200/customer/external/1?pretty

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

可以看出文檔的內容由John Doe修改成了secisland Doe。

刪除文檔

執行:DELETE localhost:9200/customer/external/1?pretty

返回:

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

然后我們查詢庫的狀態:

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

返回:

health status index    pri rep docs.count docs.deleted store.size pri.store.size 

yellow open   customer   5   1          0            0      3.6kb          3.6kb 

從中可以看出,數據庫中已經沒有記錄了。

刪除索引庫

執行:DELETE localhost:9200/customer?pretty

返回:

{
  "acknowledged" : true
}

表示刪除成功

然后我們查詢庫的狀態:

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

返回:

health status index pri rep docs.count docs.deleted store.size pri.store.size 

從中可以看出已經沒有任何的庫了。

賽克藍德(secisland)后續會逐步對Elasticsearch的最新版本的各項功能進行分析,近請期待。

 本文由用戶 jqvj9967 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!