Elasticsearch 操作命令详解

Elasticsearch 提供了一套强大的 RESTful API,允许你通过 HTTP 请求来进行各种操作,如索引(增加)、删除、更新文档,以及执行复杂的查询。以下是详细的命令介绍,包括增删改查、多条件查询、分页、聚合等。

戳底部名片,一起变现

1. 索引(Index)操作 - 增加文档

创建或更新单个文档

使用 PUT 方法向指定的索引中添加或更新一个文档:

curl -X PUT "localhost:9200/index_name/_doc/doc_id" -H 'Content-Type: application/json' -d'
{
  "field1": "value1",
  "field2": "value2"
}'

如果 doc_id 已存在,则会更新该文档;如果不存在,则会创建新文档。

批量索引

使用 _bulk API 可以一次处理多个文档:

curl -X POST "localhost:9200/_bulk?pretty" -H 'Content-Type: application/x-ndjson' -d'
{ "index" : { "_index" : "index_name", "_id" : "1" } }
{ "field1" : "value1" }
{ "index" : { "_index" : "index_name", "_id" : "2" } }
{ "field2" : "value2" }
'
2. 删除操作

删除单个文档

使用 DELETE 方法根据 ID 删除指定的文档:

curl -X DELETE "localhost:9200/index_name/_doc/doc_id"

删除整个索引

删除一个完整的索引及其所有数据:

curl -X DELETE "localhost:9200/index_name"
3. 更新操作

部分更新文档

使用 _update API 来对现有文档的部分字段进行更新:

curl -X POST "localhost:9200/index_name/_update/doc_id" -H 'Content-Type: application/json' -d'
{
  "doc": {
    "field1": "new_value1"
  }
}'

全量更新文档

如果你想替换整个文档,可以使用 POST 方法并提供完整的文档内容:

curl -X POST "localhost:9200/index_name/_doc/doc_id" -H 'Content-Type: application/json' -d'
{
  "field1": "new_value1",
  "field2": "new_value2"
}'
4. 查询操作

简单匹配查询

使用 GET 方法结合 _search API 进行简单的全文搜索:

curl -X GET "localhost:9200/index_name/_search?q=field:value"

结构化查询

构建更复杂的查询语句,例如布尔查询:

curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "must": [
        { "match": { "field1": "value1" } },
        { "match": { "field2": "value2" } }
      ]
    }
  }
}'

多条件查询

组合多个查询条件,比如使用 shouldmust_not 子句:

curl -X GET "localhost:9200/index_name/_search" -H 'Content-Type: application/json' -d'
{
  "query": {
    "bool": {
      "should": [
        { "match": { "field1": "value1" } },
        { "match": { "field2": "value2" } }
      ],
      "must_not": [
        { "term": { "status": "deleted" } }
      ]
    }
  }
}'
5. 分页查询

从头开始分页

使用 fromsize 参数来控制结果集的起始位置和返回的数量:

curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "from": 0,
  "size": 10,
  "query": {
    "match_all": {}
  }
}'

深度分页

对于深层分页,建议使用 scroll API 或者 search_after 参数以提高性能:

# 使用 scroll API 获取大量数据
curl -X GET "localhost:9200/index_name/_search?scroll=1m&size=100" -H 'Content-Type: application/json' -d'
{
  "query": {
    "match_all": {}
  }
}'

# 使用 search_after 参数
curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 10,
  "sort": [
    { "timestamp": { "order": "desc" } }
  ],
  "search_after": [1577836800000]  # 上一页最后一个文档的时间戳
}'
6. 聚合操作

基本聚合

执行简单的聚合查询,如统计某个字段的不同值数量:

curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "distinct_values": {
      "terms": {
        "field": "field_name.keyword"
      }
    }
  }
}'

管道聚合

连接多个聚合步骤,例如计算平均值后再求总和:

curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "average_prices": {
      "avg": {
        "field": "price"
      }
    },
    "total_sales": {
      "sum_bucket": {
        "buckets_path": "average_prices"
      }
    }
  }
}'

日期直方图聚合

按时间间隔(如每天、每周)汇总数据:

curl -X GET "localhost:9200/index_name/_search?pretty" -H 'Content-Type: application/json' -d'
{
  "size": 0,
  "aggs": {
    "sales_over_time": {
      "date_histogram": {
        "field": "sale_date",
        "calendar_interval": "day"
      }
    }
  }
}'
7. 其他有用的操作

获取特定文档

直接通过 ID 获取单个文档:

curl -X GET "localhost:9200/index_name/_doc/doc_id"

检查文档是否存在

使用 HEAD 方法检查文档是否存在于索引中:

curl -X HEAD "localhost:9200/index_name/_doc/doc_id"

刷新索引

强制刷新索引以便立即可见最新更改:

curl -X POST "localhost:9200/index_name/_refresh"

优化合并段落

减少索引文件的数量,有助于提高查询性能:

curl -X POST "localhost:9200/index_name/_forcemerge?max_num_segments=1"

戳底部名片,一起变现

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图苑

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值