【问题标题】:Searching by query and location in Grails app在 Grails 应用程序中按查询和位置搜索
【发布时间】:2012-07-09 12:11:08
【问题描述】:

我在我的 grails 应用程序中使用可搜索插件通过以下方式搜索“Offering”实例。

在提供类中使用以下映射进行搜索设置

class Offering {

    static searchable = {
        only: ['title', 'description']
    }

    String title
    String description

    Category category
    Location location

    BigDecimal price
    PricePeriod pricePeriod

    static belongsTo = [user: SecUser]

    static constraints = {
        title(blank: false, maxSize: 100)
        description(blank: false, maxSize: 10240)
        category(nullable: false)
        location(nullable: false)
        price(nullable: true, scale: 2)
        pricePeriod(nullable: true)
    }

    public String toString() {
        return title
    }

}

调用可搜索服务

 def search = {
    must(queryString(params.query))
 }

 def searchResult = searchableService.search(search, params)

正如预期的那样,这会从产品实例的映射属性中返回适当的命中。供品合集

我现在想做的不仅是通过搜索查询,而且还通过位置的产品子元素进行搜索。特别是子 Location 实例中的“locationName”字段。

因此,如果我通过查询“晚餐”和位置“布里斯班”进行搜索,我希望获得与“晚餐”匹配的产品集合,其中子位置实例的“locationName”属性与“布里斯班”匹配

关于使用可搜索插件从哪里开始实现类似功能的任何想法?

位置类

class Location {

    String locationName
    Location locationParent
    String postcode

    static hasMany = [locations: Location]

    static constraints = {
        locationName(blank: false, unique: true)
        locationParent(nullable: true)
        postcode(nullable: true)
    }

    public String toString() {
        return locationName
    }
}

感谢您的帮助

【问题讨论】:

    标签: search grails grails-searchable


    【解决方案1】:

    我认为你想做的事情是这样的:

    class Offering {
    
       ...
    
       static searchable = {
           only: ['title', 'description']
           location component: true
       }
    
       ...
    }
    

    class Location {
    
        ...
    
        static searchable = {
           only: ['locationName']
           location component: true
        }
    
        ...
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多