template example:
{
"query": {
"bool": {
"must": [
{
"query_string": {
"default_field": "progress_note",
"query": "{{queryString}}"
}
}
],
"must_not": ,
"should":
}
},
"from": 0,
"size": 10,
"sort": ,
"aggs": {}
}
Please don't post images of text as they are hardly readable and not searchable.
Instead paste the text and format it with </>
icon. Check the preview window.
Please format your code using </>
icon as explained in this guide. It will make your post more readable.
Or use markdown style like:
```
CODE
```
Could you provide a result output?
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",
"sex":"male","age":25,
"about":"I love to go rock climbing"
}
GET /megacorp/_search
{
"query":{
"bool":{
"must":[
{"term":{"sex":"male"}}
]
}
}
}
This returns:
{
"took": 5,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"failed": 0
},
"hits": {
"total": 1,
"max_score": 0.30685282,
"hits": [
{
"_index": "megacorp",
"_type": "employee",
"_id": "AWEnfZtfSGB1cPZqeBGA",
"_score": 0.30685282,
"_source": {
"first_name": "John",
"last_name": "Smith",
"sex": "male",
"age": 25,
"about": "I love to go rock climbing"
}
}
]
}
}
So that looks nice: a score of 0.30685282. But now comes the problem. When I want to use this searchTemplate in my Java code, the score is 1.0 .
Here is my code:
public static void main(String[] args) {
RetrievalPatients rp = new RetrievalPatients();
Map<String, Object> template_params = new HashMap<>();
template_params.put("sex", "male");
Client client = rp.getEsClient();
SearchResponse response = client.prepareSearch("megacorp").setTypes("employee")
.setSearchType(SearchType.QUERY_THEN_FETCH)
.setQuery(QueryBuilders.templateQuery("template_gender",ScriptService.ScriptType.FILE,template_params)).get();
System.out.println(response.toString());
client.close();
}
template_gender template content
{
"query":{
"bool":{
"must":[
{"term":{"sex":"{{sex}}"}}
]
}
}
}
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" : "AWEnfZtfSGB1cPZqeBGA",
"_score" : 1.0,
"_source" : {
"first_name" : "John",
"last_name" : "Smith",
"sex" : "male",
"age" : 25,
"about" : "I love to go rock climbing"
}
} ]
}
}
So why is my score 1.0 now instead of 0.30685282?
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.