From 90bb712bc352538fd7d30c93eb3dd3e92026a45b Mon Sep 17 00:00:00 2001 From: neviyn Date: Fri, 30 Apr 2021 16:56:19 +0100 Subject: [PATCH] Forced ordering of search results --- src/main/kotlin/uk/co/neviyn/booru/Controller.kt | 4 ++-- src/main/kotlin/uk/co/neviyn/booru/Repository.kt | 7 ++++--- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt index ee81cc5..ff2483d 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Controller.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Controller.kt @@ -62,11 +62,11 @@ class ImageController tagData.size != distinctTags.size -> null // Error if an invalid tag was supplied tagData.size == 1 -> { // Simpler query for single tag searches - val result = imageRepository.findAllByTagsContaining(tagData[0], page) + val result = imageRepository.findAllByTagsContainingOrderById(tagData[0], page) if (result.isEmpty) null else result } else -> { - val result = imageRepository.findByTags(tagData, tagData.size.toLong(), page) + val result = imageRepository.findByTagsOrderById(tagData, tagData.size.toLong(), page) if (result.isEmpty) null else result } } diff --git a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt index 7197b5a..4670866 100644 --- a/src/main/kotlin/uk/co/neviyn/booru/Repository.kt +++ b/src/main/kotlin/uk/co/neviyn/booru/Repository.kt @@ -14,13 +14,14 @@ 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)", + 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", nativeQuery = true ) - fun findByTags(tagIDs: List, tagCount: Long, pageable: Pageable): Page + fun findByTagsOrderById(tagIDs: List, tagCount: Long, pageable: Pageable): Page - fun findAllByTagsContaining(tag: Tag, pageable: Pageable): Page + fun findAllByTagsContainingOrderById(tag: Tag, pageable: Pageable): Page } interface TagRepository : CrudRepository {