Elasticsearch查询文档
GET查询索引单个文档
GET /索引/_doc/ID
GET /ffbf/_doc/123
返回结果如下,查到了有数据"found" : true表示
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
}
随便查询不存在的文档ID
GET /ffbf/_doc/888888888888888
返回结果,如果没有则为false
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "888888888888888",
"found" : false
}
HEAD检查索引文档是否存在
HEAD /索引/_doc/ID
HEAD /ffbf/_doc/123
返回结果,存在200,OK
200 - OK
如果查询不存在的文档
HEAD /ffbf/_doc/9999999
返回结果,404,Not Found
{"statusCode":404,"error":"Not Found","message":"404 - Not Found"}
查询单个索引里的多个文档
GET /索引/_mget,ids表示要查询的文档id数组
GET /ffbf/_mget
{
"ids":["123","456","789"]
}
返回结果,可以看到id为123、456的文档数据返回了,789这个文档是没有的返回"found" : false
{
"docs" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "456",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "789",
"found" : false
}
]
}
查询单个索引里的多个文档方法2
GET /索引/_search ,使用“query”:{}
GET /ffbf/_search
{
"query": {
"ids": {
"values": ["123","456","789"]
}
}
}
返回结果,命中2条,查询不存在的id789文档直接无视
{
"took" : 1,
"timed_out" : false,
"_shards" : {
"total" : 2,
"successful" : 2,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "456",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
}
]
}
}
查询多个索引多个文档
GET _mgt ,使用"docs":[ ],同时查询ffbf、qqbq两个索引的123、456、789文档
GET _mget
{
"docs":[
{
"_index":"ffbf",
"_id":123
},
{
"_index":"ffbf",
"_id":456
},
{
"_index":"ffbf",
"_id":789
},
{
"_index":"qqbq",
"_id":123
},
{
"_index":"qqbq",
"_id":456
},
{
"_index":"qqbq",
"_id":789
}
]
}
返回结果,ffbf索引的123、456文档返回数据,789文档不存在,qqbq123、456文档不存在,789正常返回数据
{
"docs" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_version" : 2,
"_seq_no" : 1,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "456",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "789",
"found" : false
},
{
"_index" : "qqbq",
"_type" : "_doc",
"_id" : "123",
"found" : false
},
{
"_index" : "qqbq",
"_type" : "_doc",
"_id" : "456",
"found" : false
},
{
"_index" : "qqbq",
"_type" : "_doc",
"_id" : "789",
"_version" : 1,
"_seq_no" : 0,
"_primary_term" : 1,
"found" : true,
"_source" : {
"name" : "Helen",
"age" : 20,
"address" : "天安云谷xx栋xx层",
"url" : "qqbq.top"
}
}
]
}
查询多个索引多个文档方法2
GET ffbf,qqbq/_search
{
"query": {
"ids": {
"values": ["123","456","789"]
}
}
}
返回结果,命中3条,ffbf的123、456,qqbq的789
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 3,
"successful" : 3,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "123",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
},
{
"_index" : "qqbq",
"_type" : "_doc",
"_id" : "789",
"_score" : 1.0,
"_source" : {
"name" : "Helen",
"age" : 20,
"address" : "天安云谷xx栋xx层",
"url" : "qqbq.top"
}
},
{
"_index" : "ffbf",
"_type" : "_doc",
"_id" : "456",
"_score" : 1.0,
"_source" : {
"name" : "Monster",
"age" : 26,
"address" : "天安云谷xx栋xx层",
"url" : "ffbf.top"
}
}
]
}
}
评论区