0

I'm trying to bump an exact query first using Specification and Pageable from spring data.

When making a request to this endpoint http://localhost:8080/configs?search=PATH

This is the response now:

{
    "content": [
        {
            "groupId": "PATHOD",
            "type": "company",
        },
        {
            "groupId": "PATH",
            "type": "company",
        },
        {
            "groupId": "APATH",
            "type": "company",
        }
    ],
    "pageable": {
        ...
    },
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "size": 10,
    "empty": false
}

I want to be like, with the exact match bumped to first place:

{
    "content": [
        {
            "groupId": "APATH",
            "type": "company",
        },
        {
            "groupId": "PATH",
            "type": "company",
        },
        {
            "groupId": "PATHOD",
            "type": "company",
        }
    ],
    "pageable": {
        ....
    },
    "sort": {
        "sorted": true,
        "unsorted": false,
        "empty": false
    },
    "size": 10,
    "empty": false
}

This is my code right now, but I don't know how to write the specification

fun findAllWithFilters(
        exactMatchFirst: Boolean = false,
        pageable: Pageable
    ): Page<OrderConfig> {
        val spec = and(
            withExactMatchFirst(exactMatchFirst)
        )

        return findAll(spec, pageable)
    }

The PATH groupId should be the first one to appear in the results

  • Isn't this just a sorting problem? If so, have a look at [this baeldung article](https://www.baeldung.com/spring-data-sorting). – tgr Feb 22 '21 at 07:54
  • No it isn't, I've tried all the sorting directions. I have updated my question with more examples to explain better the problem – João Victor Machado Feb 23 '21 at 12:16
  • Do I understand correctly, that you want sort by relevance? – tgr Feb 23 '21 at 15:15
  • I want to bump the exact match to the first item of the array, but since I'm using pageable that needs to happen on the query level. Something like this answer https://stackoverflow.com/questions/31315669/how-to-use-mysql-like-with-order-by-exact-match-first – João Victor Machado Feb 23 '21 at 18:38

0 Answers0