@@ -88,9 +88,7 @@ public PageResult searchAfter(String index, Cursor cursor) throws IOException, I
8888
8989 SearchResponse response = executeSearch (searchRequest );
9090
91- List <Map <String , Object >> documents = Arrays .stream (response .getHits ().getHits ())
92- .map (SearchHit ::getSourceAsMap )
93- .collect (Collectors .toList ());
91+ List <Map <String , Object >> documents = extractDocuments (response );
9492
9593 Cursor lastCursor ;
9694 if (documents .isEmpty ()) {
@@ -102,6 +100,16 @@ public PageResult searchAfter(String index, Cursor cursor) throws IOException, I
102100 return new PageResult (index , documents , lastCursor );
103101 }
104102
103+ private List <Map <String , Object >> extractDocuments (SearchResponse response ) {
104+ return Arrays .stream (response .getHits ().getHits ())
105+ .map (hit -> {
106+ Map <String , Object > sourceMap = hit .getSourceAsMap ();
107+ sourceMap .put ("es-id" , hit .getId ());
108+ sourceMap .put ("es-index" , hit .getIndex ());
109+ return sourceMap ;
110+ }).collect (Collectors .toList ());
111+ }
112+
105113 public PageResult searchAfterWithSecondarySort (String index , Cursor cursor ) throws IOException , InterruptedException {
106114 Objects .requireNonNull (secondaryCursorField );
107115 String primaryCursor = cursor .getPrimaryCursor ();
@@ -122,9 +130,7 @@ public PageResult searchAfterWithSecondarySort(String index, Cursor cursor) thro
122130
123131 SearchResponse response = executeSearch (searchRequest );
124132
125- List <Map <String , Object >> documents = Arrays .stream (response .getHits ().getHits ())
126- .map (SearchHit ::getSourceAsMap )
127- .collect (Collectors .toList ());
133+ List <Map <String , Object >> documents = extractDocuments (response );
128134
129135 Cursor lastCursor ;
130136 if (documents .isEmpty ()) {
0 commit comments