可以使用插件编写一个建议查询吗?在插件文档中没有任何关于这方面的内容。如果它是可能的,我该怎么做?
以下是关于建议查询的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的回答。
发布于 2015-02-18 14:07:51
实际上,您确实需要将查询直接发送到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映射。
对使用Elastic Search运行的_suggest服务执行HTTP POST。在我的示例中,这是一个在单个服务器上运行的简单web应用程序,因此localhost就可以了。请求的格式是基于弹性搜索documentation的JSON对象。
我们有两个响应处理程序-一个用于成功,另一个用于错误。我的成功处理程序遍历给出建议的每个单词,并为每个单词选择最好的(第一个)建议。如果您想查看原始数据,可以临时添加println(json)。
最后一个注意事项--在向项目中添加httpBuilder类时,您可能需要排除一些已经提供的工件。即:
runtime('org.codehaus.groovy.modules.http-builder:http-builder:0.5.1') {
excludes 'xalan'
excludes 'xml-apis'
excludes 'groovy'
}https://stackoverflow.com/questions/25836579
复制相似问题