API

  • 基于7.12.0
method url description
GET _cat/indices
_cat/indices?v
获取所有索引及健康度及存储信息等
GET _cat/health 获取集群健康值
GET {index} 获取索引信息
PUT {index} 创建索引
可选mapping指定字段的数据类型
创建文档时如果索引不存在会自动创建索引
DELETE {index} 删除索引
GET {index}/_doc/{id} 查询指定id的文档
POST {index}/_doc/_search 查询文档 各种复杂的查询等 重中之重
POST {index}/_create/{id} 创建文档指定id 会自动创建索引
PUT {index}/_create/{id} 创建文档指定id 会自动创建索引
POST {index}/_doc 创建文档随机id 会自动创建索引
POST {index}/_doc/{id} 创建文档指定id 更新覆盖指定id 会自动创建索引
PUT {index}/_doc/{id} 创建文档指定id 更新覆盖指定id 会自动创建索引
POST {index}/_update/{id} 更新文档
POST {index}/_doc/{id}/_update 更新文档 过期了
DELETE {index}/_doc/{id} 删除文档

索引

查询 GET {index}

GET test1

retrun:
{
  "test1" : {
    "aliases" : { },
    "mappings" : {
      "properties" : {
        "age" : {
          "type" : "long"
        },
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        }
      }
    },
    "settings" : {
      "index" : {
        "routing" : {
          "allocation" : {
            "include" : {
              "_tier_preference" : "data_content"
            }
          }
        },
        "number_of_shards" : "1",
        "provided_name" : "test1",
        "creation_date" : "1618077825326",
        "number_of_replicas" : "1",
        "uuid" : "ucni4gtPRXecSOZ2HyfwVQ",
        "version" : {
          "created" : "7120099"
        }
      }
    }
  }
}

创建 PUT {index}

  • 直接创建索引

    PUT test1
    
    return: 
    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "test1"
    }
  • 创建索引时指定其他信息

    PUT test2
    {
      "mappings": {
        "properties": {
          "name": {
            "type": "text"
          },
          "age": {
            "type": "long"
          }
        }
      }
    }
    
    return:
    {
      "acknowledged" : true,
      "shards_acknowledged" : true,
      "index" : "test2"
    }
  • 创建文档时自动创建索引 见文档的创建操作

删除 DELETE {index}

DELETE index1

return: 
{
  "acknowledged" : true
}

文档

创建 POST {index}/_create/{id}

  • POST {index}/_create/{id} 创建文档指定id

  • PUT {index}/_create/{id} 创建文档指定id

    PUT test1/_create/put_create_1
    {
      "name": "sapluk"
    }
    
    return:
    {
      "_index" : "test1",
      "_type" : "_doc",
      "_id" : "put_create_1",
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 27,
      "_primary_term" : 1
    }
  • POST {index}/_doc/{id} 创建文档指定id 更新覆盖指定id

  • PUT {index}/_doc/{id} 创建文档指定id 更新覆盖指定id

  • 注意这两个是覆盖不要拿来做更新操作,如果缺少字段会导致数据丢失

    PUT test1/_doc/put_doc
    {
      "name": "sapluk"
    }
    
    {
      "_index" : "test1",
      "_type" : "_doc",
      "_id" : "put_doc",    # id是一个字符串
      "_version" : 1,
      "result" : "created",   # or updated
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 0,
      "_primary_term" : 1
    }
    
  • POST {index}/_doc 创建文档随机id

    POST test1/_doc
    {
      "name": "sapluk"
    }
    
    return:
    {
      "_index" : "test1",
      "_type" : "_doc",
      "_id" : "EuQBvXgBiyhSmrfQOwlE",   // 随机id
      "_version" : 1,
      "result" : "created",
      "_shards" : {
        "total" : 2,
        "successful" : 1,
        "failed" : 0
      },
      "_seq_no" : 29,
      "_primary_term" : 1
    }
    

更新 POST {index}/_update/{id}

  POST test1/_update/1
  {
    "doc": {
      "name": "sapluk"
    }
  }

  return:
  {
    "_index" : "test1",
    "_type" : "_doc",
    "_id" : "1",
    "_version" : 5,         // 如果文档没变的话,不会递增
    "result" : "updated",   // 如果文档没变的话,是"noop"
    "_shards" : {
      "total" : 2,
      "successful" : 1,
      "failed" : 0
    },
    "_seq_no" : 4,
    "_primary_term" : 1
  }

删除 DELETE {index}/_doc/{id}

  DELETE test1/_doc/1

  return:
  {
    "_index" : "test1",
    "_type" : "_doc",
    "_id" : "1",
    "_version" : 6,
    "result" : "deleted",
    "_shards" : {
      "total" : 2,
      "successful" : 1,
      "failed" : 0
    },
    "_seq_no" : 5,
    "_primary_term" : 1
  }
文档更新时间: 2021-04-11 03:04   作者:sapluk