Grails ElasticSearch插件-建议查询 [英] Grails ElasticSearch Plugin - Suggest Query

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

问题描述

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

Is it possible to write a suggest query using the plugin? There's nothing about that in the plugin documentation. If it's possoble, how do I do that?

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

Here's the elasticsearch docs about suggest querys: 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

非常感谢您的回答.

推荐答案

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

Indeed, you do need to send the query directly to Elastic Search. Below is the code I used:

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映射中.

Note, that this is an excerpt. Additionally, I am performing the actual search, then appending the suggestedQuery attribute to the searchResult map.

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

Perform an HTTP POST to the _suggest service running with Elastic Search. In my example, this was a simple web application running on a single server, so localhost was fine. The format of the request is a JSON object based off of the Elastic Search documentation.

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

We have two response handlers - one for success, another for errors. My success handler iterates over each word that a suggestion was given for and picks the best (first) suggestion for each one, if there is one. If you want to see the raw data, you can temporarily add in println(json).

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

One last note - when adding the httpBuilder classes to the project you are likely to need to exclude a few artifacts that are already provided. Namely:

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

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

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