Skip to content

Commit c8af3a9

Browse files
committed
Added complex multiple level nested type test
1 parent 1954fc2 commit c8af3a9

File tree

6 files changed

+232
-2
lines changed

6 files changed

+232
-2
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
package org.springframework.data.elasticsearch;
2+
3+
import static org.elasticsearch.index.query.QueryBuilders.*;
4+
import static org.hamcrest.core.Is.is;
5+
6+
import java.util.Arrays;
7+
8+
import org.elasticsearch.index.query.BoolQueryBuilder;
9+
import org.elasticsearch.index.query.QueryBuilder;
10+
import org.elasticsearch.index.query.QueryBuilders;
11+
import org.junit.Assert;
12+
import org.junit.Before;
13+
import org.junit.Test;
14+
import org.junit.runner.RunWith;
15+
import org.springframework.beans.factory.annotation.Autowired;
16+
import org.springframework.data.domain.Page;
17+
import org.springframework.data.elasticsearch.core.ElasticsearchTemplate;
18+
import org.springframework.data.elasticsearch.core.query.*;
19+
import org.springframework.data.elasticsearch.entities.Contact;
20+
import org.springframework.data.elasticsearch.entities.Manuscript;
21+
import org.springframework.data.elasticsearch.entities.Role;
22+
import org.springframework.test.context.ContextConfiguration;
23+
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
24+
25+
/**
26+
* Created by huse01 on 31/03/14.
27+
*/
28+
29+
@RunWith(SpringJUnit4ClassRunner.class)
30+
@ContextConfiguration("classpath:/contact-test.xml")
31+
public class ComplexNestedTypeContactTests {
32+
33+
@Autowired
34+
private ElasticsearchTemplate template;
35+
36+
@Before
37+
public void before() {
38+
template.deleteIndex(Contact.class);
39+
template.createIndex(Contact.class);
40+
template.putMapping(Contact.class);
41+
}
42+
43+
@Test
44+
public void test() {
45+
46+
Contact contact1 = new Contact();
47+
contact1.setId("1");
48+
contact1.setName("2");
49+
50+
Manuscript manuscript1 = new Manuscript();
51+
manuscript1.setTitle("t1");
52+
manuscript1.setAbstractText("t2");
53+
manuscript1.setStatus("ACCEPTED");
54+
55+
Role role1 = new Role();
56+
role1.setName("role1");
57+
58+
Role role2 = new Role();
59+
role2.setName("role2");
60+
61+
manuscript1.setRole(Arrays.asList(role1, role2));
62+
63+
Manuscript manuscript2 = new Manuscript();
64+
manuscript2.setTitle("t1");
65+
manuscript2.setAbstractText("t2");
66+
manuscript2.setStatus("DELETED");
67+
68+
Role role3 = new Role();
69+
role3.setName("role3");
70+
71+
manuscript2.setRole(Arrays.asList(role3));
72+
73+
contact1.setManuscripts(Arrays.asList(manuscript1,manuscript2));
74+
75+
IndexQuery indexQuery = new IndexQueryBuilder().withObject(contact1)
76+
.withIndexName("test-contact-test").withId(contact1.getId())
77+
.withType("contact-test-type").build();
78+
79+
template.bulkIndex(Arrays.asList(indexQuery));
80+
template.refresh("test-contact-test", true);
81+
82+
BoolQueryBuilder builder = boolQuery();
83+
builder.must(nestedQuery("manuscripts", termQuery("manuscripts.status", "ACCEPTED")))
84+
.must(nestedQuery("manuscripts.role", termQuery("manuscripts.role.name", "role3")));
85+
86+
SearchQuery searchQuery = new NativeSearchQueryBuilder()
87+
.withQuery(builder).build();
88+
89+
Page<Contact> page = template.queryForPage(searchQuery, Contact.class);
90+
Assert.assertThat(page.getTotalElements(), is(1L));
91+
92+
}
93+
94+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package org.springframework.data.elasticsearch.entities;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
import org.springframework.data.annotation.Id;
7+
import org.springframework.data.elasticsearch.annotations.Document;
8+
import org.springframework.data.elasticsearch.annotations.Field;
9+
import org.springframework.data.elasticsearch.annotations.FieldType;
10+
11+
/**
12+
* Created by huse01 on 31/03/14.
13+
*/
14+
@Document(indexName = "test-contact-test",type = "contact-test-type", shards = 1, replicas = 0)
15+
public class Contact {
16+
17+
@Id
18+
private String id;
19+
private String name;
20+
@Field(type = FieldType.Nested)
21+
private List<Manuscript> manuscripts;
22+
23+
public String getId() {
24+
return id;
25+
}
26+
27+
public void setId(String id) {
28+
this.id = id;
29+
}
30+
31+
public String getName() {
32+
return name;
33+
}
34+
35+
public void setName(String name) {
36+
this.name = name;
37+
}
38+
39+
public List<Manuscript> getManuscripts() {
40+
return manuscripts;
41+
}
42+
43+
public void setManuscripts(List<Manuscript> manuscripts) {
44+
this.manuscripts = manuscripts;
45+
}
46+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package org.springframework.data.elasticsearch.entities;
2+
3+
import java.util.List;
4+
import java.util.Set;
5+
6+
import org.springframework.data.elasticsearch.annotations.Field;
7+
import org.springframework.data.elasticsearch.annotations.FieldIndex;
8+
import org.springframework.data.elasticsearch.annotations.FieldType;
9+
10+
/**
11+
* Created by huse01 on 31/03/14.
12+
*/
13+
public class Manuscript {
14+
15+
private String title;
16+
private String abstractText;
17+
@Field(type = FieldType.String, index = FieldIndex.not_analyzed)
18+
private String status;
19+
@Field(type = FieldType.Nested)
20+
private List<Role> role;
21+
22+
public String getTitle() {
23+
return title;
24+
}
25+
26+
public void setTitle(String title) {
27+
this.title = title;
28+
}
29+
30+
public String getStatus() {
31+
return status;
32+
}
33+
34+
public void setStatus(String status) {
35+
this.status = status;
36+
}
37+
38+
public String getAbstractText() {
39+
return abstractText;
40+
}
41+
42+
public void setAbstractText(String abstractText) {
43+
this.abstractText = abstractText;
44+
}
45+
46+
public List<Role> getRole() {
47+
return role;
48+
}
49+
50+
public void setRole(List<Role> role) {
51+
this.role = role;
52+
}
53+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package org.springframework.data.elasticsearch.entities;
2+
3+
/**
4+
* Created by huse01 on 31/03/14.
5+
*/
6+
public class Role {
7+
8+
private String name;
9+
10+
public String getName() {
11+
return name;
12+
}
13+
14+
public void setName(String name) {
15+
this.name = name;
16+
}
17+
}

src/test/resources/contact-test.xml

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.1.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+
12+
<bean name="elasticsearchTemplate" class="org.springframework.data.elasticsearch.core.ElasticsearchTemplate">
13+
<constructor-arg name="client" ref="client"/>
14+
</bean>
15+
16+
17+
<elasticsearch:repositories base-package="org.springframework.data.elasticsearch.repositories.book" />
18+
19+
</beans>

src/test/resources/springContext-book-test.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@
55
xsi:schemaLocation="http://www.springframework.org/schema/data/elasticsearch http://www.springframework.org/schema/data/elasticsearch/spring-elasticsearch-1.0.xsd
66
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
77

8-
<!--<elasticsearch:node-client id="client" local="true"/>-->
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"/>-->
911

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

0 commit comments

Comments
 (0)