Spring Data Elasticsearch Parent/Child Document Repositories / Test execution error

*爱你&永不变心* 提交于 2019-12-08 18:43:42

问题


I created two Spring-Data elasticsearch repositories for two parent/child related documents. You can see the document classes for both of them here.

The repositories are getting instantiated in XML with the help of repositories element like this

<elasticsearch:repositories base-package="com.acme.repositories" />

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.

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

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.


回答1:


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.



来源:https://stackoverflow.com/questions/40345014/spring-data-elasticsearch-parent-child-document-repositories-test-execution-er

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!