文章目录
索引
创建索引
PUT /my-index
{
"acknowledged" : true,
"shards_acknowledged" : true,
"index" : "my-index"
}
查询
GET /my-index
{
"my-index" : {
"aliases" : { },
"mappings" : { },
"settings" : {
"index" : {
"routing" : {
"allocation" : {
"include" : {
"_tier_preference" : "data_content"
}
}
},
"number_of_shards" : "1",
"provided_name" : "my-index",
"creation_date" : "1640305241457",
"number_of_replicas" : "1",
"uuid" : "PsyhBuKgTAmToCTaKx0uaA",
"version" : {
"created" : "7120199"
}
}
}
}
}
查询所有索引
GET _cat/indices?v
删除
DELETE /my-index
{
"acknowledged" : true
}
修改
修改副本数量为2
PUT /my-index/_settings
{
"index":{
"number_of_replicas":2
}
}
{
"acknowledged" : true
}
索引的分片数量不能修改
PUT /my-index/_settings
{
"index":{
"number_of_shards":3
}
}
{
"error" : {
"root_cause" : [
{
"type" : "illegal_argument_exception",
"reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[my-index/3z9GSoClTUiMPUMwwoiUGw]]"
}
],
"type" : "illegal_argument_exception",
"reason" : "Can't update non dynamic settings [[index.number_of_shards]] for open indices [[my-index/3z9GSoClTUiMPUMwwoiUGw]]"
},
"status" : 400
}
文档
新增文档数据
POST my-index/_doc/
{
"user":"king",
"birthday":"2000-01-01 01:00:00",
"weight":50
}
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "ldZI_H0BLiE92xTEGxY6",
"_version" : 1,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 0,
"_primary_term" : 1
}
post
的时候没有指定id
,所以随机生成了一个ldZI_H0BLiE92xTEGxY6
指定id
POST my-index/_doc/1
{
"user":"ken",
"birthday":"2000-02-01 01:00:00",
"weight":100
}
查询
GET /my-index/_doc/1
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 1,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "ken",
"birthday" : "2000-02-01 01:00:00",
"weight" : 100
}
}
更新文档数据
修改和新增是同一个API,如果根据id
查询到,则会用新的数据覆盖原有的
POST my-index/_doc/1
{
"user":"ken",
"birthday":"2001-02-01 01:00:00",
"weight":150
}
result结果为updated
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 2,
"_primary_term" : 1
}
文档已经被修改
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 2,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "ken",
"birthday" : "2001-02-01 01:00:00",
"weight" : 150
}
}
这里需要特别注意,如果请求体中的字段比原来的少,那么会删除原有的字段
POST my-index/_doc/1
{
"birthday":"2002-02-01 01:00:00",
"weight":120
}
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 5,
"_primary_term" : 1
}
当你再查询时,发现user
字段已经没了
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 5,
"_seq_no" : 5,
"_primary_term" : 1,
"found" : true,
"_source" : {
"birthday" : "2002-02-01 01:00:00",
"weight" : 120
}
}
使用_update更新
所以,如果你只想修改某一个特定的字段,还可以用_update
的方式
POST my-index/_update/1
{
"doc": {
"weight":200
}
}
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 9,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1
}
查询结果
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 9,
"_seq_no" : 9,
"_primary_term" : 1,
"found" : true,
"_source" : {
"birthday" : "2002-02-01 01:00:00",
"weight" : 200
}
}
如果字段不存在,则会自动为其添加
POST my-index/_update/1
{
"doc": {
"user":"tom",
"birthday":"2008-02-01 01:00:00"
}
}
查询结果
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 10,
"_seq_no" : 10,
"_primary_term" : 1,
"found" : true,
"_source" : {
"birthday" : "2008-02-01 01:00:00",
"weight" : 200,
"user" : "tom"
}
}
使用脚本更新
插入一条数据
POST my-index/_doc/1
{
"user":"ken",
"birthday":"2001-02-01 01:00:00",
"weight":150
}
更新
POST my-index/_update/1
{
"script": {
"source": "ctx._source.weight += params.weight",
"lang": "painless",
"params": {
"weight":20
}
}
}
查询结果
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 2,
"_seq_no" : 23,
"_primary_term" : 1,
"found" : true,
"_source" : {
"user" : "ken",
"birthday" : "2001-02-01 01:00:00",
"weight" : 170
}
}
根据条件更新
POST my-index/_doc/2
{
"user":"king",
"birthday":"2001-03-01 01:00:00",
"weight":100
}
执行_update_by_query
POST my-index/_update_by_query
{
"script": {
"source": "ctx._source.weight += 100",
"lang": "painless"
},
"query": {
"term": {
"user": "king"
}
}
}
查询结果
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "2",
"_version" : 2,
"_seq_no" : 24,
"_primary_term" : 1,
"found" : true,
"_source" : {
"birthday" : "2001-03-01 01:00:00",
"weight" : 200,
"user" : "king"
}
}
删除
DELETE my-index/_doc/1
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"_version" : 11,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11,
"_primary_term" : 1
}
GET /my-index/_doc/1
{
"_index" : "my-index",
"_type" : "_doc",
"_id" : "1",
"found" : false
}
根据条件删除
插入两条数据
POST my-index/_doc/1
{
"user":"king",
"birthday":"2001-02-01 01:00:00",
"weight":150
}
POST my-index/_doc/2
{
"user":"king",
"birthday":"2001-03-01 01:00:00",
"weight":100
}
执行_delete_by_query
POST my-index/_delete_by_query
{
"query":{
"match":{
"user":"ken"
}
}
}
{
"took" : 3,
"timed_out" : false,
"total" : 2,
"deleted" : 2,
"batches" : 1,
"version_conflicts" : 0,
"noops" : 0,
"retries" : {
"bulk" : 0,
"search" : 0
},
"throttled_millis" : 0,
"requests_per_second" : -1.0,
"throttled_until_millis" : 0,
"failures" : [ ]
}
Mapping
创建
在创建索引的时候创建Mapping
PUT /my-index
{
"mappings": {
"properties": {
"age":{
"type": "integer"
},
"name":{
"type": "text"
},
"email":{
"type": "keyword"
}
}
}
}
也可以使用_mapping
的方式,为my-index
新增一个属性
PUT my-index/_mapping
{
"properties":{
"phone":{
"type":"keyword"
}
}
}
查询mapping
信息
查询mapping信息
GET my-index/_mapping
查询结果
{
"my-index" : {
"mappings" : {
"properties" : {
"age" : {
"type" : "integer"
},
"email" : {
"type" : "keyword"
},
"name" : {
"type" : "text"
},
"phone" : {
"type" : "keyword"
}
}
}
}
}
只查看指定的属性
GET my-index/_mapping/field/phone
{
"my-index" : {
"mappings" : {
"phone" : {
"full_name" : "phone",
"mapping" : {
"phone" : {
"type" : "keyword"
}
}
}
}
}
}
修改
mapping一旦创建后就不能修改映射关系或是字段类型。