Skip to content

Commit 0605218

Browse files
authored
Merge pull request #30 from DarioBalinzo/additional-fields
Additional fields
2 parents e3df9af + b1aa186 commit 0605218

File tree

3 files changed

+150
-72
lines changed

3 files changed

+150
-72
lines changed

src/main/java/com/github/dariobalinzo/elastic/ElasticRepository.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -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()) {

src/test/java/com/github/dariobalinzo/elastic/ElasticRepositoryTest.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import java.io.IOException;
2525
import java.util.Collections;
2626

27-
import static org.junit.Assert.assertEquals;
28-
import static org.junit.Assert.assertNull;
27+
import static org.junit.Assert.*;
2928

3029
public class ElasticRepositoryTest extends TestContainersContext {
3130

@@ -94,4 +93,19 @@ public void shouldFetchDataUsingSecondarySortField() throws IOException, Interru
9493
assertNull(emptyPage.getLastCursor().getSecondaryCursor());
9594
}
9695

96+
@Test
97+
public void shouldFetchDataWithAdditionalField() throws IOException, InterruptedException {
98+
deleteTestIndex();
99+
100+
insertMockData(110, "customerA", TEST_INDEX);
101+
insertMockData(111, "customerB", TEST_INDEX);
102+
refreshIndex();
103+
104+
PageResult firstPage = repository.searchAfter(TEST_INDEX, Cursor.empty());
105+
firstPage.getDocuments().forEach(item -> {
106+
assertNotNull(item.get((String) "es-index"));
107+
assertNotNull(item.get((String) "es-id"));
108+
});
109+
}
110+
97111
}

0 commit comments

Comments
 (0)