Elasticsearch 基本操作

本文档介绍了Elasticsearch的基本操作,包括查看集群健康度、创建和删除索引、映射管理以及文档的增删改查。详细讲解了如何创建索引、设置映射、更新字段映射以及执行查询操作等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Elasticsearch 基本操作

集群

查看集群健康度
GET /_cat/health?v

curl -XGET "http://localhost:9200/_cat/health?v"

响应:

epoch      timestamp cluster       status node.total node.data shards pri relo init unassign pending_tasks max_task_wait_time active_shards_percent
1475247709 17:01:49  elasticsearch green           1         1      0   0    0    0        0             0                  -                100.0%
查看集群健康度会获取三种状态
  • 绿色——一切都好(群集完全正常工作)
  • 黄色——所有数据都可用,但一些副本尚未分配(群集完全正常工作)
  • 红色——有些数据不管出于什么原因都不可用(集群部分功能正常)
列出集群所有节点
GET /_cat/health?v

curl -X GET "localhost:9200/_cat/health?v"
列出所有的索引
GET /_cat/indices?v

curl -X GET "localhost:9200/_cat/indices?v"

索引的使用

创建索引
  • 创建默认配置的索引 customer,5 个基础分片,1 个复制集(必须需要 2 个节点才可用)。
PUT /customer?pretty

curl -X PUT "localhost:9200/customer?pretty"
  • 指定设置和映射
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
    "settings" : {
        "number_of_shards" : 1,
        "number_of_replicas" : 2,
        "refresh_interval": "1s" //默认值
    },
    "mappings" : {
        "_doc" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}
'
删除
  • 删除索引可以使用模糊匹配等多种选择
