Spring Data Elasticsearch 父/子文档存储库/测试执行错误 [英] Spring Data Elasticsearch Parent/Child Document Repositories / Test execution error
问题描述
我为两个父/子相关文档创建了两个 Spring-Data elasticsearch 存储库.您可以看到它们的文档类 此处.
I created two Spring-Data elasticsearch repositories for two parent/child related documents. You can see the document classes for both of them here.
在像这样的 repositories 元素的帮助下,存储库在 XML 中被实例化
The repositories are getting instantiated in XML with the help of repositories element like this
<elasticsearch:repositories base-package="com.acme.repositories" />
您可以在 this github repo 中找到该问题的完整示例.我的源代码基于 spring-data-elasticsearch 存储库.源代码仅包含两个域类(父、子)、相关存储库、xml 配置文件和一个 单元测试类.
You can find a complete example of the issue in this github repo. My source code is based in the tests found in spring-data-elasticsearch repo. The source code consists of just two domain classes(parent,child), the related repositories, the xml configuration files and a unit test class.
克隆并运行mvn test
,抛出java.lang.IllegalArgumentException: can't add a _parent field that指向一个已经存在的类型,这还不是一个父
代码>
Clone and run mvn test
, throws java.lang.IllegalArgumentException: can't add a _parent field that points to an already existing type, that isn't already a parent
似乎这种父/子关系产生了一个弹性搜索异常,这在 Elasticsearch 端,但我不知道在执行 Spring Data 测试时如何防止这种情况发生.
It seems that this parent/child relationship produces an elastic search exception which is clear in the Elasticsearch side, but I do not know how to prevent this from happening when executing the Spring Data test.
推荐答案
如果您仍在寻找答案,我通过在父级的 @Document 中设置 createIndex=false 解决了这个问题.并在 bean 中手动创建
If you are still looking for the answer I solved this by Setting the createIndex=false in the @Document of the parent. And creating this in manually in the beans
elasticSearchTemplate.createIndex(Parent.class);
elasticSearchTemplate.putMapping(parent.class);
elasticSearchTemplate.createIndex(Child1.class);
elasticSearchTemplate.createIndex(Child2.class);
这应该首先创建父项,然后创建关联的子项.在更新映射之前也要清除文档.
This should create the parent first and then the associated children. Also do clear the document before you update the mapping.
这篇关于Spring Data Elasticsearch 父/子文档存储库/测试执行错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!