文章目录
注:7.0之后,type默认都是
_doc
,增删改时需要加type,但查询时不用加
创建文档
elasticsearch创建文档有两种方式,1是POST,2是PUT,post是让elasticsearch自动生成id,而通过put,我们可以自己指定id
自动生成id
POST my_index/_doc
{
"username": "pibigsrar"
"age": 12
}
自定义id
指定id为1(如果id已经存在,则直接报错)
PUT my_index/_doc/1?op_type=create
{
"username": "pibigsrar"
"age": 12
}
更新
删除并更新
它会先删除原先id为1的文档,并把新的文档设置进去
PUT my_index/_doc/1
{
"username": "pibigsrar"
}
只更新
你可以通过这个新增一个字段或修改某个字段的值
POST my_index/_update/1
{
"doc": {
"username": "test"
"sex": 1
}
}
删除
DELETE my_index/_doc/1
读取
GET
根据id获取
GET my_index/_doc/1
bulk
批量执行操作,操作可以是查询,创建,删除,搜索等
POST _bulk
{ "index" : { "_index" : "test", "_id" : "1" } }
{ "field1" : "value1" }
{ "delete" : { "_index" : "test", "_id" : "2" } }
{ "create" : { "_index" : "test2", "_id" : "3" } }
{ "field1" : "value3" }
{ "update" : {"_id" : "1", "_index" : "test"} }
{ "doc" : {"field2" : "value2"} }
mget
批量获取
GET my_index/_mget
{
"docs" : [
{
"_id" : "1"
},
{
"_id" : "2"
}
]
}
不同的index
GET /_mget
{
"docs" : [
{
"_index" : "my_index",
"_id" : "1"
},
{
"_index" : "test",
"_id" : "2"
}
]
}
msearch
批量搜索
POST my_index/_msearch
{}
{"query" : {"match_all" : {}},"size":1}
{"index" : "kibana_sample_data_flights"}
{"query" : {"match_all" : {}},"size":2}
去重
根据contentId 去重
"aggs": {
"distinct_contentId": {
"cardinality": {
"field": "contentId",
}
}
}
分组
根据contentId分组count
"aggs": {
"label_group": {
"terms": {
"field": "contentId"
}
}
}
2. 笔记
2.1 URL Search
![](https://img-blog.csdnimg.cn/04f07ed6cbd64d128ee4e8bfdcc98562.png)
![](https://img-blog.csdnimg.cn/d8020441f4194e9e92dc03f53f7f7bce.png)
![](https://img-blog.csdnimg.cn/2cbbade33be74d8bb803ec2b681d5d01.png)
2.2 Request Body
![](https://img-blog.csdnimg.cn/9647424368da4740a12d80e56d8c8362.png)
![](https://img-blog.csdnimg.cn/9ce67d4dacc6494e8dae390e53a7f1a3.png)
2.3 mapping