I suggest you look at using criteria. So in this case you could do this:
def filter = 'joh'
def searchResults = Company.createCriteria().list() {
employee {
ilike('name', "%${filter}%")
}
}
Question
I have a filter, for example lets assume the name John Doe
is who a user is searching for.
I have the following domain object structure: Company--->Employee--->Name
. Where employee is an attribute of company, and name is an attribute of employee. Now, the filter is for the name attribute, but I need to keep a reference to the Company. So for example I did:
companyList.events.each {
if(it.employee!=null){
if(it.employee.name.toString().toLowerCase().contains(filter)){
filterSet.add(it)
}
}
Unfortunately, this operation is very slow for 1500 entries. However if I do something like,
def searchResults = Company.findAll{
employee.name == filter
}
It is very fast, but I need the filter to get matched with partial names (i.e. saying Joh
would still match John Doe
. I know there is an operation called like
but I have been unsuccessful using it.
Any help would be really appreciated.
Solution
I suggest you look at using criteria. So in this case you could do this:
def filter = 'joh'
def searchResults = Company.createCriteria().list() {
employee {
ilike('name', "%${filter}%")
}
}
OTHER TIPS
Try this:
Company.findAll ("from Company c where c.employee.name like '%'| |:filterName| |'%'", [filterName: yourFilterName])
This query has to be fast.