Skip to content

Commit c66e5be

Browse files
committed
DATAES-11 - another example how to use @FieldType for Collections
1 parent 692ae6b commit c66e5be

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

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

+13-1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import org.springframework.data.elasticsearch.annotations.NestedField;
88

99
import java.util.ArrayList;
10+
import java.util.Collection;
1011
import java.util.List;
1112

1213
import static org.springframework.data.elasticsearch.annotations.FieldIndex.analyzed;
@@ -32,7 +33,10 @@ public class Article {
3233
private List<String> authors = new ArrayList<String>();
3334

3435
@Field(type = Integer, store = true)
35-
private List<Integer> publishedYears = new ArrayList<Integer>();
36+
private List<Integer> publishedYears;
37+
38+
@Field(type = String, store = true)
39+
private Collection<String> tags;
3640

3741
private int score;
3842

@@ -83,4 +87,12 @@ public int getScore() {
8387
public void setScore(int score) {
8488
this.score = score;
8589
}
90+
91+
public Collection<String> getTags() {
92+
return tags;
93+
}
94+
95+
public void setTags(Collection<String> tags) {
96+
this.tags = tags;
97+
}
8698
}

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

+14
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
import org.springframework.data.elasticsearch.core.query.IndexQuery;
44

5+
import java.util.ArrayList;
6+
import java.util.List;
7+
58
public class ArticleBuilder {
69

710
private Article resutl;
@@ -34,6 +37,17 @@ public Article build() {
3437
return resutl;
3538
}
3639

40+
public ArticleBuilder addTag(String tag) {
41+
List<String> tagsTmp = new ArrayList<String>();
42+
if(resutl.getTags()==null){
43+
resutl.setTags(tagsTmp);
44+
}else {
45+
tagsTmp = (List<String>) resutl.getTags();
46+
}
47+
tagsTmp.add(tag);
48+
return this;
49+
}
50+
3751
public IndexQuery buildIndex() {
3852
IndexQuery indexQuery = new IndexQuery();
3953
indexQuery.setId(resutl.getId());

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

+14
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
import javax.annotation.Resource;
1111

12+
import java.util.ArrayList;
13+
import java.util.List;
14+
1215
import static org.hamcrest.CoreMatchers.is;
1316
import static org.hamcrest.CoreMatchers.notNullValue;
1417
import static org.junit.Assert.assertThat;
@@ -31,11 +34,22 @@ public void shouldIndexSingleBookEntity(){
3134
Article article = new Article();
3235
article.setId("123455");
3336
article.setTitle("Spring Data Elasticsearch");
37+
List<String> authors = new ArrayList<String>();
38+
authors.add("Author1");
39+
authors.add("Author2");
40+
article.setAuthors(authors);
41+
List<String> tags = new ArrayList<String>();
42+
tags.add("tag1");
43+
tags.add("tag2");
44+
tags.add("tag3");
45+
article.setTags(tags);
3446
//Indexing using sampleArticleRepository
3547
sampleArticleRepository.save(article);
3648
//lets try to search same record in elasticsearch
3749
Article indexedArticle = sampleArticleRepository.findOne(article.getId());
3850
assertThat(indexedArticle,is(notNullValue()));
3951
assertThat(indexedArticle.getId(),is(article.getId()));
52+
assertThat(indexedArticle.getAuthors().size(),is(authors.size()));
53+
assertThat(indexedArticle.getTags().size(),is(tags.size()));
4054
}
4155
}

0 commit comments

Comments
 (0)