简体   繁体   English

Grails可搜索

[英]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. 我使用grails 2.0.1并使用mongoDB GORM进行持久化,这工作正常,并且在我的模型中使用static Searchable = true进行搜索。

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 来自PHP和MySQL,这就像使用连接或其他东西修改查询一样简单

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 这应该是static searchable = true的小写“s” - 请参阅http://grails.org/Searchable+Plugin+-+Mapping

But the Searchable plugin doesn't work with Mongo or other NoSQL datastores. 但是Searchable插件不适用于Mongo或其他NoSQL数据存储区。 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. 这是因为它是使用Hibernate事件实现的,以侦听与插入,删除和更新数据库行相对应的事件,并根据这些更改更新Lucene索引。 Since there's no Hibernate in the mix, the Searchable isn't aware of any changes. 由于混合中没有Hibernate,因此Searchable不会发现任何变化。

your .withCriteria search has not much to do with the searchable plugin - it's just a normal SQL search. 你的.withCriteria搜索与可搜索的插件没什么关系 - 它只是一个普通的SQL搜索。

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. 请参阅http://grails.org/doc/2.0.x/guide/GORM.html#criteria以获取更多帮助。

Searchable Plugin works with mongodb but you need to configure and reindex it manually as the default behavior is using hibernate. Searchable Plugin与mongodb一起使用,但你需要手动配置和重新索引它,因为默认行为是使用hibernate。

  1. change your config.groovy mirrorchanges = false and bulkIndexOnStartup = false 更改您的config.groovy mirrorchanges = false和bulkIndexOnStartup = false

  2. add mapwith attribute to all your domain classes that connects to mongodb. 将mapwith属性添加到连接到mongodb的所有域类。 static mapWith="mongo" static searchable=true static mapWith =“mongo”static searchable = true

  3. reindex manually by calling reindex() from bootstrap.groovy and everytime your domain has update events. 通过从bootstrap.groovy调用reindex()以及每次域都有更新事件时手动重新索引。 def domainList = DomainName.list() DomainName.reindex(domainList) 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. 这是一个有用的链接,我提到虽然我没有实现兔子mq部分,因为我现在不需要它。 Hope this helps. 希望这可以帮助。 http://spring.io/blog/2011/08/29/rabbitmq-enabling-grails-full-text-search-on-cloud-foundry/ http://spring.io/blog/2011/08/29/rabbitmq-enabling-grails-full-text-search-on-cloud-foundry/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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