[Elasticsearc] Elasticsearch 初见

Elasticsearch 初见

启动

双击 bin 目录下的 elasticsearch.bat 文件,等待终端运行成功

索引的增删改查

  • 增(PUT)

    postman 发送请求 PUT 请求:http://127.0.0.1:9200/shopping

    返回结果:

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

    因为 PUT 请求说明接口是幂等性的,所以如果再次进行相同请求,会返回以下数据表示索引已存在:

    {
        "error": {
            "root_cause": [
                {
                    "type": "resource_already_exists_exception",
                    "reason": "index [shopping/uQ946aIJSwWwaZNTPgPwEw] already exists",
                    "index_uuid": "uQ946aIJSwWwaZNTPgPwEw",
                    "index": "shopping"
                }
            ],
            "type": "resource_already_exists_exception",
            "reason": "index [shopping/uQ946aIJSwWwaZNTPgPwEw] already exists",
            "index_uuid": "uQ946aIJSwWwaZNTPgPwEw",
            "index": "shopping"
        },
        "status": 400
    }
    
  • 删(DELETE)

    postman 发送请求 DELETE 请求:http://127.0.0.1:9200/shopping

    返回数据如下,表示删除成功

    {
        "acknowledged": true
    }
    
  • 查(GET)

    postman 发送 GET 请求:http://127.0.0.1:9200/shopping

    会返回 shopping 这个索引的相关信息:

    {
        "shopping": {
            "aliases": {},
            "mappings": {},
            "settings": {
                "index": {
                    "routing": {
                        "allocation": {
                            "include": {
                                "_tier_preference": "data_content"
                            }
                        }
                    },
                    "number_of_shards": "1",
                    "provided_name": "shopping",
                    "creation_date": "1707056309923",
                    "number_of_replicas": "1",
                    "uuid": "5i7qBmHNSvq5NubfCPyElg",
                    "version": {
                        "created": "8500008"
                    }
                }
            }
        }
    }
    

    postman 发送 GET 请求:http://127.0.0.1:9200/_cat/indices?v

    会返回所有索引的相关信息,如下:

    health status index    uuid                   pri rep docs.count docs.deleted store.size pri.store.size dataset.size
    yellow open   shopping 5i7qBmHNSvq5NubfCPyElg   1   1          0            0       227b           227b         227b
    

文档的创建

文档需要在索引的基础上进行创建,所以可以看到,创建文档的请求地址可以是 http://127.0.0.1:9200/shopping/_doc

_doc 就代表文档。等同于数据库中创建数据。

postman 发送 post 请求:http://127.0.0.1:9200/shopping/_doc

但是这么发送请求会报错,因为没有请求体,也就是具体的需要创建的数据:

{
    "error": {
        "root_cause": [
            {
                "type": "parse_exception",
                "reason": "request body is required"
            }
        ],
        "type": "parse_exception",
        "reason": "request body is required"
    },
    "status": 400
}

所以在用 post 发送请求的时候,需要带上请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "https://www.mi.com/",
    "price": 3999.0
}

返回数据创建成功数据:

{
    "_index": "shopping",
    "_id": "QxKDdI0B8aQTV-f6ZJpd",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}

但是可以看到上述的返回数据中,_id 是随机的,这是 ES 自动随机生成的,且因为用的是 post 请求,所以接口并不是幂等接口,每次 post 相同接口都可以成功创建文档返回数据,但是返回的 _id 值不同。

当然创建文档也可以自定义 id,使之不适用 ES 自动创建的 id,则可发送类似请求:

http://127.0.0.1:9200/shopping/_doc/100001

返回的数据中,id 就是自定义的 id 了:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 1,
    "result": "created",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 1,
    "_primary_term": 1
}

当然,如果再次发送相同 id 的请求,版本会增加,且操作会变为更新操作:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 2,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 2,
    "_primary_term": 1
}

这就是所谓的全量更新。使用 put 请求也可以。

创建文档的请求也同样可以使用另外一种请求方式:http://127.0.0.1:9200/shopping/_create/100002,结果相同。

