Grails Plugin Searchable - 默认通配符搜索

时间:2013-03-13 15:46:17

标签: grails searchable grails-searchable

有没有办法用通配符自动包装所有搜索?

e.g:

 Book.search("*${params.q}*", params)

1 个答案:

答案 0 :(得分:2)

我不熟悉.search(你使用插件吗?)。但是,对于模型中的通配符搜索,我通常在域模型类中创建一个方法。在您的示例中,

在Book模型类中:

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
   }
}

在您的控制器中:

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)
}

示例网址:

http://localhost:8080/mytest/mycontroller/searchBooks?title=Arms