Search code examples
grailsnamed-query

grails namedQueries how to use OR


Essentially I have 3 fields I want to check for:

class Book {
   String title
   String ISBN
   String type
}

I am trying to use named queries to do a case insensitive search of each of those 3 fields based on a single search string. Here is what I currently have:

    static namedQueries = {

        search { String searchString ->
        ilike("title", searchString) 
        or {
           ilike("ISBN", searchString)
        }
        or { 
           ilike("type", searchString)
        }
    }

I don't get any exceptions or anything, however, it simply returns no results when there should be some. Any ideas?

EDIT:

I updated to doelleri's example and it is working now. Here is the last portion of the query:

from book this_ where (lower(this_.title) like ? or lower(this_.isbn) like ? or lower(this_.type) like ?)

I also did a breakpoint inside the named query to determine if the string was getting passed in, and it was.


Solution

  • I believe the correct syntax is

    static namedQueries = {
        search { String searchString ->
            or {
                ilike("title", searchString)
                ilike("ISBN", searchString)
                ilike("type", searchString)
            }
        }
    }