Skip to content

Commit d8bf8ee

Browse files
author
Manuel Seiche
committed
added documentCreator to WriteDOcumentsToElasticsearchIndexChain
1 parent c92f18f commit d8bf8ee

File tree

1 file changed

+43
-17
lines changed

1 file changed

+43
-17
lines changed

src/main/java/io/github/cupybara/javalangchains/chains/data/writer/WriteDocumentsToElasticsearchIndexChain.java

Lines changed: 43 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,6 @@ public class WriteDocumentsToElasticsearchIndexChain implements Chain<Stream<Map
3737
*/
3838
private final RestClientBuilder restClientBuilder;
3939

40-
/**
41-
* {@link ObjectMapper} used for document serialization
42-
*/
43-
private final ObjectMapper objectMapper;
44-
4540
/**
4641
* Optional {@link Function} which provides an ID value for a document. If set,
4742
* documents are indexed using PUT /_doc/${id} instead of POST /_doc
@@ -54,6 +49,28 @@ public class WriteDocumentsToElasticsearchIndexChain implements Chain<Stream<Map
5449
*/
5550
private final BiConsumer<String, RestClient> indexCreator;
5651

52+
/**
53+
* creates the effective document json from an input document
54+
*/
55+
private final Function<Map<String, String>, String> documentJsonCreator;
56+
57+
/**
58+
* @param index {@link #index}
59+
* @param restClientBuilder {@link #restClientBuilder}
60+
* @param idProvider {@link #idProvider}
61+
* @param indexCreator {@link #indexCreator}
62+
* @param documentJsonCreator {@link #documentJsonCreator}
63+
*/
64+
public WriteDocumentsToElasticsearchIndexChain(final String index, final RestClientBuilder restClientBuilder,
65+
final Function<Map<String, String>, String> idProvider, final BiConsumer<String, RestClient> indexCreator,
66+
final Function<Map<String, String>, String> documentJsonCreator) {
67+
this.index = index;
68+
this.restClientBuilder = restClientBuilder;
69+
this.idProvider = idProvider;
70+
this.indexCreator = indexCreator;
71+
this.documentJsonCreator = documentJsonCreator;
72+
}
73+
5774
/**
5875
* @param index {@link #index}
5976
* @param restClientBuilder {@link #restClientBuilder}
@@ -64,11 +81,8 @@ public class WriteDocumentsToElasticsearchIndexChain implements Chain<Stream<Map
6481
public WriteDocumentsToElasticsearchIndexChain(final String index, final RestClientBuilder restClientBuilder,
6582
final ObjectMapper objectMapper, final Function<Map<String, String>, String> idProvider,
6683
final BiConsumer<String, RestClient> indexCreator) {
67-
this.index = index;
68-
this.restClientBuilder = restClientBuilder;
69-
this.objectMapper = objectMapper;
70-
this.idProvider = idProvider;
71-
this.indexCreator = indexCreator;
84+
this(index, restClientBuilder, idProvider, defaultIndexCreator(objectMapper),
85+
defaultDocumentJsonCreator(objectMapper));
7286
}
7387

7488
/**
@@ -122,13 +136,7 @@ public Void run(final Stream<Map<String, String>> input) {
122136
}
123137

124138
input.forEach(document -> {
125-
final String documentJson;
126-
try {
127-
documentJson = objectMapper.writeValueAsString(document);
128-
} catch (final JsonProcessingException jsonProcessingException) {
129-
throw new IllegalStateException("error creating json for document " + document,
130-
jsonProcessingException);
131-
}
139+
final String documentJson = documentJsonCreator.apply(document);
132140

133141
final Request indexRequest = createIndexRequest(document);
134142
indexRequest.setJsonEntity(documentJson);
@@ -177,6 +185,24 @@ private synchronized void createIndexIfNotExists(final RestClient restClient) th
177185
this.indexCreator.accept(index, restClient);
178186
}
179187

188+
/**
189+
* creates the default {@link Function} for creating json documents from input
190+
* documents for this chain
191+
*
192+
* @param objectMapper {@link ObjectMapper} for json operations
193+
* @return default {@link #documentJsonCreator}
194+
*/
195+
public static Function<Map<String, String>, String> defaultDocumentJsonCreator(final ObjectMapper objectMapper) {
196+
return document -> {
197+
try {
198+
return objectMapper.writeValueAsString(document);
199+
} catch (final JsonProcessingException jsonProcessingException) {
200+
throw new IllegalStateException("error creating json for document " + document,
201+
jsonProcessingException);
202+
}
203+
};
204+
}
205+
180206
/**
181207
* Realizes the default way of creating an elasticsearch index using the method
182208
* from

0 commit comments

Comments
 (0)