文章目录
1、ElasticSearch使用的是RestFul风格的API
2、http://39.105.123.197:5601/
1、需要指定索引,文档的类型,文档的Id
2、使用PUT
风格的提交方式,如下:
1、`group`:索引名称
2、`employee`:文档的名字
3、`1`:对应的id
PUT /group/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
PUT /group/employee/2
{
"first_name" : "Jane",
"last_name" : "Smith",
"age" : 32,
"about" : "I like to collect rock albums",
"interests": [ "music" ]
}
PUT /group/employee/3
{
"first_name" : "Douglas",
"last_name" : "Fir",
"age" : 35,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
1、使用GET
风格的方式检索,GET/index/document/id
如下:
GET /group/employee/1
GET /group/employee/2
1、使用DELETE风格的方式删除,DELETE /index/document/id
,如下:
DELETE /megacorp/employee/2
1、使用PUT覆盖当前的文档,使用这种方式会更新整个文档。
2、这种方式和部分更新的最大区别就是增大了网络的开销
PUT /group/employee/3
{
"first_name" : "TOM",
"last_name" : "TOM",
"age" : 60,
"about": "I like to build cabinets",
"interests": [ "forestry" ]
}
0、https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/partial-updates.html
1、文档是不可变的,因此部分的更新也是在内部重复着检索-修改-重建索引 的处理过程 。但是我们对于直接覆盖的方式,可以减少网络传输的时间
2、对指定的文档增加字段,使用doc
,如下
POST /megacorp/employee/2/_update
{
"doc" : {
"tags" : [ "testing" ],
"views": 0
}
}
3、修改已存在的变量的值,使用ctx._source
指定文档中的变量,这里的是将views加1
POST /megacorp/employee/2/_update
{
"script" : "ctx._source.views+=1"
}
4、更新可能不存在的值,如果这个值不存在,那么就添加进去,这个很像MongoDB中的检索,使用upsert
完成,如下:
POST /megacorp/employee/2/_update
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 1
}
}
5、更新和冲突
1、ElasticSearch在文档更新的时候,每个进程会自动保存版本号_version
的值,更改成功之后,如果版本号改变了,那么表示这个文档已经被人更新过了,此时就会更新请求失败。但是我们可以使用指定的参数指定在更新失败之前尝试的更新的次数,retry_on_conflict
,如下:
POST /megacorp/employee/2/_update?retry_on_conflict=5
{
"script" : "ctx._source.views+=1",
"upsert": {
"views": 0
}
}
1、https://www.elastic.co/guide/cn/elasticsearch/guide/current/getting-started.html