From f48ecb731ad5ecdf624cbbcf396223f6644b2ac7 Mon Sep 17 00:00:00 2001 From: Thomas Raffray Date: Fri, 24 Jan 2025 10:47:28 +0100 Subject: [PATCH] feat(kotlin): savePopularRecords --- .../guides/search/savePopularRecords.mustache | 23 +++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 templates/kotlin/guides/search/savePopularRecords.mustache diff --git a/templates/kotlin/guides/search/savePopularRecords.mustache b/templates/kotlin/guides/search/savePopularRecords.mustache new file mode 100644 index 0000000000..a9897c97b1 --- /dev/null +++ b/templates/kotlin/guides/search/savePopularRecords.mustache @@ -0,0 +1,23 @@ +{{> snippets/import}} +import com.algolia.client.model.search.* + +suspend fun savePopularRecords() { + {{> snippets/init}} + + var records: List = listOf() + + client.browseObjects(indexName, BrowseParamsObject(), aggregator = { response -> + records = records + response.hits.map { hit -> + val props = hit.additionalProperties ?: mapOf() + + val nbFollowers = props["nbFollowers"].toString().toInt() + + buildJsonObject { + put("twitterHandle", JsonPrimitive(props["twitterHandle"].toString())) + put("nbFollowers", JsonPrimitive(nbFollowers)) + put("isPopular", JsonPrimitive(nbFollowers > 1000000)) + } + } + }) + client.{{#dynamicSnippet}}saveObjectsRecords{{/dynamicSnippet}} +} \ No newline at end of file