Elasticsearch 9.5.x
以下是面向 Elasticsearch 9.5.x(当前最新主线) 的上手教程。9.5 引入了列模式 Columnar Mode、VectorDB 索引模式、自动校准、多模态语义字段、原生 PromQL GA、Agent Builder 增强等能力。
一、Docker 快速启动
开发环境(单节点,关闭安全认证)
docker run -d --name es9 \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms2g -Xmx2g" \
-e "xpack.security.enabled=false" \
docker.elastic.co/elasticsearch/elasticsearch:9.5.5验证:
curl http://localhost:9200返回包含 "version": {"number": "9.5.5"} 即正常。
生产环境(开启安全认证)
docker network create elastic
docker run -d --name es01 --net elastic \
-p 9200:9200 -p 9300:9300 \
-e "discovery.type=single-node" \
-e "ES_JAVA_OPTS=-Xms2g -Xmx2g" \
-e "xpack.security.enabled=true" \
-v es-data:/usr/share/elasticsearch/data \
docker.elastic.co/elasticsearch/elasticsearch:9.5.5首次启动后获取密码:
docker exec -it es01 /usr/share/elasticsearch/bin/elasticsearch-reset-password -u elastic二、连接与基础操作
# 使用 curl
curl -u elastic:你的密码 https://localhost:9200
# 或使用 Kibana Dev Tools(推荐)创建索引
PUT /products
{
"settings": { "number_of_shards": 1 },
"mappings": {
"properties": {
"name": { "type": "text", "analyzer": "standard" },
"price": { "type": "float" },
"tags": { "type": "keyword" },
"created": { "type": "date" }
}
}
}写入文档
POST /products/_doc/1
{
"name": "智能手表",
"price": 1299,
"tags": ["电子", "穿戴"],
"created": "2026-11-05T10:00:00Z"
}搜索
GET /products/_search
{
"query": {
"bool": {
"must": [{ "match": { "name": "手表" } }],
"filter": [{ "range": { "price": { "gte": 1000 } } }]
}
}
}聚合
GET /products/_search
{
"size": 0,
"aggs": {
"avg_price": { "avg": { "field": "price" } }
}
}三、9.5 新特性体验
1. VectorDB 索引模式(向量搜索开箱即用)
PUT /embeddings
{
"settings": {
"index.mode": "vectordb"
},
"mappings": {
"properties": {
"text": { "type": "semantic_text" },
"vector": { "type": "dense_vector", "dims": 384 }
}
}
}index.mode: "vectordb" 会自动调优量化、合并策略和缓存,无需手动配置 HNSW 参数。
2. 列模式 Columnar Mode(技术预览)
PUT /logs
{
"settings": {
"index.mode": "columnar"
}
}字段默认以列式存储且不建倒排索引,大幅降低存储占用,适合日志、指标等分析型场景。
3. 原生 PromQL(GA)
在 ES|QL 中直接运行 PromQL 查询,可将现有 Grafana 仪表板无缝迁移到 Elasticsearch。
FROM prometheus()
| EVAL cpu_usage = promql("rate(node_cpu_seconds_total[5m])")4. 多模态语义字段
PUT /media
{
"mappings": {
"properties": {
"image_desc": { "type": "semantic", "modalities": ["image"] }
}
}
}支持图像、音频、视频、PDF 的语义搜索,由 jina-embeddings-v5-omni 驱动。
四、生产部署要点
- Linux 内核参数:
vm.max_map_count必须 ≥ 262144。 - JVM 堆内存:
-Xms和-Xmx设为相同值,建议不超过物理内存的 50%。 - 禁用 Swap:或设置
bootstrap.memory_lock=true。 - 分片规划:单分片控制在 30–50GB,避免单节点超过 2000 个分片。
- 禁止深分页:导出用
search_after+ PIT,不要用from + size翻万页。 - 批量写入:使用
_bulkAPI,单次 5–15MB 或 1000–5000 条。 - Mapping 预定义:设置
"dynamic": "strict",避免动态映射产生垃圾字段。 - ILM 生命周期:Hot → Warm → Cold → Delete 自动管理日志索引。
五、Python 示例
from elasticsearch import Elasticsearch
es = Elasticsearch(
"http://localhost:9200",
basic_auth=("elastic", "你的密码")
)
# 创建索引
es.indices.create(index="products", body={
"mappings": {
"properties": {
"name": {"type": "text"},
"price": {"type": "float"}
}
}
})
# 写入
es.index(index="products", id=1, body={
"name": "智能手表", "price": 1299
})
# 搜索
res = es.search(index="products", body={
"query": {"match": {"name": "手表"}}
})
for hit in res["hits"]["hits"]:
print(hit["_source"])六、适用场景
适合全文搜索、日志/指标/链路可观测性、向量检索 + RAG、安全分析 SIEM、Prometheus 指标存储;不适合高频事务 OLTP、强一致性多表更新。