DELETE /my_index?pretty
DELETE /index_one,index_two
DELETE /index_*
DELETE /_all
DELETE /*

curl -X DELETE "localhost:9200/customer?pretty"
# 结果
{
  "acknowledged" : true
}

映射

单索引映射
  1. 创建索引时创建映射
curl -X PUT "localhost:9200/test" -H 'Content-Type: application/json' -d'
{
    "mappings" : {
        "_doc" : {
            "properties" : {
                "field1" : { "type" : "text" }
            }
        }
    }
}
'
  1. 在存在的索引中添加映射
curl -X PUT "localhost:9200/twitter" -H 'Content-Type: application/json' -d'
{}
'
curl -X PUT "localhost:9200/twitter/_mapping/_doc" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "email": {
      "type": "keyword"
    }
  }
}
'

说明:

  • 创建没有任何映射的 twitter 索引。
  • 使用 映射 API 添加新的字段 email 到 _doc。
  1. 其他说明
多索引

该 API 可以一次请求作用在多个索引上。如,同时更新两个映射:

# Create the two indices
PUT twitter-1
PUT twitter-2

# Update both mappings
PUT /twitter-1,twitter-2/_mapping/_doc 
{
  "properties": {
    "user_name": {
      "type": "text"
    }
  }
}

说明:多个索引遵循多索引名字写法与通配符格式。

更新字段映射

通常,现有字段的映射无法更新。这条规则有一些例外。例如:

  • 可以向对象数据类型字段添加新属性。
  • 可以将新的 multi-fields(多字段) 添加到现有字段中。
  • ignore_above 参数可以更新。
    如:
curl -X PUT "localhost:9200/my_index" -H 'Content-Type: application/json' -d'
{
  "mappings": {
    "_doc": {
      "properties": {
        "name": {
          "properties": {
            "first": {
              "type": "text"
            }
          }
        },
        "user_id": {
          "type": "keyword"
        }
      }
    }
  }
}
'
curl -X PUT "localhost:9200/my_index/_mapping/_doc" -H 'Content-Type: application/json' -d'
{
  "properties": {
    "name": {
      "properties": {
        "last": { 
          "type": "text"
        }
      }
    },
    "user_id": {
      "type": "keyword",
      "ignore_above": 100 
    }
  }
}
'

说明:

  1. 创建一个对象数据类型且下面有一个字段为 first,以及一个 user_id 字段。
  2. 添加一个 last 字段在 name 对象字段下。
  3. 更新 ingore_above 设置(默认值为 0)。

无论字段的映射参数指定与否,它都可以在存在的字段上更新。

映射获取

curl -X GET "localhost:9200/twitter/_mapping/_doc"

文档的增删改查)

创建一个文档
  • 索引为 customer ,类型称为 _doc ,并且选择 123 作为 ID:
curl -X PUT "localhost:9200/customer/_doc/1?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
  • 自动生成 ID 的方式:
curl -X POST "localhost:9200/customer/_doc/?pretty" -H 'Content-Type: application/json' -d'
{
  "name": "John Doe"
}
'
说明:

自动生成的 ID 是 URL-safe、 基于 Base64 编码且长度为20个字符的 GUID 字符串。 这些 GUID 字符串由可修改的 FlakeID 模式生成,这种模式允许多个节点并行生成唯一 ID ,且互相之间的冲突概率几乎为零。

获取一个文档
  • 文档存在
curl -i XGET "localhost:9200/customer/_doc/1?pretty"

// 返回
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 31

{
  "_index" : "customer",
  "_type" : "_doc",
  "_id" : "1",
  "_version" : 1,
  "found" : true,
  "_source" : { "name": "John Doe" }
}
  • 文档不存在
curl -i XGET "localhost:9200/customer/_doc/2?pretty"

# 响应
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 83

{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "124",
  "found" : false
}
  • 返回文档一部分
curl -X GEcurl -X GET "localhost:9200/website/blog/123?_source=title,text&pretty"

# 响应
{
  "_index" : "website",
  "_type" : "blog",
  "_id" : "123",
  "_version" : 1,
  "found" : true,
  "_source" : {
    "text" : "Just trying this out...",
    "title" : "My first blog entry"
  }
}
  • 只得到 _source 字段,不需要任何元数据
curl -X GET "localhost:9200/website/blog/123/_source"

# 响应
{
  "title": "My first blog entry",
  "text":  "Just trying this out...",
  "date":  "2014/01/01"
}
检查文档是否存在
curl --head http://localhost:9200/website/blog/123
# 或者
curl -I -XHEAD http://localhost:9200/website/blog/123

# 存在
HTTP/1.1 200 OK
content-type: application/json; charset=UTF-8
content-length: 185

# 不存在
HTTP/1.1 404 Not Found
content-type: application/json; charset=UTF-8
content-length: 61
更新文档
  • 替换整个文档
curl -X PUT "localhost:9200/website/blog/123" -H 'Content-Type: application/json' -d'
{
  "title": "My first blog entry",
  "text":  "I am starting to get the hang of this...",
  "date":  "2014/01/02"
}
'
# 响应
{
  "_index" :   "website",
  "_type" :    "blog",
  "_id" :      "123",
  "_version" : 2,
  "created":   false 
}

元数据 _version 会在每一次 put 都会增一,即使数据相同。

  • 单文档部分更新
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "doc": { "name": "Jane Doe" }
}
'
# 或者使用 script
curl -X POST "localhost:9200/customer/_doc/1/_update?pretty" -H 'Content-Type: application/json' -d'
{
  "script" : "ctx._source.age += 5"
}
'
  • 按条件更新
    Update By Query API
删除文档
  • 删除单片文档
curl -X DELETE "localhost:9200/customer/_doc/2?pretty"
  • 按条件删除
    Delete By Query API
批量 API
# index
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"index":{"_id":"1"}}
{"name": "John Doe" }
{"index":{"_id":"2"}}
{"name": "Jane Doe" }
'
# update,delete
curl -X POST "localhost:9200/customer/_doc/_bulk?pretty" -H 'Content-Type: application/json' -d'
{"update":{"_id":"1"}}
{"doc": { "name": "John Doe becomes Jane Doe" } }
{"delete":{"_id":"2"}}
'
查询
  • 通过 uri 传参请求:
curl -X GET "localhost:9200/bank/_search?q=*&sort=account_number:asc&pretty"
  • 通过 json 方式传参:
curl -X GET "localhost:9200/bank/_search" -H 'Content-Type: application/json' -d'
{
  "query": { "match_all": {} },
  "sort": [
    { "account_number": "asc" }
  ]
}
'
响应参数解析
  • took – 执行耗费的时间(毫秒)
  • timed_out – 是否超时
  • _shards – 告诉我们搜索了多少分片,以及搜索成功/失败的分片的计数
  • hits – 搜索结果
  • hits.total – 检索规则匹配到的文档总数量
  • hits.hits – 检索结果的数组 (默认10条记录)
  • hits.sort - 检索结果用到的排序值 (使用分数排序时缺失)
  • hits._score and max_score - 检索的分数
参考
  1. 设置参数: https://www.elastic.co/guide/en/elasticsearch/reference/6.4/index-modules.html
  2. 创建索引:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/indices-create-index.html
  3. 添加映射:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/indices-put-mapping.html
  4. 获取映射:https://www.elastic.co/guide/en/elasticsearch/reference/6.4/indices-get-mapping.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值