Hi everyone,
I created a search template using kibana as follows:
Post _scripts/mytemplateid3
{
"script": {
"lang": "mustache",
"source": {
"query": {
"match": {
"author": "{{query_string}}"
}
}
}
}
}
Then I ran a GET request like the following and it worked ok:
GET /spo-demo/_search/template
{
"id": "mytemplateid3",
"params": {
"query_string": "Annie"
}
}
And the output looks like this:
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 1,
"relation" : "eq"
},
"max_score" : 0.9808291,
"hits" : [
{
"_index" : "spo-demo",
"_type" : "_doc",
"_id" : "fTR_pHkB4DiV7smmisqK",
"_score" : 0.9808291,
"_source" : {
"displayurl" : "url1",
"author" : "Annie Martinez",
"totalUsers" : "2"
}
}
]
}
}
So far so good, then I decide to use the search template in my java code like this, but for some reason I am not getting any results:
SearchTemplateRequest request = new SearchTemplateRequest();
request.setRequest(new SearchRequest("spo-demo"));
request.setScriptType(ScriptType.STORED);
request.setScript("mytemplateid3");
Map<String, Object> params = new HashMap<>();
params.put("field", "query_string");
params.put("value", "Annie");
params.put("size", 5);
request.setScriptParams(params);
RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("localhost", 9200, "http")));;
SearchTemplateResponse response = client.searchTemplate(request, RequestOptions.DEFAULT);
SearchResponse searchResponse = response.getResponse();
SearchHits hits = searchResponse.getHits();
Any idea why it is not working in Java but it works in kibana?
Below my index mappings if anyone is interested:
{
"spo-demo" : {
"mappings" : {
"properties" : {
"author" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"displayurl" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
},
"totalUsers" : {
"type" : "text",
"fields" : {
"keyword" : {
"type" : "keyword",
"ignore_above" : 256
}
}
}
}
}
}
}