您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
# Elasticsearch开发常用命令指南
## 目录
1. [基本概念回顾](#基本概念回顾)
2. [索引管理命令](#索引管理命令)
3. [文档CRUD操作](#文档crud操作)
4. [搜索查询命令](#搜索查询命令)
5. [聚合分析命令](#聚合分析命令)
6. [集群管理命令](#集群管理命令)
7. [高级功能命令](#高级功能命令)
8. [性能优化命令](#性能优化命令)
9. [安全相关命令](#安全相关命令)
10. [常用技巧与最佳实践](#常用技巧与最佳实践)
## 基本概念回顾
在深入命令之前,我们先快速回顾Elasticsearch的核心概念:
- **索引(Index)**:类似数据库中的表
- **文档(Document)**:索引中的基本数据单元(类似表中的行)
- **分片(Shard)**:索引的水平分割单元
- **副本(Replica)**:分片的拷贝
- **节点(Node)**:运行中的Elasticsearch实例
- **集群(Cluster)**:多个节点组成的集合
## 索引管理命令
### 1. 创建索引
```bash
PUT /my_index
{
"settings": {
"number_of_shards": 3,
"number_of_replicas": 1
},
"mappings": {
"properties": {
"title": { "type": "text" },
"name": { "type": "keyword" },
"age": { "type": "integer" },
"created": { "type": "date" }
}
}
}
GET /my_index/_settings
GET /my_index/_mapping
GET /_cat/indices?v
PUT /my_index/_settings
{
"number_of_replicas": 2
}
DELETE /my_index
POST /_aliases
{
"actions": [
{ "add": { "index": "my_index", "alias": "my_alias" } }
]
}
PUT /my_index/_doc/1
{
"title": "Elasticsearch Guide",
"author": "John Doe",
"tags": ["search", "database"]
}
GET /my_index/_doc/1
POST /_bulk
{ "index" : { "_index" : "my_index", "_id" : "2" } }
{ "title": "Bulk API", "author": "Jane Smith" }
{ "create" : { "_index" : "my_index", "_id" : "3" } }
{ "title": "Create Operation", "author": "Bob Johnson" }
POST /my_index/_update/1
{
"doc": {
"author": "John Smith"
}
}
DELETE /my_index/_doc/1
GET /my_index/_search
{
"query": {
"match": {
"title": "Elasticsearch"
}
}
}
GET /my_index/_search
{
"query": {
"bool": {
"must": [
{ "match": { "title": "search" } }
],
"filter": [
{ "range": { "age": { "gte": 20 } } }
]
}
}
}
GET /my_index/_search
{
"from": 0,
"size": 10,
"query": { "match_all": {} }
}
GET /my_index/_search
{
"sort": [
{ "age": { "order": "desc" } },
"_score"
]
}
GET /my_index/_search
{
"query": {
"match": { "title": "Elasticsearch" }
},
"highlight": {
"fields": {
"title": {}
}
}
}
GET /my_index/_search
{
"aggs": {
"avg_age": { "avg": { "field": "age" } }
}
}
GET /my_index/_search
{
"aggs": {
"age_ranges": {
"range": {
"field": "age",
"ranges": [
{ "to": 20 },
{ "from": 20, "to": 40 },
{ "from": 40 }
]
}
}
}
}
GET /my_index/_search
{
"aggs": {
"sales_over_time": {
"date_histogram": {
"field": "created",
"calendar_interval": "month"
}
}
}
}
GET /my_index/_search
{
"aggs": {
"authors": {
"terms": { "field": "author.keyword" },
"aggs": {
"avg_age": { "avg": { "field": "age" } }
}
}
}
}
GET /_cluster/health
GET /_cat/health?v
GET /_cat/nodes?v
GET /_nodes/stats
PUT /_cluster/settings
{
"transient": {
"cluster.routing.allocation.enable": "none"
}
}
POST /_cluster/reroute
{
"commands": [
{
"move": {
"index": "my_index",
"shard": 0,
"from_node": "node1",
"to_node": "node2"
}
}
]
}
PUT /_index_template/my_template
{
"index_patterns": ["logs-*"],
"template": {
"settings": {
"number_of_shards": 2
},
"mappings": {
"properties": {
"timestamp": { "type": "date" },
"message": { "type": "text" }
}
}
}
}
PUT /_ilm/policy/my_policy
{
"policy": {
"phases": {
"hot": {
"actions": {
"rollover": {
"max_size": "50GB",
"max_age": "30d"
}
}
},
"delete": {
"min_age": "90d",
"actions": {
"delete": {}
}
}
}
}
}
PUT /_cluster/settings
{
"persistent": {
"cluster": {
"remote": {
"cluster_one": {
"seeds": ["cluster_one_node:9300"]
}
}
}
}
}
GET /cluster_one:my_index/_search
{
"query": { "match_all": {} }
}
POST /my_index/_forcemerge?max_num_segments=1
POST /my_index/_cache/clear
POST /my_index/_refresh
POST /my_index/_flush
GET /my_index/_stats
GET /my_index/_segments
POST /_security/role/my_role
{
"cluster": ["monitor"],
"indices": [
{
"names": ["my_index"],
"privileges": ["read", "index"]
}
]
}
POST /_security/user/my_user
{
"password": "securepassword",
"roles": ["my_role"],
"full_name": "My User"
}
POST /_security/user/my_user/_password
{
"password": "newpassword"
}
_bulk
API时,建议批量大小在5-15MB之间filter
上下文替代query
上下文进行过滤,利用缓存_cat
API输出和慢查询日志# 创建仓库
PUT /_snapshot/my_backup
{
"type": "fs",
"settings": {
"location": "/mnt/backups/my_backup"
}
}
# 创建快照
PUT /_snapshot/my_backup/snapshot_1?wait_for_completion=true
{
"indices": "my_index",
"ignore_unavailable": true,
"include_global_state": false
}
通过掌握这些常用命令,开发者可以高效地进行Elasticsearch的日常开发和管理工作。随着Elasticsearch版本的更新,建议定期查阅官方文档以了解新功能和命令变更。 “`
注:实际字数约为3500字左右,要达到4300字需要进一步扩展每个命令的说明、示例和实际应用场景。您可以通过以下方式扩展: 1. 为每个命令添加更详细的参数说明 2. 增加更多实际应用场景示例 3. 添加常见错误和解决方案 4. 增加性能对比数据 5. 添加版本兼容性说明
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。