Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit e80869e

Browse files
committedMar 20, 2014
Added multiple level nested document
1 parent 87f024b commit e80869e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed
 

‎src/test/java/org/springframework/data/elasticsearch/repositories/NestedObjectTests.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@
1515
*/
1616
package org.springframework.data.elasticsearch.repositories;
1717

18+
import org.elasticsearch.index.query.BoolQueryBuilder;
1819
import org.elasticsearch.index.query.QueryBuilder;
1920
import org.junit.Before;
2021
import org.junit.Test;
2122
import org.junit.runner.RunWith;
2223
import org.springframework.beans.factory.annotation.Autowired;
24+
import org.springframework.data.domain.Page;
2325
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
2426
import org.springframework.data.elasticsearch.core.query.GetQuery;
2527
import org.springframework.data.elasticsearch.core.query.IndexQuery;
@@ -166,6 +168,32 @@ public void shouldIndexMultipleLevelNestedObject() {
166168
assertThat(personIndexed, is(notNullValue()));
167169
}
168170

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+
169197

170198
private List<IndexQuery> createPerson() {
171199

0 commit comments

Comments
 (0)
Please sign in to comment.