首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Grails ElasticSearch插件-建议查询

Grails ElasticSearch插件-建议查询
EN

Stack Overflow用户
提问于 2014-09-15 02:26:28
回答 1查看 548关注 0票数 0

可以使用插件编写一个建议查询吗?在插件文档中没有任何关于这方面的内容。如果它是可能的,我该怎么做?

以下是关于建议查询的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

非常感谢mutch的回答。

EN

回答 1

Stack Overflow用户

发布于 2015-02-18 14:07:51

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

代码语言:javascript
复制
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映射。

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

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

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

代码语言:javascript
复制
runtime('org.codehaus.groovy.modules.http-builder:http-builder:0.5.1') {
    excludes 'xalan'
    excludes 'xml-apis'
    excludes 'groovy'
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25836579

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档