# Document similarity problem

**URL:** https://discuss.elastic.co/t/document-similarity-problem/116429
**Category:** Elasticsearch
**Created:** [January 22, 2018, 6:09am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429 "2018-01-22T06:09:25Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![jiangshigen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jiangshigen](https://discuss.elastic.co/u/jiangshigen)
#### Post date: [January 22, 2018, 6:09am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429/1 "2018-01-22T06:09:25Z")

</div>

![image](https://us1.discourse-cdn.com/elastic/original/3X/8/5/852b42b002b17ade707711abee7615f8cc0f907d.png)I use this way to achieve some documents, howerer,the relevance scoring of each document is 1.  
template example:  
{  
"query": {  
"bool": {  
"must": [  
{  
"query\_string": {  
"default\_field": "progress\_note",  
"query": "{{queryString}}"  
}  
}  
],  
"must\_not": ,  
"should":   
}  
},  
"from": 0,  
"size": 10,  
"sort": ,  
"aggs": {}  
}

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 22, 2018, 7:39am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429/2 "2018-01-22T07:39:40Z")

</div>

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](https://discuss.elastic.co/t/about-the-elasticsearch-category/21). It will make your post more readable.

Or use markdown style like:

````
```
CODE
```
````

---

<div class="post-metadata">

### Author: ![dadoonet](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dadoonet/32/137187_2.png) [@dadoonet](https://discuss.elastic.co/u/dadoonet)
#### Post date: [January 22, 2018, 7:47am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429/3 "2018-01-22T07:47:27Z")

</div>

Could you provide a result output?

---

<div class="post-metadata">

### Author: ![jiangshigen](https://avatars.discourse-cdn.com/v4/letter/j/e9bcb4/32.png) [@jiangshigen](https://discuss.elastic.co/u/jiangshigen)
#### Post date: [January 24, 2018, 6:32am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429/4 "2018-01-24T06:32:07Z")

</div>

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?

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [February 21, 2018, 6:32am UTC](https://discuss.elastic.co/t/document-similarity-problem/116429/5 "2018-02-21T06:32:23Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
