grails searchable插件查询

Dón*_*nal 6 lucene grails groovy searchable compass-lucene

我的Grails应用程序使用可搜索的插件,该插件基于Compass和Lucene构建,以提供搜索功能.我有两个可搜索的课程,比如作者和书.我已将这些类映射到搜索索引,因此只能搜索某些字段.

要在两个类中执行搜索,我只需调用

def results = searchableService.search(query)
Run Code Online (Sandbox Code Playgroud)

同时在两个类中进行搜索的一个很好的功能是,该results对象包括有关包含的结果数,可用结果数,分页详细信息等的元数据.

我最近approved在Book类中添加了一个布尔标志,我从不希望未经批准的书籍出现在搜索结果中.一种选择是将上面的调用替换为:

def bookResults = Book.search(query + " approved:1")
def authorResults = Author.search(query)
Run Code Online (Sandbox Code Playgroud)

但是,我现在需要弄清楚如何结合两个结果的元数据,这可能是棘手的(特别是分页).

有没有办法通过一个查询搜索Book和Author,但只返回已批准的书籍?

rdm*_*ler 6

您是希望能够找到作者还是想要找到给定作者的书籍?

如果要查找具有给定作者的书籍,可以按以下方式配置域类:

class Author {
    String name
    ...

    static searchable = {
        root false
    }    
}
Run Code Online (Sandbox Code Playgroud)

这将导致从searchableService.search(query)-result 中排除作者,您将$/Book/Author/name在索引中找到字段名称.(使用luke检查索引:http://code.google.com/p/luke/).

您可以通过prefix在Book-class中配置更好的内容来更改这些字段的名称:

class Book {
    String name
    Author author
    ...

    static searchable = {
        author component: [prefix: 'author']
    }    
}
Run Code Online (Sandbox Code Playgroud)

这会将索引中字段的名称更改为bookauthor.

如果您现在搜索searchableService.search(query),您将找到书籍名称或作者姓名包含搜索词的所有书籍.您甚至可以使用authorname:xyz语法将搜索限制为给定作者.


如果你真的想混合搜索结果,我只知道你已经提到的解决方案:将两个结果与你自己的代码混合,但我想很难将命中的得分以一种好的方式混合.

更新回复:这是我的分页代码......

.gsp:

<div class="pagination">
  <g:paginate total="${resultsTotal}" params="[q: params.q]"/>
</div>
Run Code Online (Sandbox Code Playgroud)

控制器:

result = searchableService.search(params.q, params)
[
  resultList: result.results, 
  resultsTotal: result.total
]
Run Code Online (Sandbox Code Playgroud)

因此,如果您只是合并两个搜索的结果并添加result.totals,这可能对您有用.


rdm*_*ler 4

我创建了一个测试应用程序并得出以下解决方案。也许这有帮助...

如果属性approved只有状态01,则以下查询将起作用:

def results = searchableService.search(
    "(${query} AND approved:1) OR (${query} -approved:0 -approved:1)"
)
Run Code Online (Sandbox Code Playgroud)

我想如果您不使用 QueryParser 而使用 BooleanQueryBuilder,则可以以更好的方式重新表述。

顺便说一句:如果你添加一个像

String getType() { "Book" }
Run Code Online (Sandbox Code Playgroud)

String getType() { "Author" }
Run Code Online (Sandbox Code Playgroud)

对于您的域,您甚至可以将搜索配置为这样

def results = searchableService.search(
    "(${query} AND approved:1) OR (${query} AND type:Author)"
)
Run Code Online (Sandbox Code Playgroud)