forked from arcanetechnology/arcane-platform
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: updated major version of algolia client library
- Loading branch information
1 parent
81a4bd6
commit 59ac4d2
Showing
18 changed files
with
219 additions
and
195 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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:_") | ||
} |
57 changes: 57 additions & 0 deletions
57
libs/utils/algolia/src/main/kotlin/com/k33/platform/utils/algolia/AlgoliaRecommendClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} | ||
} |
76 changes: 76 additions & 0 deletions
76
libs/utils/algolia/src/main/kotlin/com/k33/platform/utils/algolia/AlgoliaSearchClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
) | ||
} | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
libs/utils/algolia/src/main/kotlin/com/k33/platform/utils/algolia/Config.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")), | ||
) | ||
} |
22 changes: 22 additions & 0 deletions
22
libs/utils/algolia/src/main/kotlin/com/k33/platform/utils/algolia/Model.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 3 additions & 3 deletions
6
libs/utils/cms/contentful/src/main/kotlin/com/k33/platform/cms/JsonObjectId.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 0 additions & 7 deletions
7
libs/utils/cms/contentful/src/main/kotlin/com/k33/platform/cms/sync/Algolia.kt
This file was deleted.
Oops, something went wrong.
58 changes: 0 additions & 58 deletions
58
.../utils/cms/contentful/src/main/kotlin/com/k33/platform/cms/sync/AlgoliaRecommendClient.kt
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.