I do not understand how to use searchTemplates in the Java API of Elasticsearch correctly. My template seems to work fine when I test it in sense. But when I use the template in Java code, it gives different results.
Here is what I do
DELETE /megacorp
PUT /megacorp/employee/1
{
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests": [ "sports", "music" ]
}
GET /megacorp/_search
{
"query": {"match": {
"about": "rock"
}}
}
This returns:
{
"took": 9,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.11506981,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 0.11506981,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
So that looks nice: a score of 0.115. Now I create a searchTemplate
DELETE /_search/template/megacorpTemplate
POST /_search/template/megacorpTemplate
{
"template": {
"query": {
"match": {
"about": "{{txt}}"
}
}
}
}
And use it:
GET /megacorp/_search/template
{
"id": "megacorpTemplate",
"params": {
"txt": "rock"
}
}
It returns:
{
"took": 35,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.11506981,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "1",
"_score": 0.11506981,
"_source": {
"first_name": "John",
"last_name": "Smith",
"age": 25,
"about": "I love to go rock climbing",
"interests": [
"sports",
"music"
]
}
}
]
}
}
So, all good and well. But now comes the problem. When I want to use this searchTemplate in my Java code, I seem to lose some information, for example the score is 1.0 and my script fields are lost (removed for brevity in this sample). Here is my code:
@Test
public void quickTest2() {
Client client;
try {
client = TransportClient.builder().build().addTransportAddress(
new InetSocketTransportAddress(InetAddress.getByName("localhost"), 9300));
Map<String,Object> templateParams = new HashMap<>();
templateParams.put("txt", "rock");
QueryBuilder tqb = QueryBuilders.templateQuery(
"megacorpTemplate",
ScriptService.ScriptType.INDEXED,
templateParams);
SearchResponse searchResponse = client.prepareSearch("megacorp")
.setQuery(tqb)
.execute()
.actionGet();
System.out.println(searchResponse.toString());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
}
It returns:
{
"took" : 7,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 1,
"max_score" : 1.0,
"hits" : [ {
"_index" : "megacorp",
"_type" : "employee",
"_id" : "1",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"age" : 25,
"about" : "I love to go rock climbing",
"interests" : [ "sports", "music" ]
}
} ]
}
}
So why is my score 1.0 now instead of 0.115?
.setExplain(true)to yourprepareSearch, what does it say? I'm guess that it is doing a constantscore query. I also think you want to 'PUT' to ' /_search/template/megacorpTemplate' instead of 'POST'