Skip to content

Commit ff7cfa3

Browse files
committed
DATAES-23 : Add support for Nested Type (Root level initially)
1 parent 95579d1 commit ff7cfa3

File tree

4 files changed

+209
-5
lines changed

4 files changed

+209
-5
lines changed
Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,25 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
116
package org.springframework.data.elasticsearch.annotations;
217

318
/**
19+
* @author Rizwan Idrees
20+
* @author Mohsin Husen
21+
* @author Artur Konczak
422
*/
523
public enum FieldType {
6-
String, Integer, Long, Date, Float, Double, Boolean, Object, Auto
24+
String, Integer, Long, Date, Float, Double, Boolean, Object, Auto, Nested
725
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch;
17+
18+
/**
19+
* @author Rizwan Idrees
20+
* @author Mohsin Husen
21+
* @author Artur Konczak
22+
*/
23+
public class Car {
24+
25+
private String name;
26+
private String model;
27+
28+
public String getName() {
29+
return name;
30+
}
31+
32+
public void setName(String name) {
33+
this.name = name;
34+
}
35+
36+
public String getModel() {
37+
return model;
38+
}
39+
40+
public void setModel(String model) {
41+
this.model = model;
42+
}
43+
}

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

Lines changed: 81 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,32 +15,44 @@
1515
*/
1616
package org.springframework.data.elasticsearch;
1717

18+
import org.elasticsearch.index.query.QueryBuilder;
1819
import org.junit.Before;
1920
import org.junit.Test;
2021
import org.junit.runner.RunWith;
2122
import org.springframework.beans.factory.annotation.Autowired;
2223
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;
2327
import org.springframework.data.elasticsearch.repositories.SampleElasticSearchBookRepository;
2428
import org.springframework.test.context.ContextConfiguration;
2529
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
2630

2731
import javax.annotation.Resource;
2832

33+
import java.util.ArrayList;
34+
import java.util.Arrays;
35+
import java.util.List;
36+
2937
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;
3041
import static org.hamcrest.Matchers.is;
3142
import static org.hamcrest.Matchers.notNullValue;
3243
import static org.junit.Assert.assertThat;
3344

3445
/**
3546
* @author Rizwan Idrees
3647
* @author Mohsin Husen
48+
* @author Artur Konczak
3749
*/
3850
@RunWith(SpringJUnit4ClassRunner.class)
3951
@ContextConfiguration("classpath:/repository-test-nested-object.xml")
4052
public class NestedObjectTests {
4153

4254
@Resource
43-
private SampleElasticSearchBookRepository repository;
55+
private SampleElasticSearchBookRepository bookRepository;
4456

4557
@Autowired
4658
private ElasticsearchTemplate elasticsearchTemplate;
@@ -51,10 +63,75 @@ public void before() {
5163
elasticsearchTemplate.deleteIndex(Book.class);
5264
elasticsearchTemplate.createIndex(Book.class);
5365
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+
54131
}
55132

56133
@Test
57-
public void shouldIndexNestedObject() {
134+
public void shouldIndexInnerObject() {
58135
// given
59136
String id = randomAlphanumeric(5);
60137
Book book = new Book();
@@ -65,8 +142,8 @@ public void shouldIndexNestedObject() {
65142
author.setName("ABC");
66143
book.setAuthor(author);
67144
// when
68-
repository.save(book);
145+
bookRepository.save(book);
69146
// then
70-
assertThat(repository.findOne(id), is(notNullValue()));
147+
assertThat(bookRepository.findOne(id), is(notNullValue()));
71148
}
72149
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2013 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.data.elasticsearch;
17+
18+
19+
import org.springframework.data.annotation.Id;
20+
import org.springframework.data.elasticsearch.annotations.Document;
21+
import org.springframework.data.elasticsearch.annotations.Field;
22+
import org.springframework.data.elasticsearch.annotations.FieldType;
23+
24+
import java.util.List;
25+
26+
/**
27+
* @author Rizwan Idrees
28+
* @author Mohsin Husen
29+
* @author Artur Konczak
30+
*/
31+
32+
@Document( indexName = "person" , type = "user" , shards = 1, replicas = 0)
33+
public class Person {
34+
35+
@Id
36+
private String id;
37+
38+
private String name;
39+
40+
@Field( type = FieldType.Nested)
41+
private List<Car> car;
42+
43+
public String getId() {
44+
return id;
45+
}
46+
47+
public void setId(String id) {
48+
this.id = id;
49+
}
50+
51+
public String getName() {
52+
return name;
53+
}
54+
55+
public void setName(String name) {
56+
this.name = name;
57+
}
58+
59+
public List<Car> getCar() {
60+
return car;
61+
}
62+
63+
public void setCar(List<Car> car) {
64+
this.car = car;
65+
}
66+
}

0 commit comments

Comments
 (0)