File tree Expand file tree Collapse file tree 3 files changed +41
-1
lines changed
main/java/org/springframework/data/elasticsearch/entities
test/java/org/springframework/data/elasticsearch/repositories Expand file tree Collapse file tree 3 files changed +41
-1
lines changed Original file line number Diff line number Diff line change 77import org .springframework .data .elasticsearch .annotations .NestedField ;
88
99import java .util .ArrayList ;
10+ import java .util .Collection ;
1011import java .util .List ;
1112
1213import 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}
Original file line number Diff line number Diff line change 22
33import org .springframework .data .elasticsearch .core .query .IndexQuery ;
44
5+ import java .util .ArrayList ;
6+ import java .util .List ;
7+
58public 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 ());
Original file line number Diff line number Diff line change 99
1010import javax .annotation .Resource ;
1111
12+ import java .util .ArrayList ;
13+ import java .util .List ;
14+
1215import static org .hamcrest .CoreMatchers .is ;
1316import static org .hamcrest .CoreMatchers .notNullValue ;
1417import 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}
You can’t perform that action at this time.
0 commit comments