本文由 简悦 SimpRead 转码, 原文地址 blog.csdn.net
特别指出,这个最新的 Elastic Stack 8.16 发布版的功能。我很兴奋地来尝试这个功能。在今天的文章中,我们来使用一个例子来进行详细地描述。
安装
如果你还没有安装好自己的 Elasticsearch 及 Kibana,请参考如下的文章来进行安装:
你在选择安装的时候,请选择 8.x 的文档来进行安装。特别指出的是,你需要安装 Elastic Stack 8.16,因为只有这个版本才开始有这个功能。在安装的时候,我们也可以启动 SSL 安全访问。
展示
我们启动 Kibana,并进入到 DevTools console 中:
如上所示,我们打入如上的命令/请求:
1. PUT twitter/_doc/1
2. {
3. "content": "This is Xiaoguo from Elasetic"
4. }
上面的命令被用来写入一个文档到 twitter 索引之中。我们接下来使用如下的操作:
我们选择 Python,并拷贝代码:
1. import os
2. from elasticsearch import Elasticsearch
4. client = Elasticsearch(
5. hosts=["https://192.168.101.107:9200"],
6. api_key=os.getenv("ELASTIC_API_KEY"),
7. )
9. resp = client.index(
10. index="twitter",
11. id="1",
12. document={
13. "content": "This is Xiaoguo from Elasetic"
14. },
15. )
16. print(resp)
上面就是我们用来生成 twitter 索引,并写入文档到 Elasticsearch 中的代码。
细心的开发者可以发现,如果我们使用这个代码,并对没有安全设置的 Elasticsearch 安装来说是没有任何问题的。如果你的安装已经启用了 HTTPS 安全访问那么,我们该如何进行修改呢?
我们首先参考我之前写的文章 “Elasticsearch:关于在 Python 中使用 Elasticsearch 你需要知道的一切 - 8.x”。我们需要对代码进行修改。
拷贝 Elasticsearch 证书
我们执行如下的命令把 Elasticsearch 的证书拷贝到当前的目录中:
1. $ pwd
2. /Users/liuxg/python/console
3. $ cp ~/elastic/elasticsearch-8.16.0/config/certs/http_ca.crt .
4. $ ls
5. http_ca.crt
创建 API key
我们可以按照如下的步骤来获取 API key:
我们拷贝上面的 key 并在下面的代码中进行使用。
创建 Python 代码并修改
我们在当前的目录下创建 main.py 文件,并把之前拷贝的代码粘贴进去。我们需要做相应的修改:
main.py
1. import os
2. from elasticsearch import Elasticsearch
5. client = Elasticsearch(
6. hosts=["https://192.168.101.107:9200"],
7. api_key="eGhLNk41TUJ2NGtlT1d0R3R6Z3I6LWp0ckxuY0xSU09jaDAwVHNISmZUZw==",
8. ca_certs="./http_ca.crt",
9. verify_certs = True
10. )
12. print(client.info())
14. resp = client.index(
15. index="twitter",
16. id="2",
17. document={
18. "content": "This is Xiaoguo from Elasetic"
19. },
20. )
22. print(resp)
为了测试的方便,我们使用了不同的文档 id (2) 来进行测试。我们运行上面的脚本:
1. $ pwd
2. /Users/liuxg/python/console
3. $ ls
4. http_ca.crt main.py
5. $ python3 main.py
6. {'name': 'liuxgm.local', 'cluster_name': 'elasticsearch', 'cluster_uuid': 'HF3DAYNQSnOq0D1NmsNubg', 'version': {'number': '8.16.0', 'build_flavor': 'default', 'build_type': 'tar', 'build_hash': '12ff76a92922609df4aba61a368e7adf65589749', 'build_date': '2024-11-08T10:05:56.292914697Z', 'build_snapshot': False, 'lucene_version': '9.12.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}
7. {'_index': 'twitter', '_id': '2', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 1, '_primary_term': 1}
上面显示我们的代码已经成功地运行了。
我们使用 Kibana 来查看我们已经写入的文档:
从上面的输出中,我们可以看出来,我们已经成功地写入了一个 id 为 2 的文档。
按照同样的方法,我们下面来使用 bulk 请求来写入一个 id 为 3 的文档。我们得到的代码是:
1. import os
2. from elasticsearch import Elasticsearch
4. client = Elasticsearch(
5. hosts=["https://192.168.101.107:9200"],
6. api_key=os.getenv("ELASTIC_API_KEY"),
7. )
9. resp = client.bulk(
10. operations=[
11. {
12. "index": {
13. "_index": "twitter",
14. "_id": "3"
15. }
16. },
17. {
18. "content": "this is from bulk"
19. }
20. ],
21. )
22. print(resp)
我们把上面的代码写入到之前的 main.py 中:
main.py
1. import os
2. from elasticsearch import Elasticsearch
5. client = Elasticsearch(
6. hosts=["https://192.168.101.107:9200"],
7. api_key="eGhLNk41TUJ2NGtlT1d0R3R6Z3I6LWp0ckxuY0xSU09jaDAwVHNISmZUZw==",
8. ca_certs="./http_ca.crt",
9. verify_certs = True
10. )
12. print(client.info())
14. resp = client.index(
15. index="twitter",
16. # id="2",
17. document={
18. "content": "This is Xiaoguo from Elastic"
19. },
20. )
22. print(resp)
24. resp = client.bulk(
25. operations=[
26. {
27. "index": {
28. "_index": "twitter",
29. "_id": "3"
30. }
31. },
32. {
33. "content": "this is from bulk"
34. }
35. ],
36. )
38. print(resp)
请注意,这次我们写入文档时,我们在第一个写入中没有指定 id 这个字段(避免写入同样 id 2 造成的问题)。运行上面的代码:
1. $ pwd
2. /Users/liuxg/python/console
3. $ ls
4. http_ca.crt main.py
5. $ python3 main.py
6. {'name': 'liuxgm.local', 'cluster_name': 'elasticsearch', 'cluster_uuid': 'HF3DAYNQSnOq0D1NmsNubg', 'version': {'number': '8.16.0', 'build_flavor': 'default', 'build_type': 'tar', 'build_hash': '12ff76a92922609df4aba61a368e7adf65589749', 'build_date': '2024-11-08T10:05:56.292914697Z', 'build_snapshot': False, 'lucene_version': '9.12.0', 'minimum_wire_compatibility_version': '7.17.0', 'minimum_index_compatibility_version': '7.0.0'}, 'tagline': 'You Know, for Search'}
7. {'_index': 'twitter', '_id': 'yBLFN5MBv4keOWtGQDiG', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 2, '_primary_term': 1}
8. {'errors': False, 'took': 0, 'items': [{'index': {'_index': 'twitter', '_id': '3', '_version': 1, 'result': 'created', '_shards': {'total': 2, 'successful': 1, 'failed': 0}, '_seq_no': 3, '_primary_term': 1, 'status': 201}}]}
我们使用 Kibana 进行查看:
我们看见两个最新写入的文档。
我们可以使用同样的方法来针对 Nodejs 进行操作。这个留给你们自己进行尝试。针对 Nodejs 的 Elasticsearch 连接,你可以参考文章 “Elasticsearch:使用最新的 Nodejs client 8.x 来创建索引并搜索”。