ElasticSearch 7 学习(4)文档的增删改查

新增文档 指定ID PUT/POST请求

  • 请求
localhost:9200/nba/_doc/1

  • 请求体
{
    "name": "哈登",
    "team_name": "火箭",
    "position": "得分后卫",
    "play_year": "10",
    "jerse_no": "13"
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1", //文档的ID
    "_version": 1,
    "result": "created", // 响应结果
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

新增文档 自动生成ID POST请求

注意: 不指定ID只能使用POST请求
注意: 自动生成ID开关要打开,关闭状态无法自动创建ID

  • 请求
localhost:9200/nba/_doc
  • 请求体
{
    "name": "库里",
    "team_name": "勇士",
    "position": "组织后卫",
    "play_year": "10",
    "jerse_no": "30"
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "7PkGT24BeuZ7t7g8CXe-", // 自动生成ID
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 1
}

自动创建索引 POST请求

查看auto_create_index开关状态,请求http://localhost:9200/_cluster/settings
注意:当索引不存在并且auto_create_index为true的时候,新增文档时会自动创建索引,若为false是不能自动创建索引

  • 请求
localhost:9200/wnba/_doc/1
  • 请求体
{
    "name": "周琦",
    "team_name": "波兰国家队",
    "position": "中锋",
    "play_year": "3",
    "jerse_no": "9"
}
  • 响应
{
    "_index": "wnba", // 自动创建的索引
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

查看自动创建的索引 GET请求

  • 请求
localhost:9200/wnba
  • 响应
{
    "wnba": {
        "aliases": {},
        "mappings": {
            "properties": {
                "jerse_no": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "play_year": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "position": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                },
                "team_name": {
                    "type": "text",
                    "fields": {
                        "keyword": {
                            "type": "keyword",
                            "ignore_above": 256
                        }
                    }
                }
            }
        },
        "settings": {
            "index": {
                "creation_date": "1573284418718",
                "number_of_shards": "1",
                "number_of_replicas": "1",
                "uuid": "fKs9KZ11R3-_zgKi8WFQTQ",
                "version": {
                    "created": "7020199"
                },
                "provided_name": "wnba"
            }
        }
    }
}

指定操作类型

新增或修改的时候可能会把原有文档修改掉,这里可以指定类型。比如文档存在,要新增一条文档,但是没有指定类型可能修改掉原有的文档

  • 请求
localhost:9200/nba/_doc/1?op_type=create
  • 请求体
{
    "name": "周琦",
    "team_name": "波兰国家队",
    "position": "中锋",
    "play_year": "3",
    "jerse_no": "9"
}
  • 响应
{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                // 文档已经存在
                "reason": "[1]: version conflict, document already exists (current version [5])",
                "index_uuid": "hkhv1WKSQqWil3P9UXt3Aw",
                "shard": "0",
                "index": "nba"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[1]: version conflict, document already exists (current version [5])",
        "index_uuid": "hkhv1WKSQqWil3P9UXt3Aw",
        "shard": "0",
        "index": "nba"
    },
    "status": 409
}

查看指定ID文档 GET请求

  • 请求
localhost:9200/nba/_doc/1
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 5,
    "_seq_no": 5,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "哈登",
        "team_name": "火箭",
        "position": "得分后卫",
        "play_year": "10",
        "jerse_no": "13"
    }
}

查看多条文档 第一种方式 GET/POST请求

  • 请求
