ES7.12常规操作总结

https://blog.csdn.net/liu320yj/category_11046278.html

Elasticsearch是一个开源的高扩展的分布式、RESTful风格的搜索和数据分析引擎,能够解决不断涌现出的各种用例。
本文使用Elasticsearch7.12.0版本,使用Postman作为客户端访问工具

Elasticsearch数据格式

Elasticsearch是面向文档型数据库,一条数据在此就是一个文档,与MySQL对比如下:
elastic与mysql对比
ES里的Index可以看作是一个库,Types相当于表,Documents相当于表的行。在ES7.x中,Type的概念已经被删除

索引操作
创建索引
相当于关系型数据库中创建数据库,在Postman中使用PUT类型发起请求:http://localhost:9200/user
返回数据:

{
    "acknowledged": true,
    "shards_acknowledged": true,
    "index": "user"
}


acknowledged:响应结果
shards_acknowledged:分片结果
index:索引名称

如果重复添加会返回错误信息,索引已存在

{
    "error": {
        "root_cause": [
            {
                "type": "resource_already_exists_exception",
                "reason": "index [user/zF9iWH0dR-2Xt_dmr5BPZg] already exists",
                "index_uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
                "index": "user"
            }
        ],
        "type": "resource_already_exists_exception",
        "reason": "index [user/zF9iWH0dR-2Xt_dmr5BPZg] already exists",
        "index_uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
        "index": "user"
    },
    "status": 400
}

查看所有索引

在Postman中,使用GET类型发起请求:http://localhost:9200/_cat/indices?v
_cat表示查看,indices表示索引
返回结果:

health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   user     zF9iWH0dR-2Xt_dmr5BPZg   1   1          0            0       208b           208b

查看单个索引

在Postman中,使用GET类型发起请求:http://localhost:9200/user

{
    "user": {
        "aliases": {},
        "mappings": {},
        "settings": {
            "index": {
                "routing": {
                    "allocation": {
                        "include": {
                            "_tier_preference": "data_content"
                        }
                    }
                },
                "number_of_shards": "1",
                "provided_name": "user",
                "creation_date": "1618148948222",
                "number_of_replicas": "1",
                "uuid": "zF9iWH0dR-2Xt_dmr5BPZg",
                "version": {
                    "created": "7120099"
                }
            }
        }
    }
}

注意:查看索引和创建索引的请求路径都是一样的,不同的是请求方式不同



删除索引

在Postman中,使用DELETE类型发起请求:http://localhost:9200/user
返回结果:

{
    "acknowledged": true
}

 重新访问索引时,会返回响应:索引不存在

{
    "error": {
        "root_cause": [
            {
                "type": "index_not_found_exception",
                "reason": "no such index [user]",
                "resource.type": "index_or_alias",
                "resource.id": "user",
                "index_uuid": "_na_",
                "index": "user"
            }
        ],
        "type": "index_not_found_exception",
        "reason": "no such index [user]",
        "resource.type": "index_or_alias",
        "resource.id": "user",
        "index_uuid": "_na_",
        "index": "user"
    },
    "status": 404
}

文档操作

类似于关系型数据库中的表数据



创建文档

在Postman中,使用POST类型发起请求:http://localhost:9200/user/_doc

{
	"id": 100,
	"name": "张宝芸",
	"age": 18,
	"salary": 6000
}

 响应结果为:

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

_id是唯一性标识,如果没有指定,ES服务器会随机生成,如果需要自定义唯一性标识,需要在创建时指定id,即http://localhost:9200/user/_doc/1
注意:如果不指定主键,请求类型只能是POST,如果指定主键,请求类型可以是PUT

查看文档
在Postman中,使用GET请求类型发起请求:http://localhost:9200/user/_doc/1
响应结果:

{
    "_index": "user",
    "_type": "_doc",
    "_id": "1",
    "_version": 1,
    "_seq_no": 1,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "id": 101,
        "name": "萧峰",
        "age": 20,
        "salary": 8000
    }
}

其中found表示查询结果,true表示查询到,false表示未查询到



修改文档

在Postman中,使用POST类型发起请求:http://localhost:9200/user/_doc/1
响应结果:

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值