1

I'm using elastic4s client that returns a Future in response to index request and when that future completes I still have to do Thread.sleep(1000) before I can query for that indexed record. Mostly it is exactly 1 second. Is there an elasticsearch setting that I can change so that when the Future completes the record will be available?

I tried to use the java client directly client.prepareIndex....execute().actionGet(); and it ends up exactly the same way, I have to call Thread.sleep(1000)

Is there any settings I can change for the record to be ready after the future completes?

execute(index into(foo, bar) id uuid fields baz).await
Thread.sleep(1000) // This is mandatory for search to find it
execute {search in foo}.await // returns empty without Thread.sleep(1000)
4

2 回答 2

3

听起来您可能必须等待默认索引刷新间隔生效,然后才能查询新索引的数据。刷新间隔默认为 1 秒,可通过以下方式更改

curl -XPUT localhost:9200/test/_settings -d '{
    "index" : {
        "refresh_interval" : "1s"
    } }'

或者,您可以通过在索引操作的查询字符串中包含参数来在索引操作之后刷新分片。这可能比全局更改刷新间隔更有用refresh

curl -XPUT 'http://localhost:9200/{index}/{type}/{id}?refresh=true' -d '{
  "property" : "value"
}'
于 2015-02-22T23:31:09.427 回答
0

Russ 的回答是正确的,但我想补充一点关于 Scala 方面的信息。

当您进行索引操作时,future只要 Elasticsearch 集群处理完该命令,就会完成返回。这与文档可供搜索的时间不同。也就是说,正如 Russ 指出的那样,1 秒后(默认情况下)。

所以你的未来在 k 完成。您的文件可在 k+1sec 获得。

您可以在创建索引时调整刷新间隔,例如在 Elastic4s 中

create index "myindex" refreshInterval "200ms" mappings ...

在下一个版本中,您可以使用 Scala 持续时间,例如

create index "myindex" refreshInterval 200.millis mappings ...

但请注意,通过调整太多,您会删除刷新间隔带来的一些优化。如果您正在执行多个索引等,请查看批量 api。(在 Elastic4s 中,只需将您的调用包含在 中bulk(seq)

于 2015-02-23T05:04:12.533 回答