【问题标题】:Grails ElasticSearch Plugin - Suggest QueryGrails ElasticSearch 插件 - 建议查询
【发布时间】:2014-11-08 07:16:01
【问题描述】:

是否可以使用插件编写建议查询? 插件文档中没有任何内容。 如果可能的话,我该怎么做?

这是有关建议查询的 elasticsearch 文档: http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters.html http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/search-suggesters-completion.html

非常感谢您的回答。

【问题讨论】:

  • 您可以直接发送 http 查询,取回 JSON 答案并将它们映射到域类。
  • 感谢@wwarlock!你能举一些我怎么做的例子吗?
  • 是的,我可以,但我需要一些时间,也许在最近的周末。
  • 真的很抱歉,还没来得及写这篇。但我并没有忘记它。
  • 我看到该插件目前支持建议。但是对elasticsearch执行http查询是不同的方法和解决方法,直到插件不支持它。 @SergioMendonça .. 你现在如何处理它?

标签: grails elasticsearch grails-plugin search-suggestion


【解决方案1】:

确实,您确实需要将查询直接发送到 Elastic Search。下面是我使用的代码:

import groovyx.net.http.ContentType
import groovyx.net.http.Method
import org.apache.commons.lang.StringUtils
import org.apache.commons.lang.math.NumberUtils
import groovyx.net.http.HTTPBuilder
...

def suggestion = params.query

def http = new HTTPBuilder('http://localhost:9200/_suggest')
http.request(Method.POST, ContentType.JSON) {
    body = [
            'suggestion': [
                    'text': params.query,
                    'term': ["field": "_all"]
            ]
    ]

    response.success = { resp, json ->
        json?.suggestion?.each { s ->
            def oldWord = s?.text
            def newWord = s?.options[0]?.text ?: oldWord
            suggestion = StringUtils.replace(suggestion, oldWord, newWord)

        }
    }

    response.failure = { resp ->
        flash.error = "Request failed with status ${resp.status}"
    }
}
searchResult.suggestedQuery = suggestion

请注意,这是摘录。此外,我正在执行实际搜索,然后将建议查询属性附加到 searchResult 映射。

对使用 Elastic Search 运行的 _suggest 服务执行 HTTP POST。在我的示例中,这是一个在单个服务器上运行的简单 Web 应用程序,因此 localhost 很好。请求的格式是基于 Elastic Search documentation 的 JSON 对象。

我们有两个响应处理程序 - 一个用于成功,另一个用于错误。我的成功处理程序会遍历给出建议的每个单词,并为每个单词选择最好的(第一个)建议(如果有的话)。如果想看原始数据,可以临时加println(json)。

最后一点 - 在将 httpBuilder 类添加到项目时,您可能需要排除一些已经提供的工件。即:

runtime('org.codehaus.groovy.modules.http-builder:http-builder:0.5.1') {
    excludes 'xalan'
    excludes 'xml-apis'
    excludes 'groovy'
}

【讨论】:

    猜你喜欢
    • 2018-09-17
    • 1970-01-01
    • 2017-10-25
    • 2016-12-24
    • 2016-09-11
    • 1970-01-01
    • 2016-06-28
    • 2016-10-30
    • 2017-12-24
    相关资源
    最近更新 更多