|
1 | 1 | package org.springframework.data.elasticsearch.repositories;
|
2 | 2 |
|
3 | 3 | import org.apache.commons.lang.RandomStringUtils;
|
4 |
| -import org.elasticsearch.index.query.QueryBuilder; |
5 |
| -import org.elasticsearch.index.query.QueryBuilders; |
6 | 4 | import org.junit.Before;
|
7 | 5 | import org.junit.Ignore;
|
8 | 6 | import org.junit.Test;
|
9 | 7 | import org.junit.runner.RunWith;
|
10 | 8 | import org.springframework.data.domain.Page;
|
11 | 9 | import org.springframework.data.domain.PageRequest;
|
12 | 10 | import org.springframework.data.domain.Sort;
|
13 |
| -import org.springframework.data.elasticsearch.core.query.NativeSearchQuery; |
| 11 | +import org.springframework.data.elasticsearch.core.ElasticsearchTemplate; |
14 | 12 | import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
|
15 | 13 | import org.springframework.data.elasticsearch.core.query.SearchQuery;
|
16 | 14 | import org.springframework.data.elasticsearch.entities.Book;
|
| 15 | +import org.springframework.data.elasticsearch.repositories.book.SampleBookRepository; |
17 | 16 | import org.springframework.test.context.ContextConfiguration;
|
18 | 17 | import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
19 | 18 |
|
20 | 19 | import javax.annotation.Resource;
|
21 |
| -import java.util.ArrayList; |
22 |
| -import java.util.Arrays; |
23 |
| -import java.util.List; |
| 20 | +import java.util.*; |
24 | 21 |
|
25 | 22 | 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.*; |
29 | 25 | import static org.hamcrest.CoreMatchers.*;
|
30 | 26 | import static org.junit.Assert.assertThat;
|
31 | 27 |
|
32 | 28 |
|
33 | 29 | @RunWith(SpringJUnit4ClassRunner.class)
|
34 |
| -@ContextConfiguration("classpath:/springContext-test.xml") |
| 30 | +@ContextConfiguration("classpath:/springContext-book-test.xml") |
35 | 31 | public class SampleBookRepositoryTest {
|
36 | 32 |
|
37 | 33 | @Resource
|
38 | 34 | private SampleBookRepository repository;
|
39 | 35 |
|
40 |
| - @Before |
| 36 | + @Resource |
| 37 | + private ElasticsearchTemplate template; |
| 38 | + |
| 39 | + @Before |
41 | 40 | public void emptyData(){
|
42 | 41 | repository.deleteAll();
|
43 | 42 | }
|
@@ -171,7 +170,64 @@ public void shouldReturnBooksWithName(){
|
171 | 170 | assertThat(books.getContent().size(), is(1));
|
172 | 171 | }
|
173 | 172 |
|
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 |
175 | 231 | @Ignore
|
176 | 232 | @Test
|
177 | 233 | public void shouldReturnBooksForCustomMethodsWithOrCriteria(){
|
|
0 commit comments