但如果使用 _create 再次发送相同请求,则会提示已经存在当前 id 数据,文档创建失败:

{
    "error": {
        "root_cause": [
            {
                "type": "version_conflict_engine_exception",
                "reason": "[100002]: version conflict, document already exists (current version [1])",
                "index_uuid": "5i7qBmHNSvq5NubfCPyElg",
                "shard": "0",
                "index": "shopping"
            }
        ],
        "type": "version_conflict_engine_exception",
        "reason": "[100002]: version conflict, document already exists (current version [1])",
        "index_uuid": "5i7qBmHNSvq5NubfCPyElg",
        "shard": "0",
        "index": "shopping"
    },
    "status": 409
}

文档的查询

文档的查询也很简单,以刚刚创建文档的数据为例,根据主键(id)进行查询,请求地址:http://127.0.0.1:9200/shopping/_doc/100001

返回结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 2,
    "_seq_no": 2,
    "_primary_term": 1,
    "found": true,
    "_source": {
        "title": "小米手机",
        "category": "小米",
        "images": "https://www.mi.com/",
        "price": 3999.0
    }
}

可以看到 fount 属性为 true,表示查询成功,_source 就表示查询到的数据。

当然,如果查询一个不存在的 id 值,比如发送请求:http://127.0.0.1:9200/shopping/_doc/100012

返回结果:

{
    "_index": "shopping",
    "_id": "100012",
    "found": false
}

fount 属性为 false,表示没有命中请求。

除了使用 id 进行查询之外,还可以使用全数据查询。发送请求:http://127.0.0.1:9200/shopping/_search

返回结果:

{
    "took": 63,
    "timed_out": false,
    "_shards": {
        "total": 1,
        "successful": 1,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": {
            "value": 3,
            "relation": "eq"
        },
        "max_score": 1.0,
        "hits": [
            {
                "_index": "shopping",
                "_id": "QxKDdI0B8aQTV-f6ZJpd",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            },
            {
                "_index": "shopping",
                "_id": "100001",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            },
            {
                "_index": "shopping",
                "_id": "100002",
                "_score": 1.0,
                "_source": {
                    "title": "小米手机",
                    "category": "小米",
                    "images": "https://www.mi.com/",
                    "price": 3999.0
                }
            }
        ]
    }
}

如上,took 是查询的时间,单位为毫秒,timed_out 表示是否超时,hits 就是查询到的数据,一共有三条数据,索引是什么,id 是什么,数据是什么。

文档的修改

全量覆盖

postman 发出一个 PUT 请求:http://127.0.0.1:9200/shopping/_doc/100001,并修改请求体:

{
    "title": "小米手机",
    "category": "小米",
    "images": "https://www.mi.com/",
    "price": 9999.0
}

返回更新成功结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 4,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 5,
    "_primary_term": 2
}

再次查看内容已经被成功修改。

局部修改

局部修改因为不是幂等性的所以需要使用 POST 请求,请求地址为:http://127.0.0.1:9200/shopping/_update/100001

请求体为具体需要修改的属性,如:

{
    "doc": {
        "title": "华为手机"
    }
}

返回结果:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 5,
    "result": "updated",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 6,
    "_primary_term": 2
}

再次查看数据,发送请求:http://127.0.0.1:9200/shopping/_doc/100001

看到数据已经被修改:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 5,
    "_seq_no": 6,
    "_primary_term": 2,
    "found": true,
    "_source": {
        "title": "华为手机",
        "category": "小米",
        "images": "https://www.mi.com/",
        "price": 9999.0
    }
}

文档的删除

删除比较简单,只需要发送 DELETE 请求,请求地址:http://127.0.0.1:9200/shopping/_doc/100001

返回删除成功数据:

{
    "_index": "shopping",
    "_id": "100001",
    "_version": 6,
    "result": "deleted",
    "_shards": {
        "total": 2,
        "successful": 1,
        "failed": 0
    },
    "_seq_no": 7,
    "_primary_term": 2
}
posted @ 2024-02-06 16:18  knqiufan  阅读(21)  评论(0编辑  收藏  举报