elasticseach简单的增、删、查、改操作
我使用的是kibana内置的开发者工具(Dev Tools)
一、添加数据
输入如下语句,点击运行
PUT person/doc/grade1
{
"name":"code",
"grade":90
}
可以看到输出
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade1",
"_version" : 5,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1
}
二、查询数据
为了方便进行查询演示,我们再添加2条数据
PUT person/doc/grade2
{
"name":"teste",
"grade":80
}
PUT person/doc/grade3
{
"name":"product",
"grade":30
}
1. 根据索引(index)、类型(type)、id直接进行查看
GET person/doc/grade1
可以看到查询结果为:
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade1",
"_version" : 5,
"result" : "created",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 9,
"_primary_term" : 1
}
2. 根据指定字段的值查询,有两种方式
我们以查询分数为80的记录为例进行查询
(1)直接拼接参数
GET person/doc/_search?q=grade:80
我们可以看到查询结果为:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade2",
"_score" : 1.0,
"_source" : {
"name" : "teste",
"grade" : 80
}
}
]
}
}
(2)结构化查询
GET person/doc/_search
{
"query":{
"match": {
"grade": 80
}
}
}
查询结果:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade2",
"_score" : 1.0,
"_source" : {
"name" : "teste",
"grade" : 80
}
}
]
}
}
三、更新数据
修改第一条数据的grade字段的值为100
POST person/doc/grade1/_update
{
"doc":{
"grade":100
}
}
结果显示成功:
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade1",
"_version" : 6,
"result" : "updated",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 10,
"_primary_term" : 1
}
四、删除数据
DELETE person/doc/grade1
结果显示成功:
{
"_index" : "person",
"_type" : "doc",
"_id" : "grade1",
"_version" : 7,
"result" : "deleted",
"_shards" : {
"total" : 2,
"successful" : 1,
"failed" : 0
},
"_seq_no" : 11,
"_primary_term" : 1
}