11

get_or_create在对 ES 执行多个请求时,我遇到了一些问题。Elasticsearch 在响应索引文档后似乎需要一些时间POST,以至于刚刚GET调用的 a没有返回任何结果。

此示例重现了该问题:

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进展顺利:

{
    "ok": true,
    "_index": "twitter",
    "_type": "tweet",
    "_id": "yaLwtgSuQcWg5lzgFpuqHQ",
    "_version": 1
}

第一个GET不匹配任何结果:

{
    "took": 1,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "failed": 0
    },
    "hits": {
        "total": 0,
        "max_score": null,
        "hits": []
    }
}

短暂的停顿后,显示结果(第二个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"
            }
        }]
    }
}

这种行为正常吗?

即使响应速度较慢,是否有可能立即获得结果?

谢谢!

4

3 回答 3

9

是的,这是正常的,默认情况下弹性搜索每秒更新一次它的索引。

如果您需要在插入文档时立即更新,请在 URL 中包含 refresh=true

从文档中:

刷新

要在操作发生后立即刷新索引,使文档立即出现在搜索结果中,可以将 refresh 参数设置为 true。将此选项设置为 true 仅应在仔细考虑并验证它不会导致性能不佳(从索引和搜索的角度来看)之后进行。请注意,使用 get API 获取文档是完全实时的。

于 2013-08-06T12:50:44.757 回答
4

如果您需要实时访问刚刚编入索引的对象,则需要使用 get API ( http://www.elasticsearch.org/guide/reference/api/get/ ) 而不是搜索。如此处所述,搜索不是实时的。获取 API 是。因此,如果您自己给对象一个 ID,您可以立即使用 get API 通过 ID 获取该对象。

于 2013-08-06T13:17:46.973 回答
0

还有一个优化,可以在大量导入(如批量)期间关闭搜索索引refresh_interval,并在完成后将其放回原处。然后等待/睡眠几秒钟,它应该可以工作。您也可以使用它来调整刷新间隔(也许您不在乎,只希望它每 15 秒刷新一次)

于 2016-05-03T12:24:24.107 回答