让elasticsearch查询结果里输出_version字段
elasticsearch每条记录都会有一个_version字段,在文档被改变时加一。
插入文档
curl 'http://你的elasticsearch地址/web/blog/1' -d '{"title": "test", "content": "test test test"}'
响应结果为
{
"_index": "web",
"_type": "blog",
"_id": "1",
"_version": 1,
"created": true
}
检索文档
curl 'http://你的elasticsearch地址/web/blog/1'
响应结果为
{
"_index" : "web",
"_type" : "blog",
"_id" : "1",
"_version" : 1,
"found" : true,
"_source":{"title": "test", "content": "test test test"}
}
但是query查询默认是不会带_version字段的。
检索文档
curl 'http://你的elasticsearch地址/web/blog/_search?q=title:test'
响应结果为
{
"took" : 27,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "web",
"_type" : "blog",
"_id" : "1",
"_score" : 0.30685282,
"_source":{"title": "test", "content": "test test test"}
} ]
}
}
可以通过指定version
字段为true
,来实现查询结果里也带上_version字段。
检索文档
curl 'http://你的elasticsearch地址/web/blog/_search?q=title:test&version=truey'
响应结果为
{
"took" : 2,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 0.30685282,
"hits" : [ {
"_index" : "web",
"_type" : "blog",
"_id" : "1",
"_version" : 1,
"_score" : 0.30685282,
"_source":{"title": "test", "content": "test test test"}
} ]
}
}
使用Query DSL
语法也是同样的。
curl 'http://你的elasticsearch地址/web/blog/_search' -d '{"query":{"term":{"title":"test"}}, "version":true}'