如何使用java API在已有的elasticsearch索引中添加更多文档?

use*_*386 5 java indexing elasticsearch

我创建了一个索引并使用 java Index API 添加了一个文档。

client.prepareIndex("details", "Key", i).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
Run Code Online (Sandbox Code Playgroud)

这很好用。索引已创建并且文档已正确索引。现在,我需要在同一索引中添加另一个文档“Keys2”。所以,我这样做了,

client.prepareUpdate("details", "Keys2", i).setScriptParams(putJsonDocumentString(Key, Value)).execute().actionGet();
Run Code Online (Sandbox Code Playgroud)

但是,它没有添加到上述索引中。我不想使用 Bulk API(我一直收到 ClusterBlockedException,它从未解决,而且我也没有太多数据)我找不到任何示例程序做同样的事情。

例外的是:

ActionRequestValidationException:验证失败:1:缺少脚本或文档

我该如何解决这个问题?

putJsonDocumentString() 方法返回一个Map<string, Object>应该与 setScriptParams() 一起使用的值,对吗?

Tho*_*asC 2

如果我理解得很好,您想在同一索引中添加另一个文档。

在本例中,这非常简单:您可以使用与第一个文档相同的 API,但具有相关参数:

client.prepareIndex("details", <type_name>, <id>).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
Run Code Online (Sandbox Code Playgroud)

和占位符表示第二个文档的实际价值。根据您的描述,Key2是 id,而不是文档的类型。

如果您对索引类型文档概念感到困惑,请查看文档中的基本描述

APIprepareIndex将文档添加(或更新)到索引。在您的情况下,如果您之前没有创建索引和类型,ElasticSearch 将使用默认参数即时创建它。

prepareUpdateAPI用于更新现有文档,而不是向现有索引添加新文档。来自文档

The update API allows to update a document based on a script provided.
Run Code Online (Sandbox Code Playgroud)

这解释了您收到的错误消息。