簡體   English   中英

具有條件的Grails可搜索插件

[英]Grails Searchable plugin with Criteria

我對可搜索插件有一些疑問:

我有兩個域:

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

我已經開發了自己的搜索頁面,其中包含兩個字段(fromCity,toCity)。 具有類似的東西:

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

因此,我想知道如何將自己的搜索方法提供給Criteria Field。

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

如果有人可以幫助我,我將非常感激。

首先,您需要在域類中定義一個可搜索的閉包。 例如

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

然后,您可以進行如下搜索。

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

-(firstName: ${myName})這將從搜索結果中刪除我的名字,類似地,您可以根據您的邏輯和/或其他字段。

默認運算符為“和”,您可以在其中修改運算符,請參見以下示例

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"

有關更多詳細信息,請參見文檔。 可搜索的插件文檔

讓我知道您是否需要任何幫助。

有關指南針的更多幫助,請參見第12.5.1節。 查詢字符串語法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM