在Grails应用程序中按查询和位置进行搜索



我在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)

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

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

因此,如果我通过查询"dinner"和位置"Brisbane"进行搜索,我想得到一组与"dinner"匹配的产品,其中子位置实例的"locationName"属性与"Brisbrne"匹配

关于从哪里开始使用可搜索的插件来实现这样的东西,有什么想法吗?

位置类别

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
    }
}

感谢您的帮助

我认为您想要做的是:

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

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

最新更新