简体   繁体   中英

Grails searchable

Im trying to 'get my toes wet' with grails, and have decided to make a recipe site as a first project. Im using grails 2.0.1 and using mongoDB GORM for persistence, which is working fine, and am using static Searchable = true in my models for searching.

Making a simple search utility, I have managed to find recipes by name using:

def recipes = Recipe.withCriteria
{
    ilike('name', params.name)
}

The recipes can be found by name. My question is, how can I search the ingredient names to flag as results in the search (regarding the models below)? Coming from PHP and MySQL, this would be as easy as modifying the query with a join or something

my models are as follows:

class Recipe 
{
    String name;
    String method;
    Date dateAdded;

    static hasMany = [ingredients:Ingredient];    
    static Searchable = true;
    static constraints = 
    {
        name(blank:false, maxSize: 255)
        method(blank:false)
    }

    static mapping = 
    {
        sort dateAdded: "desc"
    }
}

class Ingredient 
{
    String name;

    static hasMany = [recipes:Recipe];
    static belongsTo = [Recipe]
    static constraints = 
    {
        name blank:false;
    }

    String toString()
    {
        return name;
    }
}

That should be static searchable = true with a lowercase "s" - see http://grails.org/Searchable+Plugin+-+Mapping

But the Searchable plugin doesn't work with Mongo or other NoSQL datastores. This is because it is implemented using Hibernate events to listen for events corresponding to inserting, deleting, and updating database rows and updating the Lucene index based on those changes. Since there's no Hibernate in the mix, the Searchable isn't aware of any changes.

your .withCriteria search has not much to do with the searchable plugin - it's just a normal SQL search.

Try something like

def recipes = Recipe.withCriteria
{
    or {
        ilike('name', params.name)
        ingredients {
            ilike('name', params.name)
        }
    }
}

in order to search in Recipe and Ingredient names.

see http://grails.org/doc/2.0.x/guide/GORM.html#criteria for more help.

Searchable Plugin works with mongodb but you need to configure and reindex it manually as the default behavior is using hibernate.

  1. change your config.groovy mirrorchanges = false and bulkIndexOnStartup = false

  2. add mapwith attribute to all your domain classes that connects to mongodb. static mapWith="mongo" static searchable=true

  3. reindex manually by calling reindex() from bootstrap.groovy and everytime your domain has update events. def domainList = DomainName.list() DomainName.reindex(domainList)

this is a helpful link from which i referred to though I did not implement the rabbit mq part as I don't need it at the moment. Hope this helps. http://spring.io/blog/2011/08/29/rabbitmq-enabling-grails-full-text-search-on-cloud-foundry/

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