简体   繁体   中英

Grails Searchable plugin with Criteria

I have some question about searchable plugin :

i have two domain :

class Ads { 
        static searchable = true 
        String fromCity 
        String toCity 
        User user 
     static constraints = { 
    } 
} 
class User { 
     String username 
     String password 
} 

And i have developed my own search page with two field (fromCity,toCity) . to have something like :

def listResults =  searchableService.search("NewYork","Miami") 

So I would like to know how I can give to my search method this to Criteria Field.

def srchResults = searchableService.search(??????) 

I'll be so grateful if someone can help me to do this.

First you need to define a searchable closure in your domain class. For instance

static searchable = {
        analyzer "simple"
        only = ['firstName','uuid']
        firstName boost: 5.0
    }

Then you can search as follow.

def searchResults = SomeDomain.search(textToSearch + "*" + " -(firstName: ${myName})", params)

-(firstName: ${myName}) this remove the my name from the search result, similarly you can and or other fields depending on your logic.

The default operator is "and" where as you can modify the operator, see following example

defaultOperator - Either "and" or "or". Default is to defer to the global Compass setting, which is "and" if not otherwise set by you.

search("mango chutney", defaultOperator: "or")
// ==> as if the query was "mango OR chutney"
//     without the option it would be like "mango AND chutney"

For more detail please see the documentation. Searchable Plugin Documentation

Let me know if you need any help.

More Help On Compass See section 12.5.1. Query String Syntax

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