Elasticsearch开发常用命令有哪些

发布时间:2021-12-16 11:44:48 作者:小新
来源:亿速云 阅读:197
# 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" }
    }
  }
}

2. 查看索引信息

GET /my_index/_settings
GET /my_index/_mapping
GET /_cat/indices?v

3. 修改索引设置

PUT /my_index/_settings
{
  "number_of_replicas": 2
}

4. 删除索引

DELETE /my_index

5. 索引别名操作

POST /_aliases
{
  "actions": [
    { "add": { "index": "my_index", "alias": "my_alias" } }
  ]
}

文档CRUD操作

1. 创建/更新文档

PUT /my_index/_doc/1
{
  "title": "Elasticsearch Guide",
  "author": "John Doe",
  "tags": ["search", "database"]
}

2. 获取文档

GET /my_index/_doc/1

3. 批量操作

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" }

4. 更新部分字段

POST /my_index/_update/1
{
  "doc": {
    "author": "John Smith"
  }
}

5. 删除文档

DELETE /my_index/_doc/1

搜索查询命令

1. 基本搜索

GET /my_index/_search
{
  "query": {
    "match": {
      "title": "Elasticsearch"
    }
  }
}

2. 复合查询

GET /my_index/_search
{
  "query": {
    "bool": {
      "must": [
        { "match": { "title": "search" } }
      ],
      "filter": [
        { "range": { "age": { "gte": 20 } } }
      ]
    }
  }
}

3. 分页查询

GET /my_index/_search
{
  "from": 0,
  "size": 10,
  "query": { "match_all": {} }
}

4. 排序查询

GET /my_index/_search
{
  "sort": [
    { "age": { "order": "desc" } },
    "_score"
  ]
}

5. 高亮显示

GET /my_index/_search
{
  "query": {
    "match": { "title": "Elasticsearch" }
  },
  "highlight": {
    "fields": {
      "title": {}
    }
  }
}

聚合分析命令

1. 指标聚合

GET /my_index/_search
{
  "aggs": {
    "avg_age": { "avg": { "field": "age" } }
  }
}

2. 桶聚合

GET /my_index/_search
{
  "aggs": {
    "age_ranges": {
      "range": {
        "field": "age",
        "ranges": [
          { "to": 20 },
          { "from": 20, "to": 40 },
          { "from": 40 }
        ]
      }
    }
  }
}

3. 日期直方图

GET /my_index/_search
{
  "aggs": {
    "sales_over_time": {
      "date_histogram": {
        "field": "created",
        "calendar_interval": "month"
      }
    }
  }
}

4. 嵌套聚合

GET /my_index/_search
{
  "aggs": {
    "authors": {
      "terms": { "field": "author.keyword" },
      "aggs": {
        "avg_age": { "avg": { "field": "age" } }
      }
    }
  }
}

集群管理命令

1. 查看集群健康状态

GET /_cluster/health
GET /_cat/health?v

2. 查看节点信息

GET /_cat/nodes?v
GET /_nodes/stats

3. 分片分配控制

PUT /_cluster/settings
{
  "transient": {
    "cluster.routing.allocation.enable": "none"
  }
}

4. 集群重新路由

POST /_cluster/reroute
{
  "commands": [
    {
      "move": {
        "index": "my_index",
        "shard": 0,
        "from_node": "node1",
        "to_node": "node2"
      }
    }
  ]
}

高级功能命令

1. 索引模板

PUT /_index_template/my_template
{
  "index_patterns": ["logs-*"],
  "template": {
    "settings": {
      "number_of_shards": 2
    },
    "mappings": {
      "properties": {
        "timestamp": { "type": "date" },
        "message": { "type": "text" }
      }
    }
  }
}

2. 生命周期管理(ILM)

PUT /_ilm/policy/my_policy
{
  "policy": {
    "phases": {
      "hot": {
        "actions": {
          "rollover": {
            "max_size": "50GB",
            "max_age": "30d"
          }
        }
      },
      "delete": {
        "min_age": "90d",
        "actions": {
          "delete": {}
        }
      }
    }
  }
}

3. 跨集群搜索

PUT /_cluster/settings
{
  "persistent": {
    "cluster": {
      "remote": {
        "cluster_one": {
          "seeds": ["cluster_one_node:9300"]
        }
      }
    }
  }
}

GET /cluster_one:my_index/_search
{
  "query": { "match_all": {} }
}

性能优化命令

1. 强制合并段

POST /my_index/_forcemerge?max_num_segments=1

2. 清除缓存

POST /my_index/_cache/clear

3. 刷新与冲刷控制

POST /my_index/_refresh
POST /my_index/_flush

4. 索引性能分析

GET /my_index/_stats
GET /my_index/_segments

安全相关命令

1. 创建角色

POST /_security/role/my_role
{
  "cluster": ["monitor"],
  "indices": [
    {
      "names": ["my_index"],
      "privileges": ["read", "index"]
    }
  ]
}

2. 创建用户

POST /_security/user/my_user
{
  "password": "securepassword",
  "roles": ["my_role"],
  "full_name": "My User"
}

3. 修改密码

POST /_security/user/my_user/_password
{
  "password": "newpassword"
}

常用技巧与最佳实践

  1. 批量导入数据:使用_bulkAPI时,建议批量大小在5-15MB之间
  2. 查询优化:使用filter上下文替代query上下文进行过滤,利用缓存
  3. 索引设计:合理设置分片数(通常每个分片20-40GB)
  4. 监控:定期检查_catAPI输出和慢查询日志
  5. 备份:使用快照和恢复功能定期备份重要数据
# 创建仓库
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. 添加版本兼容性说明

推荐阅读:
  1. Elasticsearch有什么用
  2. java开发中Git常用命令有哪些

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

elasticsearch

上一篇:如何分析Serverless 中函数计算的可观测性

下一篇:Linux sftp命令的用法是怎样的

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》