localhost:9200/_mget
  • 请求体
{
    "docs" : [ // 指定标签
        {
            "_index" : "nba", // 指定索引
            "_type" : "_doc", // 默认类型
            "_id" : "1"       // 指定ID
        },
        {
            "_index" : "nba",
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}
  • 响应
{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 5,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "哈登",
                "team_name": "火箭",
                "position": "得分后卫",
                "play_year": "10",
                "jerse_no": "13"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

查看多条文档 第二种方式 GET/POST请求

  • 请求
localhost:9200/nba/_mget // 先指定索引
  • 请求体
{
    "docs" : [
        {
            "_type" : "_doc",
            "_id" : "1"
        },
        {
            "_type" : "_doc",
            "_id" : "2"
        }
    ]
}
  • 响应
{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 5,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "哈登",
                "team_name": "火箭",
                "position": "得分后卫",
                "play_year": "10",
                "jerse_no": "13"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

查看多条文档 第三种方式 GET/POST请求

  • 请求
localhost:9200/nba/_doc/_mget    //指定索引、类型
  • 请求体
{
    "docs" : [
        {
            "_id" : "1"
        },
        {
            "_id" : "2"
        }
    ]
}
  • 响应
{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 5,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "哈登",
                "team_name": "火箭",
                "position": "得分后卫",
                "play_year": "10",
                "jerse_no": "13"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

查看多条文档 第四种方式 GET/POST请求

  • 请求
localhost:9200/nba/_doc/_mget
  • 请求体
{
    "ids":["1","2"]
}
  • 响应
{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 5,
            "_seq_no": 5,
            "_primary_term": 1,
            "found": true,
            "_source": {
                "name": "哈登",
                "team_name": "火箭",
                "position": "得分后卫",
                "play_year": "10",
                "jerse_no": "13"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

修改文档 POST请求

根据提供的文档片段更新数据

  • 请求
localhost:9200/nba/_update/1
  • 请求体
{
    "doc": { // doc标签必须存在
        "name": "哈登",
        "team_name": "火箭",
        "position": "双能卫",
        "play_year": "10",
        "jerse_no": "13"
    }
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 7,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 1
}

向_source增加字段 POST请求

  • 请求
localhost:9200/nba/_update/1
  • 请求体
{
    // script:标签 ctx:上下文 ._source = _source
    // 语义:通过上下文拿到 _source字段,新增age为18
    "script": "ctx._source.age = 18"
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 8,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 8,
    "_primary_term": 1
}

向_source删除字段 POST请求

  • 请求
localhost:9200/nba/_update/1
  • 请求体
{
    // json格式无法出现多个" 所以需要转义符
    "script": "ctx._source.remove(\"age\")"
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 9,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 9,
    "_primary_term": 1
}

更新指定文档的字段 POST请求

  • 请求
localhost:9200/nba/_update/1
  • 请求体
{
    "script": {
        // 先获取ID为1的数据,之后进行age+4
        "source": "ctx._source.age += params.age",
        // 指定参数
        "params": {
            "age": 4
        }
    },
    // 若存在则修改,若不存在则新增
    "upsert":{
        "age": 1
    }
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 11,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 11,
    "_primary_term": 1
}

查询更新结果

  • 请求
localhost:9200/nba/_doc/1
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 11,
    "_seq_no": 11,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "name": "大胡子",
        "team_name": "火箭",
        "position": "双能卫",
        "play_year": "10",
        "jerse_no": "13",
        "age": 22
    }
}

upsert介绍

upsert当指定的文档不存在时,upsert参数包含的内容将会被插入到索引中,作为一个新文档;如果指定的文档存在,ElasticSearch引擎将会执行指定的更新逻辑

  • 请求
localhost:9200/nba/_update/3
  • 请求体
{
    "script": {
        "source": "ctx._source.allstar += params.allstar",
        "params": {
            "allstar": 4
        }
    },
    "upsert": {
        "allstar": 1
    }
}
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "3",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 12,
    "_primary_term": 1
}

查询更新结果upsert结果

  • 请求
localhost:9200/nba/_doc/3
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "3",
    "_version": 1,
    "_seq_no": 12,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "allstar": 1
    }
}

删除文档 DELETE请求

  • 请求
localhost:9200/nba/_doc/3
  • 响应
{
    "_index": "nba",
    "_type": "_doc",
    "_id": "3",
    "_version": 2,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 1
}

版权声明:
作者:Joe.Ye
链接:https://www.appblog.cn/index.php/2023/03/12/elasticsearch-7-learning-document-add-delete-update-search/
来源:APP全栈技术分享
文章版权归作者所有,未经允许请勿转载。

THE END
分享
二维码
打赏
海报
ElasticSearch 7 学习(4)文档的增删改查
新增文档 指定ID PUT/POST请求 请求 localhost:9200/nba/_doc/1 请求体 { "name": "哈登", "team_name": "……
<<上一篇
下一篇>>
文章目录
关闭
目 录