|
15 | 15 | */
|
16 | 16 | package org.springframework.data.elasticsearch.repositories;
|
17 | 17 |
|
| 18 | +import org.elasticsearch.index.query.BoolQueryBuilder; |
18 | 19 | import org.elasticsearch.index.query.QueryBuilder;
|
19 | 20 | import org.junit.Before;
|
20 | 21 | import org.junit.Test;
|
21 | 22 | import org.junit.runner.RunWith;
|
22 | 23 | import org.springframework.beans.factory.annotation.Autowired;
|
| 24 | +import org.springframework.data.domain.Page; |
23 | 25 | import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
|
24 | 26 | import org.springframework.data.elasticsearch.core.query.GetQuery;
|
25 | 27 | import org.springframework.data.elasticsearch.core.query.IndexQuery;
|
@@ -166,6 +168,32 @@ public void shouldIndexMultipleLevelNestedObject() {
|
166 | 168 | assertThat(personIndexed, is(notNullValue()));
|
167 | 169 | }
|
168 | 170 |
|
| 171 | + @Test |
| 172 | + public void shouldSearchUsingNestedQueryOnMultipleLevelNestedObject() { |
| 173 | + //given |
| 174 | + List<IndexQuery> indexQueries = createPerson(); |
| 175 | + |
| 176 | + //when |
| 177 | + elasticsearchTemplate.putMapping(PersonMultipleLevelNested.class); |
| 178 | + elasticsearchTemplate.bulkIndex(indexQueries); |
| 179 | + elasticsearchTemplate.refresh(PersonMultipleLevelNested.class, true); |
| 180 | + |
| 181 | + //then |
| 182 | + BoolQueryBuilder builder = boolQuery(); |
| 183 | + builder.must(nestedQuery("girlFriends", termQuery("girlFriends.type", "temp"))) |
| 184 | + .must(nestedQuery("girlFriends.cars", termQuery("girlFriends.cars.name", "Ford".toLowerCase()))); |
| 185 | + |
| 186 | + SearchQuery searchQuery = new NativeSearchQueryBuilder() |
| 187 | + .withQuery(builder) |
| 188 | + .build(); |
| 189 | + |
| 190 | + Page<PersonMultipleLevelNested> personIndexed = elasticsearchTemplate.queryForPage(searchQuery, PersonMultipleLevelNested.class); |
| 191 | + assertThat(personIndexed, is(notNullValue())); |
| 192 | + assertThat(personIndexed.getTotalElements(), is(1L)); |
| 193 | + assertThat(personIndexed.getContent().get(0).getId(), is("1")); |
| 194 | + } |
| 195 | + |
| 196 | + |
169 | 197 |
|
170 | 198 | private List<IndexQuery> createPerson() {
|
171 | 199 |
|
|
0 commit comments