[英]How to Create more than one type(table) in single Index(db) using Java API in Elasticsearch
[英]How to add more documents in already existing elasticsearch index using java API?
我创建了一个索引并使用java Index API添加了一个文档。
client.prepareIndex("details", "Key", i).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
这很好。 已创建索引并正确索引文档。 现在,我需要在同一索引中添加另一个文档“Keys2”。 所以,我这样做了,
client.prepareUpdate("details", "Keys2", i).setScriptParams(putJsonDocumentString(Key, Value)).execute().actionGet();
但是,它没有被添加到上述索引中。 我不想使用Bulk API(我一直得到ClusterBlockedException,它从未解决,加上我也没有太多数据)我找不到任何样本程序做同样的事情。
例外是:
ActionRequestValidationException :验证失败:1:缺少脚本或doc
我该如何解决这个问题?
方法putJsonDocumentString()返回一个Map<string, Object>
,它应该与setScriptParams()一起使用,对吗?
如果我理解得很好,您想在同一索引中添加另一个文档。
在这种情况下,这很简单:您可以使用与第一个文档相同的API,但使用相关参数:
client.prepareIndex("details", <type_name>, <id>).setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
与第二份文件的实际价值和占位符。 根据您的描述, Key2
是id,而不是文档的类型。
如果您对索引 , 类型和文档概念感到困惑,请查看文档中的基本描述 。
prepareIndex
API将文档添加(或更新)到索引。 在您的情况下,如果您之前没有创建索引和类型,ElasticSearch将使用默认参数即时创建它。
prepareUpdate
API用于更新现有文档 ,而不是将新文档添加到现有索引。 来自文档 :
The update API allows to update a document based on a script provided.
这解释了你的错误信息。
正如@ThomasC所说,您应该以与第一个文档相同的方式索引后续文档。 我认为您遇到的问题是您尝试使用相同的ID和不同类型索引2个文档。 你可以通过索引第一个来解决这个问题:
client.prepareIndex("details", "Key", "ID1").setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
第二个是这样的:
client.prepareIndex("details", "Key2", "ID2").setSource(putJsonDocumentString(Key, Value)).execute().actionGet();
理想情况下,ID将更容易确保是唯一的,例如UUID。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.