Reputation: 58522
Is there a way to automatically wrap all searches with a wildcard?
e.g.:
Book.search("*${params.q}*", params)
Upvotes: 3
Views: 372
Reputation: 12518
I'm not familiar with .search (are you using a plugin?). However, for a wildcard search in models, I typically create a method inside the domain model class. In your example,
In the Book model class:
class Book {
String title
String author
int year
static List wildSearch(par, val) {
def foundList = this.executeQuery("select b FROM Book b WHERE ${par} like \'%${val}%\'")
return foundList
}
}
In your controller:
def searchBook = {
def b1 = new Book(title: "Farewell To Arms", author: "Ernest Hemingway").save()
def b2 = new Book(title: "The Brother's Karamazov", author: "Anton Chekov").save()
def b3 = new Book(title: "Brothers in Arms", author: "Cherry Dalton").save()
// If you search for "Arms", This returns b1 and b3
def found = Book.wildSearch("title", params.title)
}
Example URL:
http://localhost:8080/mytest/mycontroller/searchBooks?title=Arms
Upvotes: 2