elasticsearch的CRUD操作
CRUD操作就是增、删、改、查。
create创建文档
POST /accounts/person/1
{
"name":"John",
"lastname":"Doe",
"job_description":"Linux Developer"
}
在accounts这个index下的person这个type创建了一个id为1的文档。文档内容为JSON定义的内容。
可以在kibana的 DEV Tool中执行:
返回的结果:
在返回的结果中,index指明了索引,type指明了类型,id指明了文档id,version指明了文档的版本,result指明文档创建结果,created表明创建成功
read读取文档
GET accounts/person/1
update更新文档
POST /accounts/person/1/_update
{
"doc": {
"job_description":"english teacher"
}
}
然后重新查看文档:
delete删除文档
DELETE /accounts/person/1
DELETE /accounts
然后重新获取一下已验证成功删除:
可以看到文档没有找到,说明成功删除。
Elasticsearch 查询语法(Query)
Query String方法
这种方法就直接使用字符串拼接的方法创建查询请求
GET /accounts/person/_search?q=John
Query DSL 方法
这种方法是使用JSON的方式
GET /accounts/person/_search
{
"query":{
"match":{
"name":"John"
}
}
}
两种查询方式测试
首先创建几个文档:
POST /accounts/person/1
{
"name":"John",
"lastname":"Doe",
"job_description":"Linux Developer"
}
POST /accounts/person/2
{
"name":"Mike",
"lastname":"wa",
"job_description":"worker"
}
然后进行查询,首先使用Query String方法:
接着使用Query DSL 方法: