第四章:elasticsearch基本操作

一.索引

    在Elasticsearch中存儲數據的行爲就叫做索引(indexing),ES中所有相當於傳統的數據庫,type相當數據表,如下圖對比

    Relational DB -> Databases -> Tables -> Rows -> Columns
    Elasticsearch -> Indices -> Types -> Documents -> Fields

   創建一個索引:使用 PUT 進行創建

   PUT /lib/       // lib :索引名
   {
      "settings":{
       "index":{
            "number_of_shards":3,  //數據分片,不設置默認是5
      
            "number_of_replicas":0  //數據備份
           }
         }
     }

查詢索引:使用 GET 查詢

GET /lib/_settings   //查詢lib的索引

GET _all/_settings  //查詢所有的所用

刪除索引 : 使用 DELETE 

DELETE  lib   //刪除lib的索引

 

二.類型,文檔  

  創建: 使用 PUT或POST 創建類型

  PUT /lib/user/1  //user :是類型  1:這個類型id

  PUT /lib/user/1
{
  "first_name":"kale",
  "last_name":"mile",
  "age":22,
  "about":"I like to rocke",
  "interests":["music"]
}

POST /lib/user/  //使用POST生成不要指定id,ES會自動生成id
{
  "first_name":"kale",
  "last_name":"mile",
  "age":22,
  "about":"I like to rocke",
  "interests":["music"]
}

 查詢 :使用 GET

 GET lib/user/1   // 查詢user爲1

 GET lib/user/_search  //查詢user總的所有數據

  GET lib/user/1?_source=age,last_name    // 查詢user爲1,只顯示age和last_name字段

  GET /_mget    //使用_mget批量獲取文檔的數據
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":1
    },
     {
      "_index":"lib2",
      "_type":"person",
      "_id":1
    }
    ]
}

GET /_mget     //獲取指定字段的數據 加上_source
{
  "docs":[
    {
      "_index":"lib",
      "_type":"user",
      "_id":1,
      "_source":"last_name"  //加上 _source
    },
     {
      "_index":"lib2",
      "_type":"person",
      "_id":1,
       "_source":["age","last_name"]
    }
    ]
}

GET /lib/user/_mget    //在同一個文檔下批量獲取
{
  "ids":["1","2"]
}

更新 :使用 PUT 和POST

PUT /lib/user/1  //使用PUT就是把user爲1的文檔直接進行覆蓋,從而達到更新文檔
{
  "first_name":"kale",
  "last_name":"lidass",
  "age":32,
  "about":"I like to rocke",
  "interests":["music"]
}

POST /lib/user/1/_update   //POST使用修改哪個地段,就添加哪個字段
{
  "doc":{
    "age":56
  }
}

刪除文檔

DELETE  lib/user/1   //刪除user爲1的文檔

 

 

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章