15
15
*/
16
16
package org .springframework .data .elasticsearch ;
17
17
18
+ import org .elasticsearch .index .query .QueryBuilder ;
18
19
import org .junit .Before ;
19
20
import org .junit .Test ;
20
21
import org .junit .runner .RunWith ;
21
22
import org .springframework .beans .factory .annotation .Autowired ;
22
23
import org .springframework .data .elasticsearch .core .ElasticsearchTemplate ;
24
+ import org .springframework .data .elasticsearch .core .query .IndexQuery ;
25
+ import org .springframework .data .elasticsearch .core .query .NativeSearchQueryBuilder ;
26
+ import org .springframework .data .elasticsearch .core .query .SearchQuery ;
23
27
import org .springframework .data .elasticsearch .repositories .SampleElasticSearchBookRepository ;
24
28
import org .springframework .test .context .ContextConfiguration ;
25
29
import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
26
30
27
31
import javax .annotation .Resource ;
28
32
33
+ import java .util .ArrayList ;
34
+ import java .util .Arrays ;
35
+ import java .util .List ;
36
+
29
37
import static org .apache .commons .lang .RandomStringUtils .randomAlphanumeric ;
38
+ import static org .elasticsearch .index .query .QueryBuilders .boolQuery ;
39
+ import static org .elasticsearch .index .query .QueryBuilders .nestedQuery ;
40
+ import static org .elasticsearch .index .query .QueryBuilders .termQuery ;
30
41
import static org .hamcrest .Matchers .is ;
31
42
import static org .hamcrest .Matchers .notNullValue ;
32
43
import static org .junit .Assert .assertThat ;
33
44
34
45
/**
35
46
* @author Rizwan Idrees
36
47
* @author Mohsin Husen
48
+ * @author Artur Konczak
37
49
*/
38
50
@ RunWith (SpringJUnit4ClassRunner .class )
39
51
@ ContextConfiguration ("classpath:/repository-test-nested-object.xml" )
40
52
public class NestedObjectTests {
41
53
42
54
@ Resource
43
- private SampleElasticSearchBookRepository repository ;
55
+ private SampleElasticSearchBookRepository bookRepository ;
44
56
45
57
@ Autowired
46
58
private ElasticsearchTemplate elasticsearchTemplate ;
@@ -51,10 +63,75 @@ public void before() {
51
63
elasticsearchTemplate .deleteIndex (Book .class );
52
64
elasticsearchTemplate .createIndex (Book .class );
53
65
elasticsearchTemplate .refresh (Book .class , true );
66
+ elasticsearchTemplate .deleteIndex (Person .class );
67
+ elasticsearchTemplate .createIndex (Person .class );
68
+ elasticsearchTemplate .putMapping (Person .class );
69
+ elasticsearchTemplate .refresh (Person .class , true );
70
+ }
71
+
72
+ @ Test
73
+ public void shouldIndexNestedObject (){
74
+
75
+ List <Car > cars = new ArrayList <Car >();
76
+
77
+ Car saturn = new Car ();
78
+ saturn .setName ("Saturn" );
79
+ saturn .setModel ("SL" );
80
+
81
+ Car subaru = new Car ();
82
+ subaru .setName ("Subaru" );
83
+ subaru .setModel ("Imprezza" );
84
+
85
+ Car ford = new Car ();
86
+ ford .setName ("Ford" );
87
+ ford .setModel ("Focus" );
88
+
89
+ cars .add (saturn );
90
+ cars .add (subaru );
91
+ cars .add (ford );
92
+
93
+ Person foo = new Person ();
94
+ foo .setName ("Foo" );
95
+ foo .setId ("1" );
96
+ foo .setCar (cars );
97
+
98
+
99
+ Car car = new Car ();
100
+ car .setName ("Saturn" );
101
+ car .setModel ("Imprezza" );
102
+
103
+ Person bar = new Person ();
104
+ bar .setId ("2" );
105
+ bar .setName ("Bar" );
106
+ bar .setCar (Arrays .asList (car ));
107
+
108
+ List <IndexQuery > indexQueries = new ArrayList <IndexQuery >();
109
+ IndexQuery indexQuery1 = new IndexQuery ();
110
+ indexQuery1 .setId (foo .getId ());
111
+ indexQuery1 .setObject (foo );
112
+
113
+ IndexQuery indexQuery2 = new IndexQuery ();
114
+ indexQuery2 .setId (bar .getId ());
115
+ indexQuery2 .setObject (bar );
116
+
117
+ indexQueries .add (indexQuery1 );
118
+ indexQueries .add (indexQuery2 );
119
+
120
+ elasticsearchTemplate .putMapping (Person .class );
121
+ elasticsearchTemplate .bulkIndex (indexQueries );
122
+ elasticsearchTemplate .refresh (Person .class , true );
123
+
124
+ QueryBuilder builder = nestedQuery ("car" , boolQuery ().must (termQuery ("car.name" , "saturn" )).must (termQuery ("car.model" , "imprezza" )));
125
+
126
+ SearchQuery searchQuery = new NativeSearchQueryBuilder ().withQuery (builder ).build ();
127
+ List <Person > persons = elasticsearchTemplate .queryForList (searchQuery , Person .class );
128
+
129
+ assertThat (persons .size () , is (1 ));
130
+
54
131
}
55
132
56
133
@ Test
57
- public void shouldIndexNestedObject () {
134
+ public void shouldIndexInnerObject () {
58
135
// given
59
136
String id = randomAlphanumeric (5 );
60
137
Book book = new Book ();
@@ -65,8 +142,8 @@ public void shouldIndexNestedObject() {
65
142
author .setName ("ABC" );
66
143
book .setAuthor (author );
67
144
// when
68
- repository .save (book );
145
+ bookRepository .save (book );
69
146
// then
70
- assertThat (repository .findOne (id ), is (notNullValue ()));
147
+ assertThat (bookRepository .findOne (id ), is (notNullValue ()));
71
148
}
72
149
}
0 commit comments