ElasticSearch利用Kibana 进行CURD

ElasticSearch利用Kibana 进行CURD

添加索引

PUT /lib
{
	"settings":{
		"index":{
			"number_of_shards":5,
			"number_of_replicas":1
		}
	}
}

number_of_shardsnumber_of_replicas是不能修改的

返回信息

{
  "acknowledged": true,
  "shards_acknowledged": true,
  "index": "lib"
}

使用默认添加

PUT /lib2

使用GET获取lib的配置

GET /lib/_settings

返回信息

{
  "lib": {
    "settings": {
      "index": {
        "creation_date": "1561539156445",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UPA0ox8rRiKTO-W41wncpw",
        "version": {
          "created": "6040299"
        },
        "provided_name": "lib"
      }
    }
  }
}

获取lib2的配置

GET /lib2/_settings

返回信息

{
  "lib2": {
    "settings": {
      "index": {
        "creation_date": "1561539390153",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "zUMBW2vUS12yMVHsAAkvvg",
        "version": {
          "created": "6040299"
        },
        "provided_name": "lib2"
      }
    }
  }
}

默认配置为分片为 5 个,备份为 1 个

获取所有索引的配置
GET _all/_settings
返回信息
{
  "lib2": {
    "settings": {
      "index": {
        "creation_date": "1561539390153",
        "number_of_shards": "5",
        "number_of_replicas": "1",
        "uuid": "zUMBW2vUS12yMVHsAAkvvg",
        "version": {
          "created": "6040299"
        },
        "provided_name": "lib2"
      }
    }
  },
  "lib": {
    "settings": {
      "index": {
        "creation_date": "1561539156445",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UPA0ox8rRiKTO-W41wncpw",
        "version": {
          "created": "6040299"
        },
        "provided_name": "lib"
      }
    }
  }
}
添加数据
PUT /lib/user/1         
{
  "first_name":"gao",
  "last_name":"tiedan",
  "age":22,
  "about":"I like to collect rock albums",
  "interests":["music"]
}

PUT 添加数据,lib 类似于数据库, user 类似于 数据库中的表, 1 类似于每一列的ID

返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

** index : 添加到的index **
** _type: 添加到的type**
** _id: 唯一id**
** _version: 版本号 **
** result: 添加结果**


如果不指定ID,想通过elasticsearch 来生成ID可以使用POSt方式
POST lib/user
{
  "first_name":"liu",
  "last_name":"ergou",
  "age":32,
  "about":"I like to collect make love",
  "interests":["love"]
}
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "n-QSk2sBI3jKMDQna__Q",
  "_version": 1,
  "result": "created",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 0,
  "_primary_term": 1
}

** _id : 自动生成的id**

通过GET来查询文档
GET  lib/user/1
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "first_name": "gao",
    "last_name": "songsong",
    "age": 22,
    "about": "I like to collect rock albums",
    "interests": [
      "music"
    ]
  }
}
查询指定字段
GET lib/user/1?_source=age,about
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 1,
  "found": true,
  "_source": {
    "about": "I like to collect rock albums",
    "age": 22
  }
}
修改文档信息

1. 第一种办法,通过一个新的文档,来吧旧的文档来覆盖掉

PUT /lib/user/1         
{
  "first_name":"gao",
  "last_name":"tiedan",
  "age":55,
  "about":"I like to collect rock albums",
  "interests":["music"]
}
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 2,
  "result": "updated",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 1,
  "_primary_term": 1
}

你会发现版本号变成了2,result 变为了 updated

再次查询 id 为 1 的文档
GET /lib/user/1
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 2,
  "found": true,
  "_source": {
    "first_name": "gao",
    "last_name": "tiedan",
    "age": 55,
    "about": "I like to collect rock albums",
    "interests": [
      "music"
    ]
  }
}

** 年龄变成了55岁,版本号变为了 2**
2. 通过post方式来更新文档

POST lib/user/1/_update
{
  "doc": {
    "age":33
  }
}

** POST index/type/id/_update **
** doc{“age”:33} 指定字段 为 age 修改为 33**

返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 3,
  "result": "updated",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 2,
  "_primary_term": 1
}
删除一个文档
DELETE lib/user/1
返回信息
{
  "_index": "lib",
  "_type": "user",
  "_id": "1",
  "_version": 4,
  "result": "deleted",
  "_shards": {
    "total": 1,
    "successful": 1,
    "failed": 0
  },
  "_seq_no": 3,
  "_primary_term": 1
}
删除某个索引
DELETE lib2
返回信息
{
  "acknowledged": true
}
查询所有的索引
GET _all
返回信息
{
  "lib": {
    "aliases": {},
    "mappings": {
      "user": {
        "properties": {
          "about": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "age": {
            "type": "long"
          },
          "first_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "interests": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          },
          "last_name": {
            "type": "text",
            "fields": {
              "keyword": {
                "type": "keyword",
                "ignore_above": 256
              }
            }
          }
        }
      }
    },
    "settings": {
      "index": {
        "creation_date": "1561539156445",
        "number_of_shards": "3",
        "number_of_replicas": "0",
        "uuid": "UPA0ox8rRiKTO-W41wncpw",
        "version": {
          "created": "6040299"
        },
        "provided_name": "lib"
      }
    }
  }
}
CURD的所有语句

PUT /lib/
{
  "settings":{
    "index":{
      "number_of_shards":3,
      "number_of_replicas":0
    }
  }
}

PUT lib2

GET /lib/_settings

GET /lib2/_settings

GET _all/_settings

PUT /lib/user/1
{
  "first_name":"gao",
  "last_name":"songsong",
  "age":22,
  "about":"I like to collect rock albums",
  "interests":["music"]
  
}

POST lib/user
{
  "first_name":"liu",
  "last_name":"yifei",
  "age":32,
  "about":"I like to collect make love",
  "interests":["love"]
}

GET  lib/user/1

GET lib/user/1?_source=age,about

PUT /lib/user/1         
{
  "first_name":"gao",
  "last_name":"tiedan",
  "age":55,
  "about":"I like to collect rock albums",
  "interests":["music"]
}

GET /lib/user/1


POST lib/user/1/_update
{
  "doc": {
    "age":33
  }
}

DELETE lib/user/1

DELETE lib2

GET _all


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值