我希望检查Elasticsearch中特定字段值的文档是否存在。
我已经查阅了网络,但只找到了如何检查字段是否存在的方法。
我的索引/类型是:
/twitter/user
用户名是文档中的一个字段。
我想检查在这个类型中是否存在username="xyz"
。
我希望检查Elasticsearch中特定字段值的文档是否存在。
我已经查阅了网络,但只找到了如何检查字段是否存在的方法。
我的索引/类型是:
/twitter/user
用户名是文档中的一个字段。
我想检查在这个类型中是否存在username="xyz"
。
您可以使用大小为0
的查询。 total
值将给您一个关于文档是否存在的概念。
GET /twitter/user/_search
{"size": 0,
"query": {"match": {
"username": "xyz"
}}}
编辑 --
_count api也可以使用。
GET /twitter/user/_count
{ "query": {"match": {
"username": "xyz"
}}}
根据文档:
如果您只想检查文档是否存在,而不关心内容,则应使用
HEAD
方法而不是GET
方法。HEAD
请求不返回正文,只返回HTTP标头:
curl -i -XHEAD http://localhost:9200/twitter/user/userid
200 OK
状态码...如果不存在,则返回404 Not Found
。
注意: userid
是_id
字段的值。POST twitter/user/_search
{
"query": {
"term": {
"username": "xyz"
}
},
"size" : 0
}
响应:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0,
"hits": []
}
}
只需搜索文档,如果存在则返回结果,否则不返回。
http://127.0.0.1:9200/twitter/user/_search?q=username:xyz
http://127.0.0.1:9200/twitter/user/_search/exists?q=username:xyz
它将返回存在true
或false
{
"exists": false
}