创建文档:
- :创建文档时候如果索引不存在,es会自动创建对应的索引和type
PUT/index/type/id
_version:每次更改加一 - :如果只想创建文档不使用PUT命令来update文档,则可以使用:
PUT/index/type/id/_create
或者:
PUT/index/type/id?op_type=create
- :自动生成id
POST/index/type/
查询文档:
- 根据id查询文档内容
GET/index/type/id
_source:存储的json文档内容 - 将查询结果中的source字段隐藏
GET/index/type/id?_source=false
- 只获取_source中的内容
GET/index/type/id/_source
- 选取查询结果中source中某几个字段,并且隐藏一些字段
GET/index/type/id?souce_includes=field&source_excludes=password
- 如果你只想选取source中的某几个字段,多个字段用
,
隔开
GET/index/type/id?_source=field,field
- Store Field:如果你想查询的时候不想存储某个字段,则可以使用一下语法
新建mapping,将field中store属性设置为false,则在查询的时候带上store属性可以达到隐藏的效果
PUT twitter
{
"mappings": {
"_doc": {
"properties": {
"counter": {
"type": "integer",
"store": false
},
"tags": {
"type": "keyword",
"store": true
}
}
}
}
}
查询:
GET/index/type/id?store_field=counter,tags
删除
-
通过restAPI对文档进行删除
DELETE /index/type/id -
通过_delete_by_query来对match的内容进行删除
POST twitter/_delete_by_query
{
"query": {
"match": {
"job": "coder"
}
}
}
更新
-
PUT /index/type/id
-
使用脚本进行跟新:
有以下文档:
PUT test/_doc/1
{
"counter":1,
"tags":["red"]
}
POST test/_doc/1/_update
{
"script":{
"source":"ctx._source.counter+=params.count",
"lang":"painless",
"params":{
"count":4
}
},
"upsert": {
"counter":1
}
}