use of org.springframework.data.elasticsearch.core.join.JoinField in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldUpdateEntityWithJoinFields.
private void shouldUpdateEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
org.springframework.data.elasticsearch.core.document.Document document = org.springframework.data.elasticsearch.core.document.Document.create();
document.put("myJoinField", toDocument(new JoinField<>("answer", qId2)));
UpdateQuery updateQuery = //
UpdateQuery.builder(aId2).withDocument(//
document).withRouting(qId2).build();
List<UpdateQuery> queries = new ArrayList<>();
queries.add(updateQuery);
// when
operations.bulkUpdate(queries, IndexCoordinates.of(indexNameProvider.indexName()));
SearchHits<SampleJoinEntity> updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId2)).build(), SampleJoinEntity.class);
List<String> hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {
@Override
public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
return sampleJoinEntitySearchHit.getId();
}
}).collect(Collectors.toList());
assertThat(hitIds.size()).isEqualTo(1);
assertThat(hitIds.get(0)).isEqualTo(aId2);
updatedHits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
hitIds = updatedHits.getSearchHits().stream().map(new Function<SearchHit<SampleJoinEntity>, String>() {
@Override
public String apply(SearchHit<SampleJoinEntity> sampleJoinEntitySearchHit) {
return sampleJoinEntitySearchHit.getId();
}
}).collect(Collectors.toList());
assertThat(hitIds.size()).isEqualTo(1);
assertThat(hitIds.get(0)).isEqualTo(aId1);
}
use of org.springframework.data.elasticsearch.core.join.JoinField in project spring-data-elasticsearch by spring-projects.
the class ElasticsearchTemplateTests method shouldSaveEntityWithJoinFields.
// #1218
private void shouldSaveEntityWithJoinFields(String qId1, String qId2, String aId1, String aId2) throws Exception {
SampleJoinEntity sampleQuestionEntity1 = new SampleJoinEntity();
sampleQuestionEntity1.setUuid(qId1);
sampleQuestionEntity1.setText("This is a question");
JoinField<String> myQJoinField1 = new JoinField<>("question");
sampleQuestionEntity1.setMyJoinField(myQJoinField1);
SampleJoinEntity sampleQuestionEntity2 = new SampleJoinEntity();
sampleQuestionEntity2.setUuid(qId2);
sampleQuestionEntity2.setText("This is another question");
JoinField<String> myQJoinField2 = new JoinField<>("question");
sampleQuestionEntity2.setMyJoinField(myQJoinField2);
SampleJoinEntity sampleAnswerEntity1 = new SampleJoinEntity();
sampleAnswerEntity1.setUuid(aId1);
sampleAnswerEntity1.setText("This is an answer");
JoinField<String> myAJoinField1 = new JoinField<>("answer");
myAJoinField1.setParent(qId1);
sampleAnswerEntity1.setMyJoinField(myAJoinField1);
SampleJoinEntity sampleAnswerEntity2 = new SampleJoinEntity();
sampleAnswerEntity2.setUuid(aId2);
sampleAnswerEntity2.setText("This is another answer");
JoinField<String> myAJoinField2 = new JoinField<>("answer");
myAJoinField2.setParent(qId1);
sampleAnswerEntity2.setMyJoinField(myAJoinField2);
IndexCoordinates index = IndexCoordinates.of(indexNameProvider.indexName());
operations.save(Arrays.asList(sampleQuestionEntity1, sampleQuestionEntity2, sampleAnswerEntity1, sampleAnswerEntity2), index);
SearchHits<SampleJoinEntity> hits = operations.search(new NativeSearchQueryBuilder().withQuery(new ParentIdQueryBuilder("answer", qId1)).build(), SampleJoinEntity.class);
List<String> hitIds = hits.getSearchHits().stream().map(sampleJoinEntitySearchHit -> sampleJoinEntitySearchHit.getId()).collect(Collectors.toList());
assertThat(hitIds.size()).isEqualTo(2);
assertThat(hitIds.containsAll(Arrays.asList(aId1, aId2))).isTrue();
hits.forEach(searchHit -> {
assertThat(searchHit.getRouting()).isEqualTo(qId1);
});
}
Aggregations