Elasticsearch - 集群、索引、文档的基础操作

一、集群

  • 健康状态
GET /_cat/health?v
  • 集群节点
GET /_cat/nodes?v

二、索引

  • 列表索引
GET /_cat/indices?v
  • 存在判断
HEAD /goods
  • 创建索引
PUT /goods
{
  "settings": { 
    "number_of_shards": "2",   // 分片数
    "number_of_replicas": "2"  // 副本数
  } 
}
  • 查看索引
GET /goods
  • 修改设置
PUT /goods/_settings 
{ 
  "number_of_replicas" : "4"   // 分片数不可修改
}
  • 查看设置
GET /goods/_settings 
  • 添加映射
  PUT /goods/_mapping
{
    "properties": {
        "goodsId": {
            "type": "keyword"
        },
        "goodsName": {
            "type": "text",
            "analyzer": "ik_max_word"
        },
        "price": {
            "type": "float"
        },
        "classifyId": {
            "type": "integer"
        },
        "addTime": {
            "type": "date",
            "format": "yyyy-MM-dd HH:mm:ss"
        }
    }
}
  • 创建索引时同时添加映射
  PUT /goods
{
    // 添加设置
    "settings": {
        "number_of_shards": "1",
        "number_of_replicas": "1"
    },
    // 添加映射
    "mappings": {
        "properties": {
            "goodsId": {
                "type": "keyword"
            },
            "goodsName": {
                "type": "text",
                "analyzer": "ik_max_word"
            },
            "price": {
                "type": "float"
            },
            "classifyId": {
                "type": "integer"
            },
            "addTime": {
                "type": "date",
                "format": "yyyy-MM-dd HH:mm:ss"
            }
        }
    }
}
  • 查看映射
GET /goods/_mapping
  • 修改映射

        对于已存在的mapping映射,可以添加新的字段,但不允许修改已存在的字段类型

  • 删除索引
DELETE /goods

三、文档

  • 新增自动ID
POST /goods/_doc
{
  "id":1,
  "goodsName":"华硕普通键盘",
  "goodsPrice":56.56
}

新增指定ID

POST /goods/_doc/2
{
  "id":2,
  "goodsName":"华硕机械键盘",
  "goodsPrice":488.88
}
-----------------------------
PUT /goods/_doc/3
{
  "id":3,
  "goodsName":"华硕有线鼠标",
  "goodsPrice":39.39
}

覆盖更新

PUT /goods/_doc/1
{
  "id":1,
  "goodsName":"华硕普通键盘",
  "goodsPrice":65.65
}

局部更新

POST /goods/_doc/1
{
  "goodsPrice":77.77
}

指定删除

DELETE /goods/_doc/1

列表查询

GET /goods/_search

指定查询

GET /goods/_doc/1

是否存在

HEAD /goods/_doc/1

批量查询

GET /goods/_mget
{
  "ids": [1,2]
}
---------------------------------
GET /bemall_goods/_mget
{
  "docs": [
    {     
      "_id": "1"
    },
    { 
      "_id": "2"
    }
  ]
}
---------------------------------
GET /_mget
{
  "docs": [
    {
      "_index": "product",  # 跨索引
      "_id": "1"
    },
    {
      "_index": "goods",    # 跨索引
      "_id": "1"
    }
  ]
}

附、参数

v:用来在管理类结果中返回表头

GET /_cat/indices?v

pretty:用来在查询类结果中美化响应的JSON输出

GET /goods/_doc/1?pretty

_source:用来在查询中只返回原始的JSON数据

GET /goods/_doc/1/_source

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值