前言

Elasticsearch 和 Solr 都是开源搜索引擎,那么在使用时该如何选择?
如果应用使用的是 JSON,那么 Elasticsearch 是一个更好的选择。
否则使用 Solr,因为 schema.xml 和 solrconfig.xml 都有很好的文档记录。
easy-es

Elasticsearch 介绍(ES)

Elasticsearch 是一个全文检索(非结构化数据)服务器
Elasticsearch 官网
Elasticsearch 是面向文档型数据库,一条数据在这里就是一个文档。

Elasticsearch 的安装

下载

9300 端口为 Elasticsearch 集群间组件的通信端口
9200 端口为浏览器访问的 Http 协议 RESTful 端口
进入 bin 文件目录,点击 elasticsearch.bat 文件启动 ES 服务
输入地址:http://localhost:9200,测试结果

Elasticsearch 的启动

Elasticsearch 自 8.0 版本起默认开启 TLS/SSL,强制要求所有 HTTP 流量必须通过 HTTPS 加密传输
让 Windows 系统信任 Elasticsearch 的自签名 CA
步骤:打开 PowerShell(管理员)
certutil -addstore -f "ROOT" "D:\elasticsearch-9.2.2\config\certs\http_ca.crt"
启动 curl --cacert "D:\elasticsearch-9.2.2\config\certs\http_ca.crt" -u elastic https://localhost:9200

重置 elastic 用户密码
cd D:\elasticsearch-9.2.2
bin\elasticsearch-reset-password -u elastic
Windows 系统
bin\elasticsearch-reset-password.bat -u elastic
改为自己记得住的强密码,可以运行:
D:\elasticsearch-9.2.2\bin\elasticsearch-reset-password.bat -i -u elastic

使用 -k(仅限本地开发)
curl -k -u elastic https://localhost:9200

Elasticsearch 在Java 中的配置

1
2
3
4
5
6
<!-- https://www.elastic.co/docs/reference/elasticsearch/clients/java/getting-started -->
<dependency>
<groupId>co.elastic.clients</groupId>
<artifactId>elasticsearch-java</artifactId>
<version>9.2.2</version>
</dependency>

重新生成有效的 http.p12

http.p12 和 transport.p12 是 Elasticsearch(ES) 在启用安全功能(X-Pack Security)后自动生成或手动配置的两种 PKCS#12 格式证书文件,分别用于不同通信场景下的 TLS 加密与身份认证。它们在 Elasticsearch 集群的安全架构中扮演关键角色。

查看证书keytool -list -v -keystore http.p12 -storetype PKCS12
删除当前无效的 http.p12 Remove-Item D:\elasticsearch-9.2.2\config\certs\http.p12
使用 Elasticsearch 官方工具重新生成

1
2
3
4
5
# 进入 ES bin 目录
cd D:\elasticsearch-9.2.2\bin

# 生成 HTTP 证书(自动包含有效私钥+证书,密码为空)
.\elasticsearch-certutil http --silent --ip 127.0.0.1 --dns localhost

重命名并放置到 config/certs/
mv http.p12 D:\elasticsearch-9.2.2\config\certs\http.p12

1
2
cd D:\elasticsearch-9.2.2\bin
.\elasticsearch-certutil http
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
## 是否生成 CSR?(通常选 N)
Generate a certificate signing request (CSR)? [y/N] N

## 是否使用现有 CA?(如果你已有 CA,比如 elastic-stack-ca.p12,选 Y)
Use an existing CA? [y/N] Y

## 输入 CA 文件路径(注意:是 .p12 格式的 CA 文件)
Enter path to CA file: ..\config\certs\elastic-stack-ca.p12

## 输入 CA 密码(如果之前设了密码;ES 默认为空,直接回车)
Enter password for CA: [直接按 Enter]

## 设置证书有效期(例如 10 年)
For how long should your certificate be valid? [5y] 10y

## 是否为每个节点生成单独证书?(单机测试选 N)
Generate one certificate per node? [y/N] N

## 输入要保护的主机名或 IP(每行一个,空行结束)
Enter all the hostnames that you need to generate certificates for.
Press ENTER when you are done:
> localhost
> 127.0.0.1
> [再按一次 Enter 结束输入]

## 是否需要修改配置?(一般选 N)
Do you wish to change any of these options? [y/N] N

## 设置私钥密码(建议留空,直接回车)
Enter a password for the private key(s). If you leave this blank, the private key(s) will be unencrypted:
[直接按 Enter]

7.17.10(2025)

弄了半天还得是这个版本。