Elasticsearch(二):Elasticsearch入门操作


大家好,我是欧阳方超,可以扫描下方二维码关注我的公众号“欧阳方超”,后续内容将在公众号首发。
在这里插入图片描述
在这里插入图片描述

1、概述

Elasticsearch 是一个基于 Lucene 的分布式搜索引擎,广泛用于实时数据分析和全文搜索。它提供了强大的 RESTful API,使用户能够通过简单的 HTTP 请求进行数据存储和检索。本篇介绍一下elasticsearch的基本操作,需要说明的是直接在命令行操作,可以会遇到下面的问题,可以考虑使用 -k 或 --insecure 选项,告诉 curl 跳过证书验证。虽然在测试中很有用,但由于安全风险,不建议在生产环境中使用。

curl: (60) Peer's certificate issuer has been marked as not trusted by the user.
More details here: http://curl.haxx.se/docs/sslcerts.html

curl performs SSL certificate verification by default, using a "bundle"
 of Certificate Authority (CA) public keys (CA certs). If the default
 bundle file isn't adequate, you can specify an alternate file
 using the --cacert option.
If this HTTPS server uses a certificate signed by a CA represented in
 the bundle, the certificate verification probably failed due to a
 problem with the certificate (it might be expired, or the name might
 not match the domain name in the URL).
If you'd like to turn off curl's verification of the certificate, use
 the -k (or --insecure) option.

2、常用操作

2.1、查看集群状态

在使用 Elasticsearch 之前,首先要了解集群(本次以单节点为例)的健康状态。可以使用以下命令查看集群的健康状况:

[esuser@localhost config]$ curl -X GET -u elastic:123456 -k "https://192.168.25.136:9200/_cluster/health?pretty"
{
  "cluster_name" : "elasticsearch",
  "status" : "yellow",
  "timed_out" : false,
  "number_of_nodes" : 1,
  "number_of_data_nodes" : 1,
  "active_primary_shards" : 2,
  "active_shards" : 2,
  "relocating_shards" : 0,
  "initializing_shards" : 0,
  "unassigned_shards" : 1,
  "delayed_unassigned_shards" : 0,
  "number_of_pending_tasks" : 0,
  "number_of_in_flight_fetch" : 0,
  "task_max_waiting_in_queue_millis" : 0,
  "active_shards_percent_as_number" : 66.66666666666666
}

2.2、查看所有索引

要查看当前集群中的所有索引,可以使用以下命令:

[esuser@localhost config]$ curl -X GET -u elastic:123456 -k "https://192.168.25.136:9200/_cat/indices?v"
health status index      uuid                   pri rep docs.count docs.deleted store.size pri.store.size
yellow open   test-index CgiFnTGbSgqJZFIdBpJelQ   1   1          0            0       225b           225b

2.3、删除索引

如果不再需要某个索引,可以使用以下命令将其删除:

[esuser@localhost config]$ curl -X DELETE -u elastic:123456 -k "https://192.168.25.136:9200/test-index"
{"acknowledged":true}[esuser@localhost config]$

2.4、创建索引

创建一个新的索引是数据存储的第一步。以下命令创建一个名为test-index 的索引:

[esuser@localhost config]$ curl -X PUT -u elastic:123456 -k "https://192.168.25.136:9200/test-index"
{"acknowledged":true,"shards_acknowledged":true,"index":"test_index"}

2.5、向索引中添加文档

创建索引后可以向其中添加文档。以下命令向test-index索引中添加了一个文档:

[esuser@localhost config]$ curl -X POST -u elastic:123456 -k  "https://192.168.25.136:9200/test-index/_doc/1" -H 'Content-Type: application/json' -d'
{
  "title": "Elasticsearch Basics",
  "author": "John Doe",
  "published_date": "2024-01-01"
}'

返回值:

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

上面的命令中向test-index索引添加了一个文档,并指定了文档id(为1),当然也可以不指定文档id,不指定时文档id将由elasticsearch自动生成。

