对ES执行几个get_or_create
请求时遇到一些问题。 Elasticsearch在回复POST
以索引文档之后似乎需要一段时间,因此在之后的GET
被称为返回没有结果。Elasticsearch在POST后立即登录
这个例子重现问题:
curl -XPOST 'http://localhost:9200/twitter/tweet/' -d '{
"user" : "kimchy",
"post_date" : "2009-11-15T14:12:12",
"message" : "trying out Elastic Search"
}' && \
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}' && \
sleep 1 && \
curl -XGET 'http://localhost:9200/twitter/tweet/_search' -d '{
"query" : {
"term" : { "user" : "kimchy" }
}
}'
的POST
得好:
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 0,
"max_score": null,
"hits": []
}
}
又过了:
{
"ok": true,
"_index": "twitter",
"_type": "tweet",
"_id": "yaLwtgSuQcWg5lzgFpuqHQ",
"_version": 1
}
第一GET
没有任何结果匹配短暂的停顿,显示结果(第二个GET
):
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [{
"_index": "twitter",
"_type": "tweet",
"_id": "yaLwtgSuQcWg5lzgFpuqHQ",
"_score": 0.30685282,
"_source": {
"user": "kimchy",
"post_date": "2009-11-15T14:12:12",
"message": "trying out Elastic Search"
}
}]
}
}
这种行为是否正常?
即使响应较慢,是否可以立即得到结果?
谢谢!
您的索引请求会导致创建一个新的索引?如果我理解正确,你不会使用create index api预先创建它。 – javanna
是的,但该示例基于Elasticsearch的文档。事实是,我的环境中发生了以前创建的索引。 下面两个人做出了很好的答案:使用GET API对于这种情况会更安全。 –
当然,我没有注意到,大写的http GET方法困惑了我:)如答案所述,使用get或者显式调用refresh。干杯! – javanna