Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict search to component's field value with grails searchable plugin

Using grails searchable plugin, I would like to search for all products within a specific category using a query builder like:

Products.search {
  must(queryString(params.q))
  must(term('??????','Food'))  
}

Using 'category.name' returns: Failed to find mapping for alias [category] and path [category.name]

class Product {    
  String name
  String desc
  Category category

  static searchable = {
    category component: true
  }
}

class Category {      
  String name

  static hasMany = [products: Product]

  static searchable = true     
}

Any ideas? Thanks.

like image 423
Micor Avatar asked Nov 28 '25 00:11

Micor


1 Answers

I think you can do something like:

def results = Product.search {
  must(term('$/Product/category/name', params.categoryName))
  must(queryString(params.q))
}

Hope this helps!

like image 64
Maricel Avatar answered Nov 29 '25 22:11

Maricel



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!