本文主要是介绍ElasticSearch7.3.2-Rest实战指南-Document APIs,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
文章目录
- 一、Single document APIs
- 1.1、Index
- 1.2、Get
- 1.3、Delete
- 1.4、Delete by query API
- 1.5、Update API
- 二、Multi-document APIs
- 2.1、Multi get
- 2.2、Bulk
- 3、More
一、Single document APIs
1.1、Index
Adds a JSON document to the specified index and makes it searchable. If the document already exists, updates the document and increments its version.
- Request
PUT /<index>/_doc/<_id>
POST /<index>/_doc/
PUT /<index>/_create/<_id>
POST /<index>/_create/<_id>
- Example
由于高版本不再需要指定type,因此默认为_doc。
- 使用默认id
curl -XPOST 'http://10.143.228.25:9200/student/_doc?pretty' -H "Content-Type: application/json" -d'
{"id": 1,"name": "门捷列夫","age": 20,"address": "俄罗斯","birthday": "1990-02-03"
}'
- 指定自定义id
curl -XPOST 'http://10.143.228.25:9200/student/_doc/1?pretty' -H "Content-Type: application/json" -d'
{"id": 1,"name": "门捷列夫","age": 20,"address": "俄罗斯","birthday": "1990-02-03"
}
'
response示例:
{"_index" : "student","_type" : "_doc","_id" : "k1mNQHIBrm6ikcYVeiqR","_version" : 1,"result" : "created","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 0,"_primary_term" : 1
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-index_.html
1.2、Get
Retrieves the specified JSON document from an index.
- Request
GET <index>/_doc/<_id>
HEAD <index>/_doc/<_id>
GET <index>/_source/<_id>
HEAD <index>/_source/<_id>
- Example
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty'
response示例:
{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 4,"_primary_term" : 1,"found" : true,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 20,"address" : "俄罗斯","birthday" : "1990-02-03"}
}
- 返回指定字段
curl -XGET 'http://10.143.228.25:9200/student/_doc/1?pretty&_source=name,age'
response示例:
{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 1,"_seq_no" : 4,"_primary_term" : 1,"found" : true,"_source" : {"name" : "门捷列夫","age" : 20}
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-get.html
1.3、Delete
Removes a JSON document from the specified index.
- Request
DELETE /<index>/_doc/<_id>
- Example
curl -XDELETE 'http://10.143.228.25:9200/student/_doc/1?pretty'
response示例:
{"_index" : "student","_type" : "_doc","_id" : "1","_version" : 2,"result" : "deleted","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 5,"_primary_term" : 1
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-delete.html
1.4、Delete by query API
Removes a JSON document from the specified index.
- Request
POST /<index>/_delete_by_query
- Example
curl -XPOST 'http://10.143.228.25:9200/student/_delete_by_query?pretty' -H 'Content-Type: application/json' -d'
{"query": {"match": {"name": "门捷列夫"}}
}
'
response示例:
{"took" : 993,"timed_out" : false,"total" : 4,"deleted" : 4,"batches" : 1,"version_conflicts" : 0,"noops" : 0,"retries" : {"bulk" : 0,"search" : 0},"throttled_millis" : 0,"requests_per_second" : -1.0,"throttled_until_millis" : 0,"failures" : [ ]
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-delete-by-query.html
1.5、Update API
Updates a document using the specified script.
- Request
POST /<index>/_update/<_id>
- Example
curl -XPOST 'http://10.143.228.25:9200/student/_update/nlmkQHIBrm6ikcYVUir7?pretty' -H 'Content-Type: application/json' -d'
{"doc": {"name": "门捷列夫","age": 22}
}
'
response示例:
{"_index" : "student","_type" : "_doc","_id" : "nlmkQHIBrm6ikcYVUir7","_version" : 2,"result" : "updated","_shards" : {"total" : 3,"successful" : 3,"failed" : 0},"_seq_no" : 2,"_primary_term" : 1
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-update.html
二、Multi-document APIs
2.1、Multi get
Retrieves multiple JSON documents by ID.
- Request
GET /_mget
GET /<index>/_mget
- Example
curl -XGET 'http://10.143.228.25:9200/student/_mget?pretty' -H 'Content-Type: application/json' -d'
{"docs": [{"_type": "_doc","_id": "nlmkQHIBrm6ikcYVUir7"}]
}
'
response示例:
{"docs" : [{"_index" : "student","_type" : "_doc","_id" : "nlmkQHIBrm6ikcYVUir7","_version" : 2,"_seq_no" : 2,"_primary_term" : 1,"found" : true,"_source" : {"id" : 1,"name" : "门捷列夫","age" : 22,"address" : "俄罗斯","birthday" : "1990-02-03"}}]
}
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-multi-get.html
2.2、Bulk
Performs multiple indexing or delete operations in a single API call. This reduces overhead and can greatly increase indexing speed.
- Request
POST /_bulk
POST /<index>/_bulk
- Example
curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/json' -d'
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
'
更多阅读:https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs-bulk.html
3、More
https://www.elastic.co/guide/en/elasticsearch/reference/7.x/docs.html
下面的是我的公众号二维码图片,欢迎关注。
这篇关于ElasticSearch7.3.2-Rest实战指南-Document APIs的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!