Skip to content

Commit 1954fc2

Browse files
committed
Added multiple level nested document
1 parent e80869e commit 1954fc2

File tree

6 files changed

+107
-13
lines changed

6 files changed

+107
-13
lines changed

src/main/java/org/springframework/data/elasticsearch/entities/Book.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
package org.springframework.data.elasticsearch.entities;
22

3+
import java.util.Collection;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
37
import org.springframework.data.annotation.Id;
48
import org.springframework.data.annotation.Version;
59
import org.springframework.data.elasticsearch.annotations.Document;
10+
import org.springframework.data.elasticsearch.annotations.Field;
11+
import org.springframework.data.elasticsearch.annotations.FieldType;
612

713
@Document(indexName = "book",type = "book" , shards = 1, replicas = 0, indexStoreType = "memory", refreshInterval = "-1")
814
public class Book {
@@ -14,6 +20,17 @@ public class Book {
1420
@Version
1521
private Long version;
1622

23+
public Map<Integer, Collection<String>> getBuckets() {
24+
return buckets;
25+
}
26+
27+
public void setBuckets(Map<Integer, Collection<String>> buckets) {
28+
this.buckets = buckets;
29+
}
30+
31+
@Field(type = FieldType.Nested)
32+
private Map<Integer, Collection<String>> buckets = new HashMap();
33+
1734
public Book(){}
1835

1936
public Book(String id, String name,Long version) {

src/main/java/org/springframework/data/elasticsearch/repositories/SampleBookRepository.java renamed to src/main/java/org/springframework/data/elasticsearch/repositories/book/SampleBookRepository.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package org.springframework.data.elasticsearch.repositories;
1+
package org.springframework.data.elasticsearch.repositories.book;
22

33

44
import org.springframework.data.domain.Page;

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
2929
import org.springframework.data.elasticsearch.core.query.SearchQuery;
3030
import org.springframework.data.elasticsearch.entities.*;
31+
import org.springframework.data.elasticsearch.repositories.book.SampleBookRepository;
3132
import org.springframework.test.context.ContextConfiguration;
3233
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
3334

src/test/java/org/springframework/data/elasticsearch/repositories/SampleBookRepositoryTest.java

Lines changed: 68 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,42 @@
11
package org.springframework.data.elasticsearch.repositories;
22

33
import org.apache.commons.lang.RandomStringUtils;
4-
import org.elasticsearch.index.query.QueryBuilder;
5-
import org.elasticsearch.index.query.QueryBuilders;
64
import org.junit.Before;
75
import org.junit.Ignore;
86
import org.junit.Test;
97
import org.junit.runner.RunWith;
108
import org.springframework.data.domain.Page;
119
import org.springframework.data.domain.PageRequest;
1210
import org.springframework.data.domain.Sort;
13-
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
11+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
1412
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
1513
import org.springframework.data.elasticsearch.core.query.SearchQuery;
1614
import org.springframework.data.elasticsearch.entities.Book;
15+
import org.springframework.data.elasticsearch.repositories.book.SampleBookRepository;
1716
import org.springframework.test.context.ContextConfiguration;
1817
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
1918

2019
import javax.annotation.Resource;
21-
import java.util.ArrayList;
22-
import java.util.Arrays;
23-
import java.util.List;
20+
import java.util.*;
2421

2522
import static java.util.Arrays.asList;
26-
import static org.elasticsearch.index.query.FilterBuilders.boolFilter;
27-
import static org.elasticsearch.index.query.FilterBuilders.existsFilter;
28-
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
23+
import static org.elasticsearch.index.query.FilterBuilders.*;
24+
import static org.elasticsearch.index.query.QueryBuilders.*;
2925
import static org.hamcrest.CoreMatchers.*;
3026
import static org.junit.Assert.assertThat;
3127

3228

3329
@RunWith(SpringJUnit4ClassRunner.class)
34-
@ContextConfiguration("classpath:/springContext-test.xml")
30+
@ContextConfiguration("classpath:/springContext-book-test.xml")
3531
public class SampleBookRepositoryTest {
3632

3733
@Resource
3834
private SampleBookRepository repository;
3935

40-
@Before
36+
@Resource
37+
private ElasticsearchTemplate template;
38+
39+
@Before
4140
public void emptyData(){
4241
repository.deleteAll();
4342
}
@@ -171,7 +170,64 @@ public void shouldReturnBooksWithName(){
171170
assertThat(books.getContent().size(), is(1));
172171
}
173172

174-
// //todo
173+
@Test
174+
public void shouldReturnBooksForGivenBucket(){
175+
Book book1 = new Book(RandomStringUtils.random(5),"test1",System.currentTimeMillis());
176+
Book book2 = new Book(RandomStringUtils.random(5),"test2",System.currentTimeMillis());
177+
178+
Map<Integer, Collection<String>> map1 = new HashMap<Integer, Collection<String>>();
179+
map1.put(1, Arrays.asList("test1", "test2"));
180+
181+
Map<Integer, Collection<String>> map2 = new HashMap<Integer, Collection<String>>();
182+
map2.put(1, Arrays.asList("test3", "test4"));
183+
184+
book1.setBuckets(map1);
185+
book2.setBuckets(map2);
186+
187+
repository.save(Arrays.asList(book1,book2));
188+
189+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
190+
.withQuery(nestedQuery("buckets", termQuery("buckets.1", "test3")))
191+
.build();
192+
193+
Page<Book> books = repository.search(searchQuery);
194+
195+
assertThat(books.getContent().size(), is(1));
196+
}
197+
198+
199+
@Test
200+
public void shouldReturnBooksForGivenBucketUsingTemplate(){
201+
202+
template.deleteIndex(Book.class);
203+
template.createIndex(Book.class);
204+
template.putMapping(Book.class);
205+
template.refresh(Book.class, true);
206+
207+
Book book1 = new Book(RandomStringUtils.random(5),"test1",System.currentTimeMillis());
208+
Book book2 = new Book(RandomStringUtils.random(5),"test2",System.currentTimeMillis());
209+
210+
Map<Integer, Collection<String>> map1 = new HashMap<Integer, Collection<String>>();
211+
map1.put(1, Arrays.asList("test1", "test2"));
212+
213+
Map<Integer, Collection<String>> map2 = new HashMap<Integer, Collection<String>>();
214+
map2.put(1, Arrays.asList("test3", "test4"));
215+
216+
book1.setBuckets(map1);
217+
book2.setBuckets(map2);
218+
219+
repository.save(Arrays.asList(book1,book2));
220+
221+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
222+
.withQuery(nestedQuery("buckets", termQuery("buckets.1", "test3")))
223+
.build();
224+
225+
Page<Book> books = repository.search(searchQuery);
226+
227+
assertThat(books.getContent().size(), is(1));
228+
}
229+
230+
// //todo
175231
@Ignore
176232
@Test
177233
public void shouldReturnBooksForCustomMethodsWithOrCriteria(){
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<beans xmlns="http://www.springframework.org/schema/beans"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xmlns:elasticsearch="http://www.springframework.org/schema/data/elasticsearch"
5+
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
7+
8+
<!--<elasticsearch:node-client id="client" local="true"/>-->
9+
10+
<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300"/>
11+
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
12+
<constructor-arg name="client" ref="client"/>
13+
</bean>
14+
15+
16+
<elasticsearch:repositories base-package="org.springframework.data.elasticsearch.repositories.book" />
17+
18+
19+
</beans>

src/test/resources/springContext-test.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
<elasticsearch:node-client id="client" local="true"/>
99

10+
<!--<elasticsearch:transport-client id="client" cluster-name="elasticsearch" cluster-nodes="127.0.0.1:9300"/>-->
1011
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
1112
<constructor-arg name="client" ref="client"/>
1213
</bean>

0 commit comments

Comments
 (0)