grails搜索插件查询 [英] grails searchable plugin query

查看:138
本文介绍了grails搜索插件查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的Grails应用程序使用可搜索的插件,它基于Compass和Lucene构建,提供搜索功能。我有两个可搜索的类,比如Author和Book。我已将这些类映射到搜索索引,以便只能搜索某些字段。



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

  def results = searchableService.search(query)

同时在两个类中进行搜索的好处之一是,结果对象包括关于包含结果数量的元数据,可用结果数量,分页细节等。



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

  def bookResults = Book.search(query +approved:1)
def authorResults = Author.search(query)

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



有没有一种方法可以通过单个查询在Book和Author之间进行搜索,但只返回已批准的图书?

解决方案

我创建了一个测试应用程序,并提供了以下解决方案。如果属性批准只有状态 0

/ code>和 1 ,以下查询将起作用:

 <$ c $ ($ {query} -approved:0 -approved:1)
($ {query} AND approved:1)

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



顺便说一句:如果您添加一种方法,如

  String getType(){Book} 

  String getType(){Author} 

您的域名,甚至可以配置您的搜索做到这一点

  def results = searchableService.search(
($ {query} AND approved:1)OR($ {query} AND type:Author)


My Grails app is using the searchable plugin, which builds on Compass and Lucene to provide search functionality. I have two searchable classes, say Author and Book. I have mapped these classes to the search index, so that only certain fields can be searched.

To perform a search across both classes I simply call

def results = searchableService.search(query)

One of the nice features of doing the search across both class simultaneously, is that the results object includes metadata about number of results included, number of results available, pagination details etc.

I recently added a boolean approved flag to the Book class and I never want unapproved books to appear in the search results. One option is to replace the call above with:

def bookResults = Book.search(query + " approved:1")
def authorResults = Author.search(query)

However, I now need to figure out how to combine the metadata for both results, which is likely to be tricky (particularly pagination).

Is there a way to search across Book and Author with a single query, but only return approved books?

解决方案

I've created a test app and came to the following solution. maybe it helps...

if the property approved only has the states 0 and 1, the following query will work:

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

I guess this can be reformulated in a better way if you don't use the QueryParser but the BooleanQueryBuilder.

BTW: if you add a method like

String getType() { "Book" }

and

String getType() { "Author" }

To your domains, you can even configure your search to do it like this

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

这篇关于grails搜索插件查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