1. 程式人生 > 實用技巧 >ES RESTFUL操作

ES RESTFUL操作

說明

index_name:索引名稱

_doc:文件型別,預設為__doc。ES6.0以後不建議使用

id:文件id

新增文件

POST http://ip:port/index_name/_doc/id
{
	"name":"張三",
	"age":20,
	"sex":"男",
	"mobile":"138001238000"
}

刪除文件

DELETE http://ip:port/index_name/_doc/id

注意:ES是邏輯刪除,設定一個已刪除的標記,每次操作會文件版本號會 +1

修改文件

1.修改指定的欄位

POST http://ip:port/index_name/_doc/id/_update?version=2
{
	"doc":{
		"age":18
	}
}

version=2,表示文件版本號,es的樂觀鎖。

2.全部更新,所有欄位都需要加上

PUT http://ip:port/index_name/_doc/id
{
	"name":"張小三",
	"age":18,
	"sex":"男",
	"mobile":"138001238001"
}

查詢

1.根據id查詢文件,並指定查詢的欄位name,age

GET http://ip:port/index_name/_doc/id?_source=name,age

2.查詢索引中所有文件,並指定查詢的欄位

GET http://ip:port/index_name/_doc/_search?_source=name,age

3.判斷文件是否存在

HEAD http://ip:port/index_name/_doc/id

通過http狀態碼判斷。200表示存在,404表示不存在

給文件增加欄位

增加一個address和email

POST http://ip:port/index_name/mapping
{
	"properties":{
		"address":{
			"type":"text",
			"index":true  //預設true,是否索引(索引的才能被搜尋)
		},
		"email":{
			"type":"keword",
			"index":"true"
		}
	}
}

type:資料型別

index:是否索引。索引的欄位才能被搜尋(預設true)