简体   繁体   English

Grails ElasticSearch插件-建议查询

[英]Grails ElasticSearch Plugin - Suggest Query

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? 如果有可能,我该怎么办?

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 以下是有关建议查询的elasticsearch文档: http : //www.elasticsearch.org/guide/zh-CN/elasticsearch/reference/current/search-suggesters.html http://www.elasticsearch.org/guide/zh-CN/elasticsearch/reference /current/search-suggesters-completion.html

Thanks very mutch for the answer. 非常感谢您的回答。

Indeed, you do need to send the query directly to Elastic Search. 实际上,您确实需要将查询直接发送到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

Note, that this is an excerpt. 请注意,这是摘录。 Additionally, I am performing the actual search, then appending the suggestedQuery attribute to the searchResult map. 另外,我正在执行实际的搜索,然后将suggestedQuery属性附加到searchResult映射。

Perform an HTTP POST to the _suggest service running with Elastic Search. 对通过Elastic Search运行的_suggest服务执行HTTP POST。 In my example, this was a simple web application running on a single server, so localhost was fine. 在我的示例中,这是一个在单个服务器上运行的简单Web应用程序,因此localhost很好。 The format of the request is a JSON object based off of the Elastic Search documentation . 请求的格式是基于Elastic Search 文档的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) . 如果要查看原始数据,可以临时添加println(json)

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. 最后一点-将httpBuilder类添加到项目中时,您可能需要排除已经提供的一些工件。 Namely: 即:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM