Skip to content

basis

9200 RESTFul 请求接口 9300 集群通讯接口

数据格式 Index Documents Fields Type(7.0+已移除)

restful

  1. 创建索引
  • 相同索引只能创建一次
http
PUT https://127.0.0.1:9200/demo
{
  // 分片设置
  "settings": {
    // 主分片数量
    "number_of_shards": 3,
    // 副本分片数量
    "number_of_replicas": 1,
  },
  // 字段映射设置
  "mappings": {
    "properties": {
      "name": {
        "type": "text"
      },
      "age": {
        "type": "integer"
      }
    }
  }
}
json
{
  // 索引名称
  "index": "demo",
  // 响应结果
  "acknowledged": true,
  // 分片操作结果
  "shards_acknowledged": true
}
  1. 查看命令
http
GET http://127.0.0.1:9200/_cat
text
=^.^=
/_cat/allocation
/_cat/shards
/_cat/shards/{index}
/_cat/master
/_cat/nodes
/_cat/tasks
/_cat/indices
/_cat/indices/{index}
/_cat/segments
/_cat/segments/{index}
/_cat/count
/_cat/count/{index}
/_cat/recovery
/_cat/recovery/{index}
/_cat/health
/_cat/pending_tasks
/_cat/aliases
/_cat/aliases/{alias}
/_cat/thread_pool
/_cat/thread_pool/{thread_pools}
/_cat/plugins
/_cat/fielddata
/_cat/fielddata/{fields}
/_cat/nodeattrs
/_cat/repositories
/_cat/snapshots/{repository}
/_cat/templates
/_cat/ml/anomaly_detectors
/_cat/ml/anomaly_detectors/{job_id}
/_cat/ml/trained_models
/_cat/ml/trained_models/{model_id}
/_cat/ml/datafeeds
/_cat/ml/datafeeds/{datafeed_id}
/_cat/ml/data_frame/analytics
/_cat/ml/data_frame/analytics/{id}
/_cat/transforms
/_cat/transforms/{transform_id}
  • 查看索引信息

    v 表示显示表头

http
GET http://127.0.0.1:9200/_cat/indices?v
text
health status index   uuid                   pri rep docs.count docs.deleted store.size pri.store.size
green  open   demo    gG4YnaiqQ4SgtnMLXkn7gQ   3   1          0            0      1.2kb           624b
green  open   testing Zw5soUHNS0iRDs-giIkXzA   2   1    1960003            1      8.7gb          4.3gb

health 当前服务器健康状态: green(集群完整) yellow(单点正常、集群不完整) red(单点不正常) status 索引打开、关闭状态 index 索引名 uuid 索引统一编号 pri 主分片数量 rep 副本数量 docs.count 可用文档数量 docs.deleted 文档删除状态(逻辑删除) store.size 主分片和副分片整体占空间大小 pri.store.size 主分片占空间大小

  • 查看单个索引完整配置
http
GET http://10.112.120.35:9200/demo
json
{
  "demo": {
    "aliases": {},
    "mappings": {
      "properties": {
        "age": {
          "type": "integer"
        },
        "name": {
          "type": "text"
        }
      }
    },
    "settings": {
      "index": {
        "routing": {
          "allocation": {
            "include": {
              "_tier_preference": "data_content"
            }
          }
        },
        "number_of_shards": "3",
        "provided_name": "demo",
        "creation_date": "1715085770510",
        "number_of_replicas": "1",
        "uuid": "gG4YnaiqQ4SgtnMLXkn7gQ",
        "version": {
          "created": "7100099"
        }
      }
    }
  }
}
  • 删除索引
http
DELETE http://10.112.120.35:9200/demo
json
{
  // 响应结果
  "acknowledged": true
}
  • 文档操作
  1. 创建文档

    必须是 POST 不能是 PUT

http
// 不指定 _id 自动生成
POST http://127.0.0.1:9200/demo/_doc/
{
  "name": "张三",
  "age": 18
}
http
// 也可以 指定 _id 手动生成,此时可以使用PUT方式请求
POST http://127.0.0.1:9200/demo/_doc/${id}
PUT http://127.0.0.1:9200/demo/_doc/${id}
JSON
{
    // 索引
    "_index": "demo",
    // 文档类型
    "_type": "_doc",
    // 文档id
    "_id": "UNMhU48BPbRy_5sQBNdm",
    // 文档版本
    "_version": 1,
    // 操作结果 created 表示创建成功
    "result": "created",
    // 分片
    "_shards": {
      // 分片总数
        "total": ,
        // 分片成功数
        "successful": 2,
        // 分片失败数
        "failed": 0
    },
    "_seq_no": 0,
    "_primary_term": 1
}
  1. 查看文档
http
GET http://10.112.120.35:9200/demo/_doc/1
json
{
  // 索引
  "_index": "demo",
  // 文档类型
  "_type": "_doc",
  // 文档id
  "_id": "1",
  // 文档版本id,每次修改文档时,都会增加1
  "_version": 3,
  "_seq_no": 3,
  "_primary_term": 1,
  // 查询结果 true表示找到 false表示没找到
  "found": true,
  // 文档原始信息
  "_source": {
    "name": "张三",
    "age": 18
  }
}
  1. 修改文档

这里指的是修改整条记录

http
POST http://10.112.120.35:9200/demo/_doc/1
  1. 修改字段

这里指的是修改部分记录

http
POST http://10.112.120.35:9200/demo/_update/1