简体   繁体   中英

Grails Searchable return unique or distinct results

I'm doing a search query on Grails using searchable, but I want to return only distinct results.

years = House.searchEvery('(house_type:"condo")', [sort: 'house_year', order: 'desc'])

How do I make the house_year unique/distinct, or do I need to just parse it myself?

If you want to eliminate duplicates from the results (and in this case a duplicate is considered an object that has the same house_year value), you can use the unique method Groovy adds to Collection

years = House.searchEvery('(house_type:"condo")', [sort: 'house_year', order: 'desc'])
def uniqueYears = years.unique {it.house_year}

obviously there is no simple way to do this with the help of the searchable plugin. after searching for your term you can filtering you result set by your own. in your case i would try to use a regular grails criteria or a hql query. eg

House.createCriteria.listDistinct {
   order("house_year", "desc")
   eq("house_type", "condo")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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