简体   繁体   English

Grails Plugin Searchable - 默认通配符搜索

[英]Grails Plugin Searchable - default wildcard search

Is there a way to automatically wrap all searches with a wildcard? 有没有办法用通配符自动包装所有搜索?

eg: 例如:

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

I'm not familiar with .search (are you using a plugin?). 我不熟悉.search(你使用插件吗?)。 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: 在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
   }
}

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    

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM