24
24
import java .util .ArrayList ;
25
25
import java .util .List ;
26
26
import java .util .Set ;
27
+ import java .util .Map ;
28
+ import java .util .HashMap ;
27
29
28
30
import org .elasticsearch .action .index .IndexRequest ;
29
31
import org .elasticsearch .action .search .SearchResponse ;
@@ -890,6 +892,7 @@ public void shouldDeleteDocumentBySpecifiedTypeUsingDeleteQuery() {
890
892
indexQuery .setObject (sampleEntity );
891
893
892
894
elasticsearchTemplate .index (indexQuery );
895
+ elasticsearchTemplate .refresh (SampleEntity .class , true );
893
896
// when
894
897
DeleteQuery deleteQuery = new DeleteQuery ();
895
898
deleteQuery .setQuery (fieldQuery ("id" , documentId ));
@@ -1059,13 +1062,11 @@ public void shouldDoBulkIndexWithoutId() {
1059
1062
// given
1060
1063
List <IndexQuery > indexQueries = new ArrayList <IndexQuery >();
1061
1064
// first document
1062
- String documentId = randomNumeric (5 );
1063
1065
SampleEntity sampleEntity1 = new SampleEntity ();
1064
1066
sampleEntity1 .setMessage ("some message" );
1065
1067
sampleEntity1 .setVersion (System .currentTimeMillis ());
1066
1068
1067
1069
IndexQuery indexQuery1 = new IndexQuery ();
1068
- //indexQuery1.setId(documentId);
1069
1070
indexQuery1 .setObject (sampleEntity1 );
1070
1071
indexQueries .add (indexQuery1 );
1071
1072
@@ -1088,4 +1089,98 @@ public void shouldDoBulkIndexWithoutId() {
1088
1089
assertThat (sampleEntities .getContent ().get (0 ).getId (), is (notNullValue ()));
1089
1090
assertThat (sampleEntities .getContent ().get (1 ).getId (), is (notNullValue ()));
1090
1091
}
1092
+
1093
+ @ Test
1094
+ public void shouldIndexMapWithIndexNameAndTypeAtRuntime () {
1095
+ //given
1096
+ Map <String , Object > person1 = new HashMap <String , Object >();
1097
+ person1 .put ("userId" , "1" );
1098
+ person1 .
put (
"email" ,
"[email protected] " );
1099
+ person1 .put ("title" , "Mr" );
1100
+ person1 .put ("firstName" , "Mohsin" );
1101
+ person1 .put ("lastName" , "Husen" );
1102
+
1103
+ Map <String , Object > person2 = new HashMap <String , Object >();
1104
+ person2 .put ("userId" , "2" );
1105
+ person2 .
put (
"email" ,
"[email protected] " );
1106
+ person2 .put ("title" , "Mr" );
1107
+ person2 .put ("firstName" , "Artur" );
1108
+ person2 .put ("lastName" , "Konczak" );
1109
+
1110
+ IndexQuery indexQuery1 = new IndexQuery ();
1111
+ indexQuery1 .setId ("1" );
1112
+ indexQuery1 .setObject (person1 );
1113
+ indexQuery1 .setIndexName ("test-index" );
1114
+ indexQuery1 .setType ("test-type" );
1115
+
1116
+ IndexQuery indexQuery2 = new IndexQuery ();
1117
+ indexQuery2 .setId ("2" );
1118
+ indexQuery2 .setObject (person2 );
1119
+ indexQuery2 .setIndexName ("test-index" );
1120
+ indexQuery2 .setType ("test-type" );
1121
+
1122
+ List <IndexQuery > indexQueries = new ArrayList <IndexQuery >();
1123
+ indexQueries .add (indexQuery1 );
1124
+ indexQueries .add (indexQuery2 );
1125
+
1126
+ //when
1127
+ elasticsearchTemplate .bulkIndex (indexQueries );
1128
+ elasticsearchTemplate .refresh ("test-index" , true );
1129
+
1130
+ // then
1131
+ SearchQuery searchQuery = new NativeSearchQueryBuilder ().withIndices ("test-index" )
1132
+ .withTypes ("test-type" ).withQuery (matchAllQuery ()).build ();
1133
+ Page <Map > sampleEntities = elasticsearchTemplate .queryForPage (searchQuery , Map .class , new SearchResultMapper () {
1134
+ @ Override
1135
+ public <T > FacetedPage <T > mapResults (SearchResponse response , Class <T > clazz , Pageable pageable ) {
1136
+ List <Map > chunk = new ArrayList <Map >();
1137
+ for (SearchHit searchHit : response .getHits ()) {
1138
+ if (response .getHits ().getHits ().length <= 0 ) {
1139
+ return null ;
1140
+ }
1141
+ Map <String , Object > person = new HashMap <String , Object >();
1142
+ person .put ("userId" , searchHit .getSource ().get ("userId" ));
1143
+ person .put ("email" , searchHit .getSource ().get ("email" ));
1144
+ person .put ("title" , searchHit .getSource ().get ("title" ));
1145
+ person .put ("firstName" , searchHit .getSource ().get ("firstName" ));
1146
+ person .put ("lastName" , searchHit .getSource ().get ("lastName" ));
1147
+ chunk .add (person );
1148
+ }
1149
+ if (chunk .size () > 0 ) {
1150
+ return new FacetedPageImpl <T >((List <T >) chunk );
1151
+ }
1152
+ return null ;
1153
+ }
1154
+ });
1155
+ assertThat (sampleEntities .getTotalElements (), is (equalTo (2L )));
1156
+ assertThat (sampleEntities .getContent ().get (0 ).get ("userId" ), is (person1 .get ("userId" )));
1157
+ assertThat (sampleEntities .getContent ().get (1 ).get ("userId" ), is (person2 .get ("userId" )));
1158
+ }
1159
+
1160
+ @ Test
1161
+ public void shouldIndexSampleEntityWithIndexAndTypeAtRuntime () {
1162
+ // given
1163
+ String documentId = randomNumeric (5 );
1164
+ SampleEntity sampleEntity = new SampleEntity ();
1165
+ sampleEntity .setId (documentId );
1166
+ sampleEntity .setMessage ("some message" );
1167
+ sampleEntity .setVersion (System .currentTimeMillis ());
1168
+
1169
+ IndexQuery indexQuery = new IndexQuery ();
1170
+ indexQuery .setId (documentId );
1171
+ indexQuery .setIndexName ("test-index" );
1172
+ indexQuery .setType ("test-type" );
1173
+ indexQuery .setObject (sampleEntity );
1174
+
1175
+ elasticsearchTemplate .index (indexQuery );
1176
+ elasticsearchTemplate .refresh ("test-index" , true );
1177
+
1178
+ SearchQuery searchQuery = new NativeSearchQueryBuilder ().withIndices ("test-index" )
1179
+ .withTypes ("test-type" ).withQuery (matchAllQuery ()).build ();
1180
+ // when
1181
+ Page <SampleEntity > sampleEntities = elasticsearchTemplate .queryForPage (searchQuery , SampleEntity .class );
1182
+ // then
1183
+ assertThat (sampleEntities , is (notNullValue ()));
1184
+ assertThat (sampleEntities .getTotalElements (), greaterThanOrEqualTo (1L ));
1185
+ }
1091
1186
}
0 commit comments