Elasticsearch文档操作


ES文档操作

添加文档

自动创建id方式

索引已经创建好之后就可以继续创建文档,并添加数据。 这里的文档可以类比为关系型数据库中的表数据,添加的数据格式为JSON格式。

选择post请求方式,并发出如下请求

http://localhost:9200/golang/_doc

Body里面选择raw,添加的数据选择JSON格式,内容如下:

{
    "name": "Psych",
    "url": "https://blog.csdn.net/qq_39280718?type=blog",
    "author": "PPPsych"
}

运行结果如下:

在这里插入图片描述

返回的结果里面包含了_id字段也就是该文档的唯一标识。这个id也是可以自定义的。

自定义id方式

在某些场景下我们可能需要去自主产生维护id信息, 这样的情况下主要创建文档的URL要有变化,就是在url后面要加上id信息。例如http://127.0.0.1:9200/golang/_doc/001
同样使用POST请求,在Body里面选择raw,添加的数据选择JSON格式(操作同上)

在这里插入图片描述

这个时候响应体里面的id就是自定义的001了。

其实创建文档的地址也可以是http://127.0.0.1:9200/golang/_create/002.也就是_create_doc都是可行的

在这里插入图片描述

查询文档

主键查询

从前面的操作来看,其实可以很好的猜想根据主键查询的url地址一个和主键添加文档的URL地址一样,只是请求的方法得使用GET请求。例如查询001的文档,URL地址:http://127.0.0.1:9200/golang/_doc/001

在这里插入图片描述

如果我们给出的id是不存在的,例如:http://127.0.0.1:9200/golang/_doc/999
返回内容就会变为:

在这里插入图片描述

全查询

全查询就是查询所有的文档信息:
请求的url地址:http://127.0.0.1:9200/golang/_search

运行结果为:

{
    "took": 38,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 5,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "golang",
                "_type": "_doc",
                "_id": "WmazaIMBJ_0P-pFT6wOp",
                "_score": 1.0,
                "_source": {
                    "name": "Psych",
                    "url": "https://blog.csdn.net/qq_39280718?type=blog",
                    "author": "PPPsych"
                }
            },
            {
                "_index": "golang",
                "_type": "_doc",
                "_id": "001",
                "_score": 1.0,
                "_source": {
                    "name": "Psych",
                    "url": "https://blog.csdn.net/qq_39280718?type=blog",
                    "author": "PPPsych"
                }
            },
            {
                "_index": "golang",
                "_type": "_doc",
                "_id": "002",
                "_score": 1.0,
                "_source": {
                    "name": "Psych",
                    "url": "https://blog.csdn.net/qq_39280718?type=blog",
                    "author": "PPPsych"
                }
            },
            {
                "_index": "golang",
                "_type": "_doc",
                "_id": "003",
                "_score": 1.0,
                "_source": {
                    "name": "Psych",
                    "url": "https://blog.csdn.net/qq_39280718?type=blog",
                    "author": "PPPsych"
                }
            },
            {
                "_index": "golang",
                "_type": "_doc",
                "_id": "005",
                "_score": 1.0,
                "_source": {
                    "name": "Morax",
                    "url": "https://blog.csdn.net/qq_39280718?type=blog",
                    "author": "钟离"
                }
            }
        ]
    }
}

响应内容里面会包含了是否超时time_out 、切片信息shards

hits这个里面就是我们查询出来的内容数据。

修改文档

当文档数据被创建之后如果想对文档进行修改,这样的操作在ES中是被允许的。但是在ES中修改是分为两种情况的,

  1. 一种是完全覆盖式的修改,也叫全量修改。
  2. 另外一种是局部性的修改。

全量修改

全量修改的情况下,无论发送多少次请求,数据都是会被完全覆盖的。

所以可以采用PUT进行操作. 地址跟主键查询和自定义主键修改是类似的。

URL:http://127.0.0.1:9002/golang/_doc/001

其中:

  • _doc表示文档数据。

  • 001就是文档主键。

  • http请求的body体就是我们准备修改后的内容。比如下面我们准备修改一下name

    {
        "name": "ROOT",
        "url": "https://blog.csdn.net/qq_39280718?type=blog",
        "author": "PPPsych"
    }
    

    发送请求获取响应:

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

    执行结果中主要内容:

    1. result修改结果为updated,即修改成功。

      1. _version:版本,修改了一次加了1,变为了2.
  • 查询文档是否修改成功:

    在这里插入图片描述

    发现name发生变化,即修改成功

局部修改

由于我们是局部修改,所以我们每次修改的结果不会是相同的。此时不能采用PUT,要使用POST

URL地址和全量更新也不相同:

  • http://127.0.0.1:9002/golang/_update/002

  • http请求的body也有固定的格式,例如同上修改namebody体如下:

    {
        "doc":{
            "name": "root"
        }
    }
    
  • 发送请求获取响应如下:

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

    这个时候响应结果resultupdate

  • 如果我们修改的内容与库中内容完全一致,如下响应的resultnoop

    在这里插入图片描述

  • 查询文档是否修改成功

    在这里插入图片描述

    发现name发生变化,即修改成功

删除文档

文档创建之后删除也是我们对数据管理的一些常规操作。与前面的操作类似,删除操作是,http请求的方式需要使用delete操作.资源地址是一样的。

具体过程:

  1. 先全查询当前有哪些文档

    {
        "took": 1,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 5,
                "relation": "eq"
            },
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "WmazaIMBJ_0P-pFT6wOp",
                    "_score": 1.0,
                    "_source": {
                        "name": "Psych",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "003",
                    "_score": 1.0,
                    "_source": {
                        "name": "Psych",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "005",
                    "_score": 1.0,
                    "_source": {
                        "name": "Morax",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "钟离"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "001",
                    "_score": 1.0,
                    "_source": {
                        "name": "ROOT",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "002",
                    "_score": 1.0,
                    "_source": {
                        "name": "root",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                }
            ]
        }
    }
    
  2. 删除idWmazaIMBJ_0P-pFT6wOp的文档

  3. url地址为:http://127.0.0.1:9200/golang/_doc/WmazaIMBJ_0P-pFT6wOp

  4. 发送DELETE请求

    在这里插入图片描述

    响应内容的result可以看出为deleted,也就是被删除成功了。

  5. 如果我们再点击一次继续删除。响应结果如下:

    在这里插入图片描述

    此时result的值为not_found也就是没有找到该文档。即表示删除成功

  6. 再次全查询查看当前已有文档:

    {
        "took": 90,
        "timed_out": false,
        "_shards": {
            "total": 1,
            "successful": 1,
            "skipped": 0,
            "failed": 0
        },
        "hits": {
            "total": {
                "value": 4,
                "relation": "eq"
            },
            "max_score": 1.0,
            "hits": [
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "003",
                    "_score": 1.0,
                    "_source": {
                        "name": "Psych",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "005",
                    "_score": 1.0,
                    "_source": {
                        "name": "Morax",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "钟离"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "001",
                    "_score": 1.0,
                    "_source": {
                        "name": "ROOT",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                },
                {
                    "_index": "golang",
                    "_type": "_doc",
                    "_id": "002",
                    "_score": 1.0,
                    "_source": {
                        "name": "root",
                        "url": "https://blog.csdn.net/qq_39280718?type=blog",
                        "author": "PPPsych"
                    }
                }
            ]
        }
    }
    

    发现idWmazaIMBJ_0P-pFT6wOp的文档已被删除。


版权声明:本文为qq_39280718原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。