注意,在elasticsearch 7.0 以及之后的版本中Type(类型)被废弃了。一个 index 中只有一个默认的 type,即 _doc。type 被废弃后,ES中库表合一。
_doc 的角色:
在 Elasticsearch 7.0 及以后版本中,_doc 不再表示文档类型(type)。
它现在是一个固定的端点名称,用于文档操作。
为什么保留 _doc:
兼容性:保留 _doc 可以让 API 结构保持一致,便于从旧版本迁移。
清晰性:它明确表示这是一个文档操作,而不是其他类型的操作(如索引设置)。

2.6、查询文档

添加文档后可根据指定文档id查询文档:

[esuser@localhost config]$ curl -X GET -u elastic:123456 -k "https://192.168.25.136:9200/test-index/_doc/1"
{"_index":"test-index","_id":"1","_version":1,"_seq_no":2,"_primary_term":1,"found":true,"_source":
{
  "title": "Elasticsearch Basics",
  "author": "John Doe",
  "published_date": "2024-01-01"
}}

2.7、查询索引内所有文档

还可以查询某索引内的所有文档,命令如下:

[esuser@localhost config]$ curl -X GET -u elastic:123456 -k 'https://192.168.25.136:9200/test-index/_search' -H 'Content-Type: application/json' -d '{
  "query": {
    "match_all": {}
  }
}'

返回值:

{"took":2,"timed_out":false,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0},"hits":{"total":{"value":2,"relation":"eq"},"max_score":1.0,"hits":[{"_index":"test-index","_id":"1","_score":1.0,"_source":
{
  "title": "Elasticsearch Basics",
  "author": "John Doe",
  "published_date": "2024-01-01"
}},{"_index":"test-index","_id":"2","_score":1.0,"_source":
{
  "title": "Elasticsearch Basics2",
  "author": "John Doe2",
  "published_date": "2024-01-01"
}}]}}

2.8、更新文档

使用post请求和_update API实现部分更新操作,这种方法只更新指定的字段,其他字段的值将保持不变。

[esuser@localhost config]$ curl -X POST -u elastic:123456 -k 'https://192.168.25.136:9200/test-index/_update/2' -H 'Content-Type: application/json' -d '{
  "doc": {
    "author": "John Doe update"
  }
}'

返回值

{"_index":"test-index","_id":"2","_version":2,"result":"updated","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":6,"_primary_term":1}

执行完后,文档2将从

{
                "_index": "test-index",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "Elasticsearch Basics2",
                    "author": "John Doe",
                    "published_date": "2024-01-01"
                }
            }

变为

{
                "_index": "test-index",
                "_id": "2",
                "_score": 1.0,
                "_source": {
                    "title": "Elasticsearch Basics2",
                    "author": "John Doe update",
                    "published_date": "2024-01-01"
                }
            }

2.9、计算文档总数

如果想获取文档的总数,可以使用 Elasticsearch 的 count API

[esuser@localhost config]$ curl -X GET -u elastic:123456 -k "https://192.168.25.136:9200/test-index/_count"

返回值:

{"count":2,"_shards":{"total":1,"successful":1,"skipped":0,"failed":0}}

2.10、删除文档

下面的命令将删除文档id为2的文档:

[esuser@localhost config]$ curl -X DELETE -u elastic:123456 -k "https://192.168.25.136:9200/test-index/_doc/2"

返回值:

{"_index":"test-index","_id":"2","_version":3,"result":"deleted","_shards":{"total":2,"successful":1,"failed":0},"_seq_no":7,"_primary_term":1}

3、总结

本文介绍了 Elasticsearch 的一些基本操作,包括查看集群状态、创建索引、添加、查询、更新和删除文档等。通过这些操作,可以快速入门并开始使用 Elasticsearch 进行数据存储和检索。
我是欧阳方超,把事情做好了自然就有兴趣了,如果你喜欢我的文章,欢迎点赞、转发、评论加关注。我们下次见。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值