File tree 3 files changed +41
-1
lines changed
main/java/org/springframework/data/elasticsearch/entities
test/java/org/springframework/data/elasticsearch/repositories
3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 7
7
import org .springframework .data .elasticsearch .annotations .NestedField ;
8
8
9
9
import java .util .ArrayList ;
10
+ import java .util .Collection ;
10
11
import java .util .List ;
11
12
12
13
import static org .springframework .data .elasticsearch .annotations .FieldIndex .analyzed ;
@@ -32,7 +33,10 @@ public class Article {
32
33
private List <String > authors = new ArrayList <String >();
33
34
34
35
@ 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 ;
36
40
37
41
private int score ;
38
42
@@ -83,4 +87,12 @@ public int getScore() {
83
87
public void setScore (int score ) {
84
88
this .score = score ;
85
89
}
90
+
91
+ public Collection <String > getTags () {
92
+ return tags ;
93
+ }
94
+
95
+ public void setTags (Collection <String > tags ) {
96
+ this .tags = tags ;
97
+ }
86
98
}
Original file line number Diff line number Diff line change 2
2
3
3
import org .springframework .data .elasticsearch .core .query .IndexQuery ;
4
4
5
+ import java .util .ArrayList ;
6
+ import java .util .List ;
7
+
5
8
public class ArticleBuilder {
6
9
7
10
private Article resutl ;
@@ -34,6 +37,17 @@ public Article build() {
34
37
return resutl ;
35
38
}
36
39
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
+
37
51
public IndexQuery buildIndex () {
38
52
IndexQuery indexQuery = new IndexQuery ();
39
53
indexQuery .setId (resutl .getId ());
Original file line number Diff line number Diff line change 9
9
10
10
import javax .annotation .Resource ;
11
11
12
+ import java .util .ArrayList ;
13
+ import java .util .List ;
14
+
12
15
import static org .hamcrest .CoreMatchers .is ;
13
16
import static org .hamcrest .CoreMatchers .notNullValue ;
14
17
import static org .junit .Assert .assertThat ;
@@ -31,11 +34,22 @@ public void shouldIndexSingleBookEntity(){
31
34
Article article = new Article ();
32
35
article .setId ("123455" );
33
36
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 );
34
46
//Indexing using sampleArticleRepository
35
47
sampleArticleRepository .save (article );
36
48
//lets try to search same record in elasticsearch
37
49
Article indexedArticle = sampleArticleRepository .findOne (article .getId ());
38
50
assertThat (indexedArticle ,is (notNullValue ()));
39
51
assertThat (indexedArticle .getId (),is (article .getId ()));
52
+ assertThat (indexedArticle .getAuthors ().size (),is (authors .size ()));
53
+ assertThat (indexedArticle .getTags ().size (),is (tags .size ()));
40
54
}
41
55
}
You can’t perform that action at this time.
0 commit comments