Elasticsearch(二)——Rest API
RESTful 介面 URL 的格式是:
http://cluster的地址: 9200/<index>I<type>I [<id>]
其中,index, type 是必須提供的( index 可以理解為資料庫;type 理解為資料表); id 是可選的(相當於資料庫表中記 錄的主鍵是唯一的。如果不提供, Elasticsearch 會向動生成。增 、刪、改,查分別對應 HTTP 請求的 PUT 、DELETE、POST、GET方法。
Kibana DevTools
建立資料
建立index是province,type是citys ,當前id為1的document資料
PUT /province/citys/1
{
"name":"北京",
"code":"010"
}
返回結果
{
"_index": "province",
"_type": "citys",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
不適用id建立資料
POST /province/citys/
{
"name":"上海",
"code":"021"
}
返回結果
{
"_index": "province",
"_type": "citys",
"_id": "VV6_G2YBajYJa8_UqerL",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
修改資料
只想修改id唯一的field是name的值
POST /province/citys/1/_update
{
"doc":{
"name":"廣州"
}
}
替換資料
PUT /province/citys/1
{
"name":"廣州"
}
這個行為是替換資料,和修改資料是兩種不同形式的資料更新
刪除資料
DELETE /province/citys/1
返回結果
{
"_index": "province",
"_type": "citys",
"_id": "1",
"_version": 7,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 6,
"_primary_term": 1
}
curl命令列
建立索引
$ curl -XPUT "http://127.0.0.1:9200/school"
{
"acknowledged": true,
"shards_acknowledged": true,
"index": "school"
}
刪除索引
curl -XDELETE http://localhost:9200/school
{"acknowledged":true}
插入資料
指定id
$ curl -H "Content-Type:application/json" -XPUT http://localhost:9200/school/student/1 -d '{"name":"xiaoming","age":20}'
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
不指定id
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/ -d '{"name":"xiaowang","age":18}'
{
"_index": "school",
"_type": "student",
"_id": "kYeii2UBT-VMLOAXEoWx",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1
}
修改資料
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/1/_update -d '{"doc":{"name":"xiaohong","age":21}}'
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1
}
查詢資料
curl -XGET http://localhost:9200/school/student/1
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 2,
"found": true,
"_source": {
"name": "xiaohong",
"age": 21
}
}
刪除
刪除資料
curl -XDELETE http://localhost:9200/school/student/1
{
"_index": "school",
"_type": "student",
"_id": "1",
"_version": 3,
"result": "deleted",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 2,
"_primary_term": 1
}
刪除Type
curl -H "Content-Type:application/json" -XPOST http://localhost:9200/school/student/_delete_by_query -d '{"query":{"match_all":{}}}'
{
"took": 40,
"timed_out": false,
"total": 1,
"deleted": 1,
"batches": 1,
"version_conflicts": 0,
"noops": 0,
"retries": {
"bulk": 0,
"search": 0
},
"throttled_millis": 0,
"requests_per_second": -1,
"throttled_until_millis": 0,
"failures": []
}
bulk批量匯入
POST _bulk
{ "index" : { "_index" : "test", "_type" : "_doc", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_type" : "_doc", "_id" : "2" } }
{ "create" : { "_index" : "test", "_type" : "_doc", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_type" : "_doc", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
匯入結果
{
"took": 176,
"errors": false,
"items": [
{
"index": {
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"delete": {
"_index": "test",
"_type": "_doc",
"_id": "2",
"_version": 1,
"result": "not_found",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 404
}
},
{
"create": {
"_index": "test",
"_type": "_doc",
"_id": "3",
"_version": 1,
"result": "created",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 0,
"_primary_term": 1,
"status": 201
}
},
{
"update": {
"_index": "test",
"_type": "_doc",
"_id": "1",
"_version": 2,
"result": "updated",
"_shards": {
"total": 2,
"successful": 1,
"failed": 0
},
"_seq_no": 1,
"_primary_term": 1,
"status": 200
}
}
]
}
reindex資料遷移
POST _reindex
{
"source": {
"index": "twitter"
},
"dest": {
"index": "new_twitter"
}
}
批量匯入資料
POST /blog/paper/_bulk
{"index":{"_id":1}}
{"pID":"7ec0e0e5","uID":1,"publish":false,"date":"2018-01-02"}
{"index":{"_id":2}}
{"pID":"69590fe7","uID":2,"publish":true,"date":"2018-10-11"}
{"index":{"_id":3}}
{"pID":"aec30fa2","uID":1,"publish":false,"date":"2018-06-25"}
{"index":{"_id":4}}
{"pID":"a8733527","uID":3,"publish":false,"date":"2017-01-16"}
相關文章
- Elasticsearch 入門實戰(8)--REST API 使用二(Search API)ElasticsearchRESTAPI
- elasticsearch常用請求介面Rest API示例ElasticsearchRESTAPI
- Elasticsearch Java High Level REST Client(Delete API)ElasticsearchJavaRESTclientdeleteAPI
- Elasticsearch Java High Level REST Client(Exists API)ElasticsearchJavaRESTclientAPI
- ElasticSearch 2.3.3 java API操作二ElasticsearchJavaAPI
- SharePoint REST API - 基本操作(二)RESTAPI
- rest apiRESTAPI
- GraphQL API vs REST APIAPIREST
- Elasticsearch 7.x 之文件、索引和 REST API 【基礎入門篇】Elasticsearch索引RESTAPI
- Elasticsearch Java REST Client(目錄)ElasticsearchJavaRESTclient
- 使用ASP.NET web API建立REST服務(二)ASP.NETWebAPIREST
- Spark REST API & metricsSparkRESTAPI
- ElasticSearch—— Java APIElasticsearchJavaAPI
- Elasticsearch Search APIElasticsearchAPI
- ElasticSearch Java APIElasticsearchJavaAPI
- REST : rest_framework.decorators.api_view 實現PATCHRESTFrameworkAPIView
- Elasticsearch 入門實戰(9)--Java API Client 使用二ElasticsearchJavaAPIclient
- SharePoint REST API - 使用REST API和jQuery上傳一個檔案RESTAPIjQuery
- SharePoint REST API - 確定REST端點URLRESTAPI
- SharePoint REST API - 概述RESTAPI
- http REST API 驗證庫HTTPRESTAPI
- 利用OpenStack Rest API 建立映象RESTAPI
- 撰寫合格的REST APIRESTAPI
- Rest API 的那些事兒RESTAPI
- REST API 最佳入門指南RESTAPI
- ElasticSearch Java API使用ElasticsearchJavaAPI
- elasticsearch(八)---search apiElasticsearchAPI
- ElasticSearch – API ConventionsElasticsearchAPI
- elasticsearch api client使用ElasticsearchAPIclient
- Elasticsearch Java Low Level REST Client(初始化)ElasticsearchJavaRESTclient
- Elasticsearch Java High Level REST Client(入門)ElasticsearchJavaRESTclient
- ASP.NET Web API與Rest web api(一)ASP.NETWebAPIREST
- Django REST framework API 指南(21):SchemasDjangoRESTFrameworkAPI
- Django REST framework API 指南(8):渲染DjangoRESTFrameworkAPI
- Django REST framework API 指南(6):路由DjangoRESTFrameworkAPI路由
- Django REST framework API 指南(7):解析DjangoRESTFrameworkAPI
- Django REST framework API 指南(15):限流DjangoRESTFrameworkAPI
- 5個REST API安全準則RESTAPI