in core/search/src/main/kotlin/io/klibs/core/search/PackageSearchRepositoryJdbc.kt [21:118]
override fun find(
rawQuery: String?,
platforms: List<PackagePlatform>,
targetFilters: Map<TargetGroup, Set<String>>,
ownerLogin: String?,
sortBy: SearchSort,
page: Int,
limit: Int
): List<SearchPackageResult> {
val isQueryPresent = rawQuery?.isBlank() == false
val offset = limit * (page - 1)
val exactMatchQuery = rawQuery?.normalizeSearchQuery()
val exactMatchWildcardQuery = exactMatchQuery?.addWildcardPostfix()
val (wildcardQueryWithSpecialSymbols, wildcardQueryWithoutSpecialSymbols) = rawQuery?.let {
createWildcardSubqueries(
it
)
} ?: Pair("", "")
val targetCondition = formTargetCondition(targetFilters)
val sql = buildString {
append("SELECT group_id, artifact_id, latest_version, latest_description, release_ts, ")
append("owner_type, owner_login, latest_license_name, array_to_string(platforms, ',') AS platforms, ")
append("array_to_string(targets, ',') AS targets")
// For debugging and testing purposes
append(", targets_vector")
if (isQueryPresent && sortBy == SearchSort.RELEVANCY) {
append(
", (ts_rank_cd(fts, :exactMatchQuery ::tsquery) * 0.7 + " +
"ts_rank_cd(fts, :wildcardQueryWithSpecialSymbols ::tsquery || wildcardQueryWithoutSpecialSymbols ::tsquery || :exactMatchWildcardQuery ::tsquery) * 0.3) AS weighted_rank"
)
}
appendLine(" FROM package_index")
var prefix = "WHERE"
if (isQueryPresent) {
appendLine(", to_tsquery('english', :wildcardQueryWithoutSpecialSymbols) wildcardQueryWithoutSpecialSymbols")
appendLine(
" WHERE (:wildcardQueryWithSpecialSymbols ::tsquery || wildcardQueryWithoutSpecialSymbols || :exactMatchWildcardQuery ::tsquery) @@ fts"
)
prefix = "AND"
}
if (platforms.isNotEmpty()) {
val platformsQuery = platforms.distinct().joinToString(separator = " & ") { it.name }
appendLine(" $prefix platforms_vector @@ '$platformsQuery'")
prefix = "AND"
}
if (ownerLogin != null) {
appendLine(" $prefix owner_login = :ownerLogin")
prefix = "AND"
}
if (targetCondition != null) {
appendLine(" $prefix targets_vector @@ $targetCondition")
prefix = "AND"
}
if (targetFilters.containsKey(TargetGroup.JavaScript)) {
appendLine(" $prefix platforms_vector @@ 'JS'")
prefix = "AND"
}
if (targetFilters.containsKey(TargetGroup.Wasm)) {
appendLine(" $prefix platforms_vector @@ 'WASM'")
}
val orderBy = when {
sortBy == SearchSort.RELEVANCY && isQueryPresent -> "weighted_rank DESC"
else -> "release_ts DESC"
}
appendLine(" ORDER BY $orderBy")
appendLine(" LIMIT $limit")
appendLine(" OFFSET $offset")
}
@Suppress("SqlSourceToSinkFlow")
val query = jdbcClient.sql(sql)
.param("limit", limit)
.param("offset", offset)
.param("exactMatchQuery", exactMatchQuery)
.param("exactMatchWildcardQuery", exactMatchWildcardQuery)
.param("wildcardQueryWithSpecialSymbols", wildcardQueryWithSpecialSymbols)
.param("wildcardQueryWithoutSpecialSymbols", wildcardQueryWithoutSpecialSymbols)
.param("ownerLogin", ownerLogin)
return query.query(PACKAGE_OVERVIEW_ROW_MAPPER).list()
}