Skip to content

Commit

Permalink
feat: updated major version of algolia client library
Browse files Browse the repository at this point in the history
  • Loading branch information
vihangpatil committed Aug 23, 2024
1 parent 81a4bd6 commit 59ac4d2
Show file tree
Hide file tree
Showing 18 changed files with 219 additions and 195 deletions.
14 changes: 14 additions & 0 deletions libs/utils/algolia/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
plugins {
`java-library`
kotlin("jvm")
kotlin("plugin.serialization")
}

dependencies {
implementation(project(":libs:utils:config"))
implementation(project(":libs:utils:logging"))

implementation(Ktor.plugins.serialization.kotlinx.json)

implementation("com.algolia:algoliasearch-client-kotlin:_")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package com.k33.platform.utils.algolia

import com.algolia.client.api.RecommendClient
import com.algolia.client.model.recommend.GetRecommendationsParams
import com.algolia.client.model.recommend.RecommendHit
import com.algolia.client.model.recommend.RelatedModel
import com.algolia.client.model.recommend.RelatedQuery

class AlgoliaRecommendClient(
applicationId: Algolia.ApplicationId,
apiKey: Algolia.ApiKey,
private val index: Algolia.Index,
) {
private val client by lazy {
RecommendClient(
appId = applicationId.value,
apiKey = apiKey.value,
)
}

suspend fun getRelated(
objectID: Algolia.ObjectID,
): List<Algolia.ObjectID> {
return client
.getRecommendations(
getRecommendationsParams = GetRecommendationsParams(
requests = listOf(
RelatedQuery(
indexName = index.name,
maxRecommendations = 3,
threshold = 40.0,
model = RelatedModel.RelatedProducts,
objectID = objectID.value,
)
)
)
)
.results
.flatMap { recommendationsResult ->
recommendationsResult.hits.map { hit ->
Algolia.ObjectID((hit as RecommendHit).objectID)
}
}
}

companion object {
fun getInstance(
index: Algolia.Index,
): AlgoliaRecommendClient {
return AlgoliaRecommendClient(
applicationId = algoliaConfig.applicationId,
apiKey = algoliaConfig.apiKey,
index = index,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
package com.k33.platform.utils.algolia

import com.algolia.client.api.SearchClient
import com.algolia.client.extensions.replaceAllObjects
import com.algolia.client.model.search.BrowseParamsObject
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.jsonPrimitive

class AlgoliaSearchClient(
applicationId: Algolia.ApplicationId,
apiKey: Algolia.ApiKey,
private val index: Algolia.Index,
) {

private val client by lazy {
SearchClient(
appId = applicationId.value,
apiKey = apiKey.value,
)
}

suspend fun upsert(
objectID: Algolia.ObjectID,
record: JsonObject,
) {
client.addOrUpdateObject(
indexName = index.name,
objectID = objectID.value,
body = record,
)
}

suspend fun batchUpsert(
records: List<JsonObject>
) {
client.replaceAllObjects(
indexName = index.name,
objects = records,
)
}

suspend fun delete(
objectID: Algolia.ObjectID,
) {
client.deleteObject(
indexName = index.name,
objectID = objectID.value,
)
}

suspend fun getAllIds(): Map<Algolia.ObjectID, String> {
return client
.browse(
indexName = index.name,
browseParams = BrowseParamsObject(
attributesToRetrieve = listOf("publishedAt"),
)
)
.hits
.associate {
Algolia.ObjectID(it.objectID) to it.additionalProperties!!["publishedAt"]!!.jsonPrimitive.content
}
}

companion object {
fun getInstance(
index: Algolia.Index,
): AlgoliaSearchClient {
return AlgoliaSearchClient(
applicationId = algoliaConfig.applicationId,
apiKey = algoliaConfig.apiKey,
index = index,
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.k33.platform.utils.algolia

data class AlgoliaConfig(
val applicationId: Algolia.ApplicationId,
val apiKey: Algolia.ApiKey,
)

val algoliaConfig: AlgoliaConfig by lazy {
AlgoliaConfig(
applicationId = Algolia.ApplicationId(System.getenv("ALGOLIA_APP_ID")),
apiKey = Algolia.ApiKey(System.getenv("ALGOLIA_API_KEY")),
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.k33.platform.utils.algolia

object Algolia {

object Key {
const val ObjectID = "objectID"
}

@JvmInline
value class ApplicationId(val value: String)

@JvmInline
value class ApiKey(val value: String)

@Suppress("EnumEntryName")
enum class Index {
articles,
}

@JvmInline
value class ObjectID(val value: String)
}
4 changes: 2 additions & 2 deletions libs/utils/cms/contentful/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ plugins {
}

dependencies {
implementation(project(":libs:utils:algolia"))

implementation(project(":libs:utils:config"))
implementation(project(":libs:utils:logging"))
implementation(project(":libs:utils:slack"))
Expand All @@ -27,8 +29,6 @@ dependencies {
implementation("org.apache.logging.log4j:log4j-to-slf4j:_")
implementation("org.apache.logging.log4j:log4j-core:_")

implementation("com.algolia:algoliasearch-client-kotlin:_")

// test
testImplementation("io.kotest:kotest-runner-junit5-jvm:_")
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package com.k33.platform.cms

import com.algolia.search.helper.toObjectID
import com.k33.platform.cms.sync.Algolia
import com.k33.platform.utils.algolia.Algolia
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.contentOrNull
import kotlinx.serialization.json.jsonPrimitive

val JsonObject.objectIDString get() = get(Algolia.Key.ObjectID)?.jsonPrimitive?.contentOrNull
val JsonObject.objectID get() = objectIDString?.toObjectID()

val JsonObject.objectID get() = objectIDString?.let(Algolia::ObjectID)
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,25 @@

package com.k33.platform.cms.config

import com.k33.platform.utils.algolia.Algolia

@Suppress("EnumEntryName")
enum class Sync(
val config: SyncConfig,
) {
researchArticles(
SyncConfig(
ContentfulSpace.research,
AlgoliaIndexName.articles,
Algolia.Index.articles,
)
),
}

data class SyncConfig(
val contentfulSpace: ContentfulSpace,
val algoliaIndexName: AlgoliaIndexName,
val algoliaIndex: Algolia.Index,
)

@Suppress("EnumEntryName")
enum class AlgoliaIndexName {
articles,
}

//
// Contentful
//
Expand All @@ -46,19 +43,3 @@ data class ContentfulSpaceConfig(
val token: String,
val cmaToken: String,
)

//
// Algolia
//

data class AlgoliaConfig(
val applicationId: String,
val apiKey: String,
)

val algoliaCconfig: AlgoliaConfig by lazy {
AlgoliaConfig(
applicationId = System.getenv("ALGOLIA_APP_ID"),
apiKey = System.getenv("ALGOLIA_API_KEY"),
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.k33.platform.cms.space.research.article
import com.k33.platform.cms.clients.ContentfulGraphql
import com.k33.platform.cms.content.Content
import com.k33.platform.cms.objectIDString
import com.k33.platform.cms.sync.Algolia
import com.k33.platform.cms.utils.optional
import com.k33.platform.cms.utils.richToPlainText
import com.k33.platform.utils.algolia.Algolia
import com.k33.platform.utils.config.lazyResourceWithoutWhitespace
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.contentOrNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package com.k33.platform.cms.space.research.page
import com.k33.platform.cms.clients.ContentfulGraphql
import com.k33.platform.cms.content.Content
import com.k33.platform.cms.objectIDString
import com.k33.platform.cms.sync.Algolia
import com.k33.platform.cms.utils.optional
import com.k33.platform.cms.utils.richToPlainText
import com.k33.platform.utils.algolia.Algolia
import com.k33.platform.utils.config.lazyResourceWithoutWhitespace
import kotlinx.serialization.json.JsonObject
import kotlinx.serialization.json.contentOrNull
Expand Down

This file was deleted.

This file was deleted.

Loading

0 comments on commit 59ac4d2

Please sign in to comment.