ES文档操作

前言

在添加数据的时候,所有被添加的数据一定是JSON格式

{
    "name":"小小",
    "team_name":"TT",
    "position":"MeiGong",
    "play_year":"10",
    "jerse_no":"2"
}

一定要注意下面几点(我用的软件是POSTMAN)
使用的软件

添加一个文档

//发送的是PUT请求
localhost:9200/索引/_doc/文档的ID

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 4
}

添加文档(不指定ID,这里也写一下防止踩坑)

//注:当你不指定ID时,一定要发送POST请求
//当你不指定ID的时候,系统会自动生成一个ID
localhost:9200/索引/_doc

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "3fNBHW4BbfcyTillyx_N", //看这个ID就生成了是一个20位的GUID
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 3,
    "_primary_term": 4
}

获取一个文档


//发送GET请求
localhost:9200/索引/_doc/文档的ID

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "_seq_no": 2,
    "_primary_term": 4,
    "found": true,
    "_source": {
        "name": "小小",
        "team_name": "TT",
        "position": "MeiGong",
        "play_year": "10",
        "jerse_no": "2"
    }
}

自动创建一个索引

//通过GET的方式访问下面的这个请求
localhost:9200/_cluster/settings

查看这个状态auto_create_index

{
    "persistent": {},
    "transient": {}
}

在家会看到我上面请求得到的响应并没有这个状态,
注:当索引不存在并且auto_create_index为true的时候,新增的文档会自动创建索引

修改auto_create_index状态

访问请求

//通过PUT请求
localhost:9200/_cluster/settings

请求体

{
    "persistent":{
        "action.auto_create_index":"true"
    }
}

创建一个不存在的索引的文档


//通过POST发送
localhost:9200/一个不存在的索引/_doc/

请求体不写了哈!!!!

响应创建成功

{
    "_index": "niubi",
    "_type": "_doc",
    "_id": "3vNQHW4BbfcyTillOh_r",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

指定操作类型

localhost:9200/niubi/_doc/1?op_type=create

这里是因为怕误操作所以可以加上个操类型,防止出错

查看文档

查看单个文档(这里是和上面的获取一个文档是重复的,这里就在整理一下)

//发送GET请求
localhost:9200/索引/_doc/文档的ID

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 3,
    "_seq_no": 2,
    "_primary_term": 4,
    "found": true,
    "_source": {
        "name": "小小",
        "team_name": "TT",
        "position": "MeiGong",
        "play_year": "10",
        "jerse_no": "2"
    }
}

查看多个文档

//通过POST发送
localhost:9200/_mget

请求体


{
    "docs":[
        {
            "_index":"nba",
            "_type":"_doc",
            "_id":"1"
        },
        {
            "_index":"nba",
            "_type":"_doc",
            "_id":"2"
        }
    ]
}

响应

{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 3,
            "_seq_no": 2,
            "_primary_term": 4,
            "found": true,
            "_source": {
                "name": "小小",
                "team_name": "TT",
                "position": "MeiGong",
                "play_year": "10",
                "jerse_no": "2"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

当有的数据查出来的时候就会显示出来,没有查到的数据在found那里是false

另一种查询的方式

//发送POST请求
localhost:9200/nab/_mget

Body体

{
    "docs":[
        {
            "_type":"doc",
            "_id":"1"
        },
        {
            "_type":"doc",
            "_id":"2"
        }
    ]
}

这里因为在URL中指定了一个索引在请求体中就不会在写_index

再写一种方式

localhost:9200/索引/_doc/_mget

请求体

{
    "docs":[
        {
            "_id":"1"
        },
        {
            "_id":"2"
        }
    ]
}

响应

{
    "docs": [
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "1",
            "_version": 3,
            "_seq_no": 2,
            "_primary_term": 4,
            "found": true,
            "_source": {
                "name": "小小",
                "team_name": "TT",
                "position": "MeiGong",
                "play_year": "10",
                "jerse_no": "2"
            }
        },
        {
            "_index": "nba",
            "_type": "_doc",
            "_id": "2",
            "found": false
        }
    ]
}

上面的请求体还可以换成数据的方式

{
    "ids":["1","2"]
}

删除文档

//发送的是DELETE请求
localhost:9200/索引/_doc/文档ID

响应

{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 12,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 13,
    "_primary_term": 4
}

修改文档的信息

localhost:9200/索引/_update/文档ID

这里面的JSON数组要注意,不要写错了

{
    "doc":{
        "name":"zzz",
        "team_name":"www",
        "position":"ssss",
        "play_year":"10",
        "jerse_no":"30"
    }
}

响应


{
    "_index": "nba",
    "_type": "_doc",
    "_id": "1",
    "_version": 4,
    "_seq_no": 4,
    "_primary_term": 4,
    "found": true,
    "_source": {
        "name": "zzz",
        "team_name": "www",
        "position": "ssss",
        "play_year": "10",
        "jerse_no": "30"
    }
}

在文档添加一个字段

//发送POST请求
localhost:9200/索引/_update/1

请求体

{
    "script":"ctx._source.sex = 1"
}

通过ctx获取到_source,添加一个sex的字段

在文档删除一个字段

只要把请求体换成

同样是发送的POST

{
    "script":"ctx._source.remove(\"字段\")"
}

在原用的字段上进行加操作

//通过POST方式
localhost:9200/nba/_update/1

请求体

{
    "script":{
        "source":"ctx._source.age += params.age", //通过ctx找到source中的age 加上 params中age的值
        "params":{ //指定参数
            "age":4 //这个指也可以和source中的age的名字不一样
        }
    }
}

upsert的作用,当指定的文档不存在的时候,upsert就会创建一个新的文档,如果指定的文档存在就会执行更新逻辑

"upsert":{
    "sex":1
}
Last modification:October 31st, 2019 at 08:38 am
如果觉得我的文章对你有用,请随意赞赏

Leave a Comment