|
17 | 17 |
|
18 | 18 | import static org.assertj.core.api.Assertions.*; |
19 | 19 |
|
| 20 | +import example.springdata.elasticsearch.speaker.Speaker; |
| 21 | +import example.springdata.elasticsearch.talk.Talk; |
| 22 | + |
20 | 23 | import java.text.ParseException; |
21 | 24 | import java.text.SimpleDateFormat; |
| 25 | +import java.util.List; |
22 | 26 |
|
23 | 27 | import org.junit.jupiter.api.Test; |
24 | 28 | import org.springframework.beans.factory.annotation.Autowired; |
@@ -103,4 +107,38 @@ void geoSpatialSearch() { |
103 | 107 |
|
104 | 108 | assertThat(result).hasSize(2); |
105 | 109 | } |
| 110 | + |
| 111 | + @Test |
| 112 | + void criteriaQueryOnNestedSpeakers() { |
| 113 | + |
| 114 | + String speakerName = "Ali"; |
| 115 | + var speaker = Speaker.builder() |
| 116 | + .name(speakerName) |
| 117 | + .position("Developer") |
| 118 | + .build(); |
| 119 | + |
| 120 | + var talkWithSpeaker = Talk.builder() |
| 121 | + .id("1") |
| 122 | + .title("Spring & Elasticsearch") |
| 123 | + .speakers(List.of(speaker)) |
| 124 | + .build(); |
| 125 | + |
| 126 | + var talkWithoutSpeaker = Talk.builder() |
| 127 | + .id("2") |
| 128 | + .title("No Speakers Here") |
| 129 | + .build(); |
| 130 | + |
| 131 | + operations.save(talkWithSpeaker); |
| 132 | + operations.save(talkWithoutSpeaker); |
| 133 | + |
| 134 | + var criteria = new Criteria("speakers.name").is(speakerName); |
| 135 | + var query = new CriteriaQuery(criteria); |
| 136 | + |
| 137 | + var result = operations.search(query, Talk.class); |
| 138 | + |
| 139 | + assertThat(result).hasSize(1); |
| 140 | + assertThat(result.getSearchHits().get(0).getContent() |
| 141 | + .getSpeakers().get(0).getName()) |
| 142 | + .isEqualTo(speakerName); |
| 143 | + } |
106 | 144 | } |
0 commit comments