diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index ff2483d..2e487c0 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -56,7 +56,7 @@ class ImageController "" -> imageRepository.findAll(page) else -> { val distinctTags = tags.split(" ").distinct() - val tagData = distinctTags.mapNotNull { tagRepository.findByTagIs(it) } // Try to get actual tag objects + val tagData = tagRepository.findAllByTagIn(distinctTags) // Try to get actual tag objects when { tagData.isEmpty() -> null // No tags existed with the specified search terms tagData.size != distinctTags.size -> null // Error if an invalid tag was supplied diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 4670866..0c04cc2 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -13,10 +13,9 @@ interface UserRepository : CrudRepository { interface RoleRepository : CrudRepository interface ImageRepository : JpaRepository { - //@Query("select i from Image i join i.tags t where t in ?1 group by i.id having count(i.id) = ?2") @Suppress("SqlResolve") @Query( - value = "select * from booru.image i where i.id in (select image_id from booru.tag_image ti where ti.tag_id in ?1 group by ti.image_id having count(ti.image_id) = ?2) order by i.id", + value = "WITH X AS (SELECT IMAGE_ID FROM BOORU.TAG_IMAGE TI WHERE TI.TAG_ID in ?1 GROUP BY TI.IMAGE_ID HAVING COUNT(TI.IMAGE_ID) = ?2) SELECT I.* FROM BOORU.IMAGE I RIGHT JOIN X ON I.ID = X.IMAGE_ID ORDER BY I.ID", nativeQuery = true ) fun findByTagsOrderById(tagIDs: List, tagCount: Long, pageable: Pageable): Page @@ -26,4 +25,5 @@ interface ImageRepository : JpaRepository { interface TagRepository : CrudRepository { fun findByTagIs(tag: String): Tag? + fun findAllByTagIn(tags: List): List } \ No newline at end of file