文档管理

创建文档

创建文档不指定id

# curl -X POST 192.168.1.27:9200/<index-name>/_doc
curl -X POST 192.168.1.27:9200/study/_doc \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Will",
    "hobby": ["Driving", "Climbing"]
}'

创建文档指定id

# curl -X POST 192.168.1.27:9200/<index-name>/_doc/<id>
curl -X POST 192.168.1.27:9200/study/_doc/1001 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "Anny",
    "hobby": ["Ridding", "Running"]
}'

修改文档

全局更新

# curl -X POST 192.168.1.27:9200/<index-name>/_doc/<id>
curl -X POST 192.168.1.27:9200/study/_doc/1001 \
  -H "Content-Type: application/json" \
  -d '{
    "name": "David",
    "hobby": ["Drinking", "Running"]
}'

局部更新

# curl -X POST 192.168.1.27:9200/<index-name>/_doc/<id>/_update
curl -X POST 192.168.1.27:9200/study/_doc/1001/_update \
  -H "Content-Type: application/json" \
  -d '{
    "doc": {
        "age": "33"
    }
}'

查看文档

查看指定索引中的指定文档

# curl 192.168.1.27:9200/<index-name>/_doc/<id>
curl 192.168.1.27:9200/study/_doc/1001

查看指定索引中的所有文档

# curl 192.168.1.27:9200/<index-name>/_doc/_search
curl 192.168.1.27:9200/study/_doc/_search

删除文档

删除指定索引中的指定文档

# curl -X DELETE 192.168.1.27:9200/<index-name>/_doc/<id>
curl -X DELETE 192.168.1.27:9200/study/_doc/1001

文档的批量操作

文档的批量创建

curl -X POST 192.168.1.27:9200/_bulk \
  -H "Content-Type: application/json" \
  -d '{ "create": { "_index": "study"} }
{ "name": "Will", "hobby": ["Driving", "Drinking"] }
{ "create": { "_index": "study", "_id": "1002"} }
{ "name": "Will-id", "hobby": ["Driving", "Drinking"] }
{ "create": { "_index": "study", "_id": "1003"} }
{ "name": "Will-1", "hobby": ["Swimming", "Sleeping"] }
{ "create": { "_index": "study"} }
{ "name": "Will-2", "hobby": ["Riding", "Eatting"] }
'

文档的批量删除

curl -X POST 192.168.1.27:9200/_bulk \
  -H "Content-Type: application/json" \
  -d '{ "delete": { "_index": "study", "_id": "1002"} }
{ "delete": { "_index": "study", "_id": "1003"} }
'

文档的批量修改

curl -X POST 192.168.1.27:9200/_bulk \
  -H "Content-Type: application/json" \
  -d '{ "update": { "_index": "study", "_id": "1002"} }
{ "doc": { "name": "William" } }
{ "update": { "_index": "study", "_id": "YmFsBZYB4_hN6tREPwur"} }
{ "doc": { "name": "William-1" } }
'

文档的批量查看

curl -X POST 192.168.1.27:9200/_mget \
  -H "Content-Type: application/json" \
  -d '{
    "docs": [
        {
            "_index": "study",
            "_id": "YmFsBZYB4_hN6tREPwur"
        },
        {
            "_index": "study",
            "_id": "1002"
        }
    ]
}
'

Last updated