Elasticsearch是一款分布式的全文搜索和分析引擎,基于Lucene构建。它具有实时搜索、稳定、扩展性强等特点,被广泛用于日志分析、全文搜索、业务数据分析等场景。本文将深入介绍Elasticsearch的基础使用和高阶使用,包括安装配置、基本操作、高级查询、集群管理、性能优化等内容。
Elasticsearch 是一个开源的搜索引擎,基于Lucene库构建。它支持RESTful API,具有实时搜索能力和高扩展性,能够处理PB级别的数据量。
可以在官方网站下载Elasticsearch安装包,支持多种操作系统。也可以通过Docker、Homebrew等方式安装。
下载和解压:
wget https://artifacts.elastic.co/downloads/elasticsearch/elasticsearch-7.10.1-linux-x86_64.tar.gz tar -xzf elasticsearch-7.10.1-linux-x86_64.tar.gz cd elasticsearch-7.10.1 启动Elasticsearch:
./bin/elasticsearch 验证安装:
 打开浏览器访问http://localhost:9200,如果安装成功会返回集群的基本信息。
Elasticsearch的配置文件位于config/elasticsearch.yml。常用配置包括:
集群名称:
cluster.name: my-cluster 节点名称:
node.name: node-1 数据存储路径:
path.data: /path/to/data 日志存储路径:
path.logs: /path/to/logs 网络配置:
network.host: 0.0.0.0 http.port: 9200 使用PUT请求创建索引:
curl -X PUT "localhost:9200/my_index?pretty" 添加文档:
curl -X POST "localhost:9200/my_index/_doc/1?pretty" -H 'Content-Type: application/json' -d' {   "name": "John Doe",   "age": 30,   "about": "I love to go rock climbing" } ' 更新文档:
curl -X POST "localhost:9200/my_index/_update/1?pretty" -H 'Content-Type: application/json' -d' {   "doc": {     "age": 31   } } ' 删除文档:
curl -X DELETE "localhost:9200/my_index/_doc/1?pretty" 匹配查询:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "query": {     "match": {       "about": "rock climbing"     }   } } ' 精确查询:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "query": {     "term": {       "age": 31     }   } } ' 布尔查询:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "query": {     "bool": {       "must": [         { "match": { "about": "climbing" } }       ],       "filter": [         { "term": { "age": 31 } }       ]     }   } } ' 范围查询:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "query": {     "range": {       "age": {         "gte": 30,         "lte": 40       }     }   } } ' 求平均值:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "aggs": {     "average_age": {       "avg": {         "field": "age"       }     }   },   "size": 0 } ' 分组统计:
curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "aggs": {     "age_groups": {       "terms": {         "field": "age"       }     }   },   "size": 0 } ' curl -X GET "localhost:9200/my_index/_search?pretty" -H 'Content-Type: application/json' -d' {   "query": {     "script_score": {       "query": { "match_all": {} },       "script": {         "source": "doc[\'age\'].value * params.factor",         "params": { "factor": 1.2 }       }     }   } } ' 集群名称:
cluster.name: my-cluster 节点名称:
node.name: node-1 初始主节点:
discovery.seed_hosts: ["host1", "host2"] cluster.initial_master_nodes: ["node-1", "node-2"] 添加节点:
 在新节点上进行相应配置,使其加入集群。
移除节点:
 使用API将节点从集群中移除:
curl -X POST "localhost:9200/_cluster/voting_config_exclusions?node_names=node-1" 查看索引状态:
curl -X GET "localhost:9200/_cat/indices?v" 关闭索引:
curl -X POST "localhost:9200/my_index/_close" 打开索引:
curl -X POST "localhost:9200/my_index/_open" 分片和副本配置:
 根据数据量合理配置分片和副本数量。
使用别名:
 通过别名管理索引,方便索引切换和升级。
使用search_after或scroll API替代深分页。
JVM内存配置:
 调整JVM内存配置,确保Elasticsearch运行在最佳状态。
磁盘I/O优化:
 使用SSD磁盘提高I/O性能,减少延迟。
在实际应用中,Elasticsearch被广泛用于日志分析、全文搜索和业务数据分析。以下是几个典型案例:
使用Elasticsearch结合Logstash和Kibana(ELK Stack)进行日志收集、处理和可视化分析,实时监控系统运行状态。
电商网站使用Elasticsearch实现产品搜索,通过多条件查询和过滤,提高搜索准确性和用户体验。
金融行业使用Elasticsearch存储和分析交易数据,实时监控交易异常,提升风控能力。
通过本文的介绍,您应该对Elasticsearch的基础使用和高阶使用有了全面的了解。无论是索引管理、查询优化还是集群管理,Elasticsearch都提供了强大的功能和工具。通过合理配置和优化,您可以充分发挥Elasticsearch的潜力,为业务提供高效可靠的数据支持。