elasticsearch - Elasticsearch Java API:等到在搜索结果中找到文档?

标签 elasticsearch

我已经编写了一个使用高级Elasticsearch API的REST API(javax.ws.rs)。这是ES 7.2。

客户端需要对一条记录进行索引,然后执行包含该记录的搜索,并且在索引操作之后,文档将实际出现在搜索中存在一些延迟。

有什么方法可以阻止索引操作,直到新的索引记录出现在搜索结果中?

失败的话,有没有办法获得一个异步通知,说明该文档现在可以搜索了?

为了给我一个用例一个思路,下面是客户端的代码:

  const cr = await this.client.dNodeCreate(fixedNode).toPromise();
  const fr = await this.client.dNodeGetById(cr._id).toPromise();
  await this.client.dNodeCreate(replyRoot).toPromise();

第一行向ES发出索引请求,并返回状态对象。该对象包括新文档的ID。

第二行按ID提取记录。这始终有效。

第三行失败。它尝试索引的文档取决于第一个文档,REST中间件尝试通过搜索(而不是ID)查找该文档。这等效于REST层强制执行的SQL关系。

我总是可以通过在第三个调用之前引入一个延迟(例如1500ms)来使代码正常工作,但这实际上是一种非鲁棒的解决方案。它可能始终在开发模式下工作(所有服务器都在我的笔记本电脑上,没有其他用户),但是无法预测实际生产中的延迟时间。

更新:已解决。

下面标记的答案似乎可以解决问题。作为引用,Java API中的所需调用如下所示:
        IndexRequest req = new IndexRequest(DNode.INDEX);
        req.setRefreshPolicy(WriteRequest.RefreshPolicy.WAIT_UNTIL);

最佳答案

'refresh'参数是您要寻找的。从Elasticsearch documentation:

Refresh (Index API): ) If true, Elasticsearch refreshes the affected shards to make this operation visible to search, if wait_for then wait for a refresh to make this operation visible to search, if false do nothing with refreshes. Valid values: true, false, wait_for. Default: false



因此,您的索引请求应如下所示:
PUT /<index>/_doc/<_id>?refresh=wait_for

我不相信有一种内置的方式来获取文档现在可以搜索的异步通知。话虽如此,如果您已经可以访问文档ID,那么在代码中使用它而不是进行搜索可能更有意义。

关于elasticsearch - Elasticsearch Java API:等到在搜索结果中找到文档?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57760371/

相关文章:

elasticsearch - 在 ElasticSearch 中,我应该为单独但相关的实体使用多个索引吗?

java - "match or null"在elasticsearch中查询

elasticsearch - 使用德语的简单Elasticsearch PDF文本搜索

elasticsearch - 休眠搜索索引注释是否支持Elastic Search的 'copy_to'属性

elasticsearch - 使用Elastic Search + Kibana检索列中搜索的字符串的值计数

elasticsearch - 如何在elasticsearch中对时间戳进行减法运算?

ruby - 映射ElasticSearch GeoPoint字段

apache-spark - 如何用Spark写入远程Elasticsearch节点?

elasticsearch - 如何查看NEST.SearchRequest.Type属性对我的ElasticSearch查询的影响

当我尝试关闭 ElasticSearch 客户端时出现 java.lang.NoSuchMethodError 异常