Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

grails searchable plugin query

My Grails app is using the searchable plugin, which builds on Compass and Lucene to provide search functionality. I have two searchable classes, say Author and Book. I have mapped these classes to the search index, so that only certain fields can be searched.

To perform a search across both classes I simply call

def results = searchableService.search(query)

One of the nice features of doing the search across both class simultaneously, is that the results object includes metadata about number of results included, number of results available, pagination details etc.

I recently added a boolean approved flag to the Book class and I never want unapproved books to appear in the search results. One option is to replace the call above with:

def bookResults = Book.search(query + " approved:1")
def authorResults = Author.search(query)

However, I now need to figure out how to combine the metadata for both results, which is likely to be tricky (particularly pagination).

Is there a way to search across Book and Author with a single query, but only return approved books?

like image 621
Dónal Avatar asked Jan 10 '12 10:01

Dónal


1 Answers

I've created a test app and came to the following solution. maybe it helps...

if the property approved only has the states 0 and 1, the following query will work:

def results = searchableService.search(
    "(${query} AND approved:1) OR (${query} -approved:0 -approved:1)"
)

I guess this can be reformulated in a better way if you don't use the QueryParser but the BooleanQueryBuilder.

BTW: if you add a method like

String getType() { "Book" }

and

String getType() { "Author" }

To your domains, you can even configure your search to do it like this

def results = searchableService.search(
    "(${query} AND approved:1) OR (${query} AND type:Author)"
)
like image 78
rdmueller Avatar answered Sep 28 '22 08:09

rdmueller