in current project I'm using Elasticsearch Spring Data (3.0.9) with ES 5.5.0 and I'm researching for usage of parent-child relation (more specific I'm testing what approach is better [in meaning of performance] to use - nested objects or parent-child relation]. Here what I've got
parent Entity:
@Document(indexName = "testIndex", type="parentItem", createIndex = false)
public class ParentItem {
@Id
private String id;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// getters and setters
}
Child entity:
@Document(indexName = "testIndex", type="childItem", createIndex = false)
public class ChildItem {
@Id
private String id;
@Field(type = FieldType.Keyword, store = true)
@Parent(type = "parentItem")
private String parentId;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// getters and setters
}
then ofc I have simple repository classes (with no body just extending ElasticsearchRepository
) and I'm simply creating parent-child pair like
ParentItem parent = createParent(); // creating parent instance
ChildItem child = createChild(); // creating child instance
child.setParentId(parent.getId());
and after saving/indexing this using repository it is being stored to the ES node - briliant :)
Now the question is: is there any possibility in Spring Data to keep child object inside of parent entity and eagerly load this during fetching parent like
@Document(indexName = "testIndex", type="parentItem", createIndex = false)
public class ParentItem {
@Id
private String id;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// can I do anything like this?
// @WhatAnnotation?
private ChildItem child;
// getters and setters
}
or the other way - to "join" eagerly parent instance to the child?
@Document(indexName = "testIndex", type="childItem", createIndex = false)
public class ChildItem {
@Id
private String id;
@Field(type = FieldType.Keyword, store = true)
@Parent(type = "parentItem")
private String parentId;
@Field(type = FieldType.Text)
@JsonProperty("value")
private String value;
// can I do anything like this?
// @WhatAnnotation?
private ParentItem parent;
// getters and setters
}
Is there anything like this in Spring Data? Or do I need to implement my own mechanism?
The other issue is that during saving these documents I simply need to make two "calls" to ES with repositories. So getting back to creating example - further I need to do
ParentItem parent = createParent(); // creating parent instance
ChildItem child = createChild(); // creating child instance
child.setParentId(parent.getId());
parentRepository.index(parent);
childRepository.index(child);
which means that it cannot be done in one transaction. This seems to be not very good for performance?
Does ES Spring Data support anything like this or not? Official documentation is not verey helpful to be honest - also available examples are not showing anything more that I just implemented :(