插入后立即查询的 Elasticsearch 奇怪行为

标签 elasticsearch

我正在为一个使用 Elasticsearch 的应用程序编写一些集成测试,但我遇到了一个奇怪的行为。如果我插入一个文档然后直接查询,每次都会得到不同的结果。 我怀疑,尽管插入本身会返回,但索引本身不会同步发生,因此,查询将试验具有不可预测结果的竞争条件。

如果是这种情况:有没有同步的方法,这样当我运行我的查询时,我知道它们已经准备好并且成功了???

更多详细信息:我正在使用嵌入式 elasticsearch,查询是一个简单的过滤器。唯一奇怪的是我正在为文档模型使用模板文件。

编辑:我什至尝试在插入后通过 ID 获取文档,但查询仍然返回随机结果(除非我让线程 sleep 等待几秒钟)。

最佳答案

来自Elasticsearch docs for the index API :

refresh

To refresh the index immediately after the operation occurs, so that the document appears in search results immediately, the refresh parameter can be set to true. Setting this option to true should ONLY be done after careful thought and verification that it does not lead to poor performance, both from an indexing and a search standpoint. Note, getting a document using the get API is completely realtime.

这就是我的查询返回奇怪结果的原因。因为索引有时还没有完成。此外,可以不作为插入的一部分进行刷新,using the _refresh endpoint :

$ curl -XPOST 'http://localhost:9200/twitter/_refresh'

关于插入后立即查询的 Elasticsearch 奇怪行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19773881/

相关文章:

java - Elasticsearch "StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath."

elasticsearch - Elasticsearch自定义映射定义

elasticsearch - 没有配置的节点可用:[]

elasticsearch - Grafana在Ansible上提供Elasticsearch数据存储

php - ElasticSearch查询以搜索父文档并认为至少有一个 child

java - 如何处理 Elasticsearch QueryBuilder Java 中的连字符?

elasticsearch - json 文档的倒排索引

ruby-on-rails - Rails 中的 Elasticsearch 排序结果

json - Elasticsearch中的SQL查询

elasticsearch - "Message"字段不是由 Serilog ElasticSearch 接收器发送