elasticsearch,grails-plugin,search-suggestion,Grails,elasticsearch,Grails Plugin,Search Suggestion" /> elasticsearch,grails-plugin,search-suggestion,Grails,elasticsearch,Grails Plugin,Search Suggestion" />

Grails ElasticSearch插件-建议查询

Grails ElasticSearch插件-建议查询,grails,elasticsearch,grails-plugin,search-suggestion,Grails,elasticsearch,Grails Plugin,Search Suggestion,是否可以使用插件编写建议查询? 插件文档中没有这方面的内容。 如果可能的话,我该怎么做 以下是关于建议查询的elasticsearch文档: 非常感谢mutch的回答。事实上,您确实需要将查询直接发送到Elastic Search。下面是我使用的代码: import groovyx.net.http.ContentType import groovyx.net.http.Method import org.apache.commons.lang.StringUtils import org.a

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

以下是关于建议查询的elasticsearch文档:


非常感谢mutch的回答。

事实上,您确实需要将查询直接发送到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
请注意,这是一个摘录。此外,我正在执行实际的搜索,然后将suggestedQuery属性附加到searchResult映射

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

我们有两个响应处理程序——一个用于成功,另一个用于错误。我的成功处理程序会对每个单词进行迭代,并为每个单词选择最佳的第一个建议(如果有)。如果要查看原始数据,可以临时添加printlnjson

最后一个注意事项-在向项目中添加httpBuilder类时,可能需要排除一些已经提供的工件。即:

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

您可以发送直接http查询,返回JSON答案并将其映射到域类。谢谢@wwarlock!你能举一些我该怎么做的例子吗?是的,我可以,但我需要一些时间,也许在最近的周末。我真的很抱歉,仍然没有时间写这个。但我并没有忘记,我已经看到这个插件目前支持这个建议。但对elasticsearch执行http查询是另一种方法,也是一种变通方法,直到插件不支持它为止@塞尔吉奥门多萨。。你现在是怎么处理的?