23
23
24
24
import java .util .*;
25
25
26
+ import org .apache .commons .lang .StringUtils ;
26
27
import org .elasticsearch .action .get .MultiGetItemResponse ;
27
28
import org .elasticsearch .action .get .MultiGetResponse ;
28
29
import org .elasticsearch .action .index .IndexRequest ;
35
36
import org .junit .*;
36
37
import org .junit .runner .RunWith ;
37
38
import org .springframework .beans .factory .annotation .Autowired ;
39
+ import org .springframework .data .annotation .Id ;
40
+ import org .springframework .data .annotation .Version ;
38
41
import org .springframework .data .domain .Page ;
39
42
import org .springframework .data .domain .PageRequest ;
40
43
import org .springframework .data .domain .Pageable ;
41
44
import org .springframework .data .domain .Sort ;
42
45
import org .springframework .data .elasticsearch .ElasticsearchException ;
46
+ import org .springframework .data .elasticsearch .annotations .Document ;
43
47
import org .springframework .data .elasticsearch .builder .SampleEntityBuilder ;
44
48
import org .springframework .data .elasticsearch .core .query .*;
49
+ import org .springframework .data .elasticsearch .entities .HetroEntity1 ;
50
+ import org .springframework .data .elasticsearch .entities .HetroEntity2 ;
45
51
import org .springframework .data .elasticsearch .entities .SampleEntity ;
46
52
import org .springframework .data .elasticsearch .entities .SampleMappingEntity ;
47
53
import org .springframework .test .context .ContextConfiguration ;
@@ -74,16 +80,18 @@ public void before() {
74
80
elasticsearchTemplate .refresh (SampleEntity .class , true );
75
81
}
76
82
77
- @ After
83
+
84
+ // @After
78
85
/** // doing a cleanup to ensure that no indexes are left behind after the test run */
86
+ /**
79
87
public void after() {
80
88
81
89
// it is safe to call deleteIndex as it checks for index existance before deleting it
82
90
elasticsearchTemplate.deleteIndex(INDEX_NAME);
83
91
elasticsearchTemplate.deleteIndex(INDEX_1_NAME);
84
92
elasticsearchTemplate.deleteIndex(INDEX_2_NAME);
85
93
86
- }
94
+ }**/
87
95
88
96
89
97
@ Test
@@ -706,7 +714,6 @@ public void shouldReturnListForGivenCriteria() {
706
714
@ Test
707
715
public void shouldReturnListForGivenStringQuery () {
708
716
// given
709
- List <IndexQuery > indexQueries = new ArrayList <IndexQuery >();
710
717
// first document
711
718
String documentId = randomNumeric (5 );
712
719
SampleEntity sampleEntity1 = new SampleEntityBuilder (documentId )
@@ -727,7 +734,7 @@ public void shouldReturnListForGivenStringQuery() {
727
734
.rate (15 )
728
735
.version (System .currentTimeMillis ()).build ();
729
736
730
- indexQueries = getIndexQueries (Arrays .asList (sampleEntity1 , sampleEntity2 , sampleEntity3 ));
737
+ List < IndexQuery > indexQueries = getIndexQueries (Arrays .asList (sampleEntity1 , sampleEntity2 , sampleEntity3 ));
731
738
732
739
// when
733
740
elasticsearchTemplate .bulkIndex (indexQueries );
@@ -1556,6 +1563,47 @@ public void shouldTestResultsAcrossMultipleIndices() {
1556
1563
assertThat (sampleEntities .size (), is (equalTo (2 )));
1557
1564
}
1558
1565
1566
+ @ Test
1567
+ /**
1568
+ * This is basically a demonstration to show composing entities out of heterogeneous indexes.
1569
+ */
1570
+ public void shouldComposeObjectsReturnedFromHeterogeneousIndexes () {
1571
+
1572
+ // Given
1573
+
1574
+ HetroEntity1 entity1 = new HetroEntity1 (randomNumeric (3 ), "aFirstName" );
1575
+ HetroEntity2 entity2 = new HetroEntity2 (randomNumeric (4 ), "aLastName" );
1576
+
1577
+ IndexQuery idxQuery1 = new IndexQueryBuilder ().withIndexName (INDEX_1_NAME ).withId (entity1 .getId ()).withObject (entity1 ).build ();
1578
+ IndexQuery idxQuery2 = new IndexQueryBuilder ().withIndexName (INDEX_2_NAME ).withId (entity2 .getId ()).withObject (entity2 ).build ();
1579
+
1580
+
1581
+ elasticsearchTemplate .bulkIndex (Arrays .asList (idxQuery1 , idxQuery2 ));
1582
+ elasticsearchTemplate .refresh (INDEX_1_NAME , true );
1583
+ elasticsearchTemplate .refresh (INDEX_2_NAME , true );
1584
+
1585
+ // When
1586
+
1587
+ SearchQuery searchQuery = new NativeSearchQueryBuilder ().withQuery (matchAllQuery ()).withTypes ("hetro" ).withIndices (INDEX_1_NAME , INDEX_2_NAME ).build ();
1588
+ Page <ResultAggregator > page = elasticsearchTemplate .queryForPage (searchQuery , ResultAggregator .class , new SearchResultMapper () {
1589
+ @ Override
1590
+ public <T > FacetedPage <T > mapResults (SearchResponse response , Class <T > clazz , Pageable pageable ) {
1591
+ List <ResultAggregator > values = new ArrayList <ResultAggregator >();
1592
+ for (SearchHit searchHit : response .getHits ()) {
1593
+ String id = String .valueOf (searchHit .getSource ().get ("id" ));
1594
+ String firstName = StringUtils .isNotEmpty ((String )searchHit .getSource ().get ("firstName" ))?(String )searchHit .getSource ().get ("firstName" ):"" ;
1595
+ String lastName = StringUtils .isNotEmpty ((String )searchHit .getSource ().get ("lastName" ))?(String )searchHit .getSource ().get ("lastName" ):"" ;
1596
+ values .add (new ResultAggregator (id , firstName , lastName ));
1597
+ }
1598
+ return new FacetedPageImpl <T >((List <T >) values );
1599
+ }
1600
+ });
1601
+
1602
+ assertThat (page .getTotalElements (), is (2l ));
1603
+
1604
+ }
1605
+
1606
+
1559
1607
private IndexQuery getIndexQuery (SampleEntity sampleEntity ) {
1560
1608
return new IndexQueryBuilder ().withId (sampleEntity .getId ()).withObject (sampleEntity ).build ();
1561
1609
}
@@ -1567,4 +1615,23 @@ private List<IndexQuery> getIndexQueries(List<SampleEntity> sampleEntities) {
1567
1615
}
1568
1616
return indexQueries ;
1569
1617
}
1618
+
1619
+ @ Document (indexName = INDEX_2_NAME , replicas = 0 , shards = 1 )
1620
+ class ResultAggregator {
1621
+
1622
+ private String id ;
1623
+ private String firstName ;
1624
+ private String lastName ;
1625
+
1626
+ ResultAggregator (String id , String firstName , String lastName ) {
1627
+ this .id = id ;
1628
+ this .firstName = firstName ;
1629
+ this .lastName = lastName ;
1630
+ }
1631
+
1632
+ }
1633
+
1634
+
1635
+
1636
+
1570
1637
}
0 commit comments