File tree Expand file tree Collapse file tree 1 file changed +28
-0
lines changed
templates/kotlin/guides/search Expand file tree Collapse file tree 1 file changed +28
-0
lines changed Original file line number Diff line number Diff line change 1+ import java.io.File
2+ import kotlinx.serialization.builtins.ListSerializer
3+ import kotlinx.serialization.json.*
4+
5+ { {> snippets/import} }
6+ import com.algolia.client.model.search.*
7+
8+ suspend fun saveObjectsModified() {
9+ {{> snippets/init} }
10+
11+ val path = "/tmp/records.json"
12+ val json = try {
13+ File(path).readText()
14+ } catch (e: Exception) {
15+ throw RuntimeException(" Failed to read file at $path" , e)
16+ }
17+ val products: List<JsonObject > = Json.decodeFromString(ListSerializer(JsonObject.serializer()), json)
18+
19+ val records = products.map { product ->
20+ val reference = product[" product_reference" ].toString()
21+ val suffixes = reference.windowed(reference.length, 1, partialWindows = true ).drop(1).map { JsonPrimitive(it) }
22+ JsonObject(
23+ product + ("product_reference_suffixes" to JsonArray(suffixes)),
24+ )
25+ }
26+
27+ client.{ {#dynamicSnippet} }saveObjectsRecords{ {/dynamicSnippet} }
28+ }
You can’t perform that action at this time.
0 commit comments