[英]Plain string template query for elasticsearch through java API?
I have a template foo.mustache saved in {{ES_HOME}}/config/scripts. 我有一个模板foo.mustache保存在{{ES_HOME}} / config / scripts中。
POST to http://localhost:9200/forward/_search/template with the following message body
returns a valid response: POST到http:// localhost:9200 / forward / _search / template并带有以下
message body
将返回有效响应:
{
"template": {
"file": "foo"
},
"params": {
"q": "a",
"hasfilters": false
}
}
I want to translate this to using the java API now that I've validated all the different components work. 既然我已经验证了所有不同的组件的工作能力,那么我想将其转换为使用Java API。 The documentation here describes how to do it in java:
此处的文档描述了如何在Java中执行此操作:
SearchResponse sr = client.prepareSearch("forward")
.setTemplateName("foo")
.setTemplateType(ScriptService.ScriptType.FILE)
.setTemplateParams(template_params)
.get();
However, I would instead like to just send a plain string query (ie the contents of the message body from above) rather than build up the response using the java. 但是,我只想发送一个简单的字符串查询(即,消息主体的内容从上方),而不是使用Java建立响应。 Is there a way to do this?
有没有办法做到这一点? I know with normal queries, I can construct it like so:
我知道通过普通查询,我可以像这样构造它:
SearchRequestBuilder response = client.prepareSearch("forward")
.setQuery("""JSON_QUERY_HERE""")
I believe the setQuery() method wraps the contents into a query object, which is not what I want for my template query. 我相信setQuery()方法会将内容包装到查询对象中,这不是我想要的模板查询对象。 If this is not possible, I will just have to go with the documented way and convert my json params to
Map<String, Object>
如果这不可能,那么我只需要按照记录的方式将json参数转换为
Map<String, Object>
I ended up just translating my template_params to a Map<String, Object>
as the documentation requires. 我最终只是按照文档要求将template_params转换为
Map<String, Object>
。 I utilized groovy's JsonSlurper to convert the text to an object with a pretty simple method. 我使用了groovy的JsonSlurper,通过一种非常简单的方法将文本转换为对象。
import groovy.json.JsonSlurper
public static Map<String,Object> convertJsonToTemplateParam(String s) {
Object result = new JsonSlurper().parseText(s);
//Manipulate your result if you need to do any additional work here.
//I.e. Programmatically determine value of hasfilters if filters != null
return (Map<String,Object>) result;
}
And you could pass in the following as a string to this method: 您可以将以下内容作为字符串传递给此方法:
{
"q": "a",
"hasfilters": true
"filters":[
{
"filter_name" : "foo.untouched",
"filters" : [ "FOO", "BAR"]
},
{
"filter_name" : "hello.untouched",
"list" : [ "WORLD"]
}
]
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.