Elasticsearch 6 入門教程之ElasticSearch API 實現CRUD

系列文章

用ElasticSearch API 實現CRUD

添加索引:

PUT /lib/
{
	"settings":{
	"index":{  
		"number_of_shards": 5,
        "number_of_replicas": 1
    }    
  }
}

查看索引信息:

  • GET /lib/_settings
  • GET _all/_settings

添加文檔:

PUT /lib/user/1
{ 
    "first_name" : "Jane",
    "last_name" :   "Smith",
    "age" :         32,
    "about" :       "I like to collect rock albums",
    "interests":  [ "music" ]
}

POST /lib/user/
{     
    "first_name" : "Douglas",
    "last_name" :   "Fir",
    "age" :         23,
    "about":        "I like to build cabinets",
    "interests":  [ "forestry" ]
}

查看文檔:

  • GET /lib/user/1
  • GET /lib/user/
  • GET /lib/user/1?_source=age,interests

更新文檔:

PUT /lib/user/1
{ 
	"first_name" : "Jane",
	"last_name" :   "Smith",
	"age" :         36,
	"about" :       "I like to collect rock albums",
	"interests":  [ "music" ]
}

POST /lib/user/1/_update
{
	"doc":{
		"age":33
	}
}

刪除一個文檔

  • DELETE /lib/user/1

刪除一個索引

  • DELETE /lib

批量獲取文檔

使用es提供的Multi Get API:

使用Multi Get API可以通過索引名、類型名、文檔id一次得到一個文檔集合,文檔可以來自同一個索引庫,也可以來自不同索引庫

使用curl命令:

curl 'http://192.168.25.131:9200/_mget' -d '{
"docs":[
	{
		"_index": "lib",
		"_type": "user",
		"_id": 1
	},
	{
		"_index": "lib",
		"_type": "user",
		"_id": 2
	}
]}'

在客戶端工具中:

GET /_mget
{
    "docs":[   
    {
        "_index": "lib",
        "_type": "user",
        "_id": 1
   },
   {
        "_index": "lib",
        "_type": "user",
        "_id": 2
   },
   {
        "_index": "lib",
        "_type": "user",
        "_id": 3
   }]
}

可以指定具體的字段:

GET /_mget
{
    "docs":[   
   {
       "_index": "lib",
       "_type": "user",
       "_id": 1,
       "_source": "interests"
   },
   {
       "_index": "lib",
       "_type": "user",
       "_id": 2,
       "_source": ["age","interests"]
   }]
}

獲取同索引同類型下的不同文檔:

GET /lib/user/_mget
{
    "docs":[   
   {
       "_id": 1
   },
   {
       "_type": "user",
       "_id": 2,
   }]
}

GET /lib/user/_mget
{
    "ids": ["1","2"]
}

使用Bulk API 實現批量操作

bulk的格式:

  • {action:{metadata}}\n
  • {requstbody}\n
  • action:(行爲)
  • create:文檔不存在時創建
  • update:更新文檔
  • index:創建新文檔或替換已有文檔
  • delete:刪除一個文檔
  • metadata:_index,_type,_id
  • create 和index的區別

如果數據存在,使用create操作失敗,會提示文檔已經存在,使用index則可以成功執行。

示例:

  • {"delete":{"_index":"lib","_type":"user","_id":"1"}}

批量添加:

  • POST /lib2/books/_bulk
  • {"index":{"_id":1}}
  • {"title":"Java","price":55}
  • {"index":{"_id":2}}
  • {"title":"Html5","price":45}
  • {"index":{"_id":3}}
  • {"title":"Php","price":35}
  • {"index":{"_id":4}}
  • {"title":"Python","price":50}

批量獲取:

  • GET /lib2/books/_mget {
  • "ids": ["1","2","3","4"] }

刪除:沒有請求體

  • POST /lib2/books/_bulk
  • {"delete":{"_index":"lib2","_type":"books","_id":4}}
  • {"create":{"_index":"tt","_type":"ttt","_id":"100"}}
  • {"name":"lisi"}
  • {"index":{"_index":"tt","_type":"ttt"}}
  • {"name":"zhaosi"}
  • {"update":{"_index":"lib2","_type":"books","_id":"4"}}
  • {"doc":{"price":58}}

bulk一次最大處理多少數據量:

bulk會把將要處理的數據載入內存中,所以數據量是有限制的,最佳的數據量不是一個確定的數值,它取決於你的硬件,你的文檔大小以及複雜性,你的索引以及搜索的負載。

一般建議是1000-5000個文檔,大小建議是5-15MB,默認不能超過100M,可以在es的配置文件(即$ES_HOME下的config下的elasticsearch.yml)中。

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