Elasticsearch 是一个基于 Apache Lucene 构建的分布式、RESTful 风格的搜索和分析引擎,由 Shay Banon 于 2010 年创建。Elasticsearch 是 ELK Stack(Elasticsearch + Logstash + Kibana)的核心组件,也是全球最流行的全文搜索引擎。
Elasticsearch 的核心定位是 实时搜索与数据分析。它提供了:
Shay Banon 在 2010 年创建了 Elasticsearch,最初是为了给妻子的食谱应用提供搜索功能。Elasticsearch 基于 Apache Lucene,但提供了更易用的 RESTful API 和分布式能力。
# 创建索引
PUT /products
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"name": { "type": "text" },
"price": { "type": "float" },
"category": { "type": "keyword" },
"description": { "type": "text" },
"created_at": { "type": "date" }
}
}
}
# 插入文档
POST /products/_doc/1
{
"name": "iPhone 15 Pro Max",
"price": 999.99,
"category": "Electronics",
"description": "Apple iPhone 15 Pro Max - 256GB",
"created_at": "2024-01-15"
}
# 批量插入
POST /products/_bulk
{"index": {"_id": "2"}}
{"name": "MacBook Pro", "price": 1999.99, "category": "Electronics"}
{"index": {"_id": "3"}}
{"name": "Samsung Galaxy S24", "price": 799.99, "category": "Electronics"}
# 基础搜索
GET /products/_search
{
"query": {
"match": {
"name": "iPhone"
}
}
}
# 多字段搜索
GET /products/_search
{
"query": {
"multi_match": {
"query": "Apple iPhone",
"fields": ["name", "description"]
}
}
}
# 短语搜索
GET /products/_search
{
"query": {
"match_phrase": {
"name": "Pro Max"
}
}
}
# 布尔查询
GET /products/_search
{
"query": {
"bool": {
"must": [
{ "match": { "category": "Electronics" } }
],
"filter": [
{ "range": { "price": { "gte": 500 } } }
]
}
}
}
# 分组聚合
GET /products/_search
{
"size": 0,
"aggs": {
"categories": {
"terms": { "field": "category" }
}
}
}
# 统计聚合
GET /products/_search
{
"size": 0,
"aggs": {
"avg_price": {
"avg": { "field": "price" }
},
"max_price": {
"max": { "field": "price" }
},
"min_price": {
"min": { "field": "price" }
}
}
}
# 日期直方图
GET /logs/_search
{
"size": 0,
"aggs": {
"daily": {
"date_histogram": {
"field": "timestamp",
"calendar_interval": "day"
}
}
}
}
# 嵌套聚合
GET /products/_search
{
"size": 0,
"aggs": {
"categories": {
"terms": { "field": "category" },
"aggs": {
"avg_price": {
"avg": { "field": "price" }
}
}
}
}
}
# 创建带地理位置的索引
PUT /stores
{
"mappings": {
"properties": {
"name": { "type": "text" },
"location": { "type": "geo_point" }
}
}
}
# 插入地理位置数据
POST /stores/_doc/1
{
"name": "Store A",
"location": {
"lat": 39.9,
"lon": 116.4
}
}
# 附近搜索
GET /stores/_search
{
"query": {
"geo_distance": {
"distance": "10km",
"location": {
"lat": 39.9,
"lon": 116.4
}
}
}
}
# 查看集群状态
GET /_cluster/health
# 查看节点信息
GET /_cat/nodes?v
# 查看索引列表
GET /_cat/indices?v
# 查看分片状态
GET /_cat/shards?v
# 索引别名
POST /_aliases
{
"actions": [
{ "add": { "index": "products", "alias": "search_products" } }
]
}
# 热-冷架构(索引生命周期管理)
PUT /_ilm/policy/hot_cold_policy
{
"policy": {
"phases": {
"hot": {
"min_age": "0ms",
"actions": {}
},
"warm": {
"min_age": "30d",
"actions": {}
},
"cold": {
"min_age": "90d",
"actions": {}
},
"delete": {
"min_age": "365d",
"actions": {
"delete": {}
}
}
}
}
}
Elasticsearch 安装、核心概念、CRUD 操作、简单搜索
全文搜索、聚合分析、集群管理、索引生命周期
地理空间查询、性能调优、ELK Stack、安全配置
日志分析平台、电商搜索、APM 监控
Elasticsearch 是数据搜索和分析的瑞士军刀。
从 2010 年的个人项目到全球最流行的搜索引擎,Elasticsearch 的成长令人瞩目。它的 分布式架构、RESTful API、实时搜索能力 让它成为日志分析、应用搜索、安全分析的行业标准。
无论你是做 DevOps、大数据、电商开发还是安全分析,Elasticsearch 都是值得深入学习的核心技术。
"Elasticsearch 让数据搜索变得简单而强大。" 🔍