# Elastic search java client unable to handle scores more than 1

**URL:** https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987
**Category:** Elasticsearch
**Created:** [November 4, 2016, 6:45am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987 "2016-11-04T06:45:29Z")
**Posts on this page:** 17
**Page:** 1

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 6:45am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/1 "2016-11-04T06:45:29Z")

</div>

I am using elastic search java client with elastic search version 2.3 In one of my queries, the search score come out in the order of 4-5. But elastic search java client is converting all the score above 1 to 1 and therefore I am not getting correct results. What can be done in this case to get the correct result? I don't need the scores but I need to sort the search result in the order of score which is not possible now.

---

<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: [November 4, 2016, 7:17am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/2 "2016-11-04T07:17:56Z")

</div>

Any chance you can share some code?

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 7:27am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/3 "2016-11-04T07:27:34Z")

</div>

When I am doing using curl command, I am getting proper results  
{  
"took" : 105,  
"timed\_out" : false,  
"\_shards" : {  
"total" : 5,  
"successful" : 5,  
"failed" : 0  
},  
"hits" : {  
"total" : 8212,  
"max\_score" : 5.198194,  
....

But when I use Java client, using the following code. I am getting max score as 1 and the result are not properly sorted.  
`searchResponse = client.prepareSearch() .setIndices((String) entitySearchParams.get(INDEX_NAME)) .setTypes(entityTypesArray) .setQuery(queryBuilder) .setFrom(0) .setSize(10) .setFetchSource(include, exclude) .get();`

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 7:34am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/4 "2016-11-04T07:34:25Z")

</div>

I am using ngram analyzer and I have around 30,000 documents.

```
   "analyzer": {
                "twofifteengram": {
                  "type": "custom",
                  "tokenizer": "standard",
                  "filter": [
                    "lowercase",
                    "twofifteengram_filter"
                  ]
                }
              }
            }
          }
```

---

<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: [November 4, 2016, 7:58am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/5 "2016-11-04T07:58:48Z")

</div>

Please format your code using `</>` icon. It will make your post more readable.

What do you have in `queryBuilder` and `filter`?  
What is the exact curl command you are sending?

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 8:11am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/6 "2016-11-04T08:11:15Z")

</div>

```
 QueryBuilder queryBuilder = new TemplateQueryBuilder(templateInstance);

```

I am using a mustache template

```
Template templateInstance = new Template(
                searchQueryParamInstance.getTemplateName(),
                ScriptService.ScriptType.INDEXED,
                MustacheScriptEngineService.NAME,
                null,
                searchQueryParamInstance.getParams()
        );

```

The curl command I am using is  
`curl -XGET 'localhost:9200/indexname/typename/_search/template?pretty' -d@temp.json`  
where temp.json contains the following json:

```
 {
    "template": {
        "id": "templateName" 
    },
    "params": {
        "param1": "attribute.ngram",
        "value1":"firstword secondword thirdword "
    }
} 

```

Same params and template name is passed in java client as in the curl command

Filter can be removed.

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 8:13am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/7 "2016-11-04T08:13:04Z")

</div>

The mustache template is  
`"{ \"query\" : { \"bool\" : { \"should\" : [ {{#param1}} {{#value1}} { \"match\" : { \"{{param1}}\" : \"{{value1}}\" } } {{/value1}} {{/param1}} {{#param2}} {{#value2}} , { \"match\" : { \"{{param2}}\" : \"{{value2}}\" } } {{/value2}} {{/param2}}` ...

The same is continued for 10 params and values

---

<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: [November 4, 2016, 8:42am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/8 "2016-11-04T08:42:51Z")

</div>

Can you print the queryBuilder.toString() or string() (don't recall from the top of my head)?

---

<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: [November 4, 2016, 8:44am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/9 "2016-11-04T08:44:23Z")

</div>

Even better. Don't call get() on the prepareSearch but print its content with toString().

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 8:56am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/10 "2016-11-04T08:56:25Z")

</div>

Query builder

```
  {
          "template" : {
            "id" : "templateName",
            "lang" : "mustache",
            "params" : {
              "param1" : "attributesInfo.en-US.xyz.ngram",
              "value1" : "Random company name CO., LTD OF"
            }
          }
        }
```

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 8:57am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/11 "2016-11-04T08:57:34Z")

</div>

prepareSearch:

```
{
  "from" : 0,
  "size" : 5,
  "query" : {
    "template" : {
      "id" : "templateName",
      "lang" : "mustache",
      "params" : {
        "param1" : "attributesInfo.en-US.xyz.ngram",
        "value1" : "Random company name CO., LTD OF"
      }
    }
  },
  "_source" : {
    "includes" : [],
    "excludes" : []
  }
}
```

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 4, 2016, 9:03am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/12 "2016-11-04T09:03:47Z")

</div>

Response from curl command

```
{
"took" : 105,
"timed_out" : false,
"_shards" : {
"total" : 5,
"successful" : 5,
"failed" : 0
},
"hits" : {
"total" : 8212,
"max_score" : 5.198194,
....

```

Response that I am getting from java client:

```
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 8212,
    "max_score" : 1.0,
    "hits" : [ {

```

...

As we can see total number of results is same, just the max\_score is 1 in later case. Therefore when we are setting the size as 5 , we are not getting expected result

---

<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: [November 5, 2016, 7:38am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/13 "2016-11-05T07:38:19Z")

</div>

Can you run again this?

```auto
curl localhost:9200/_search -d '{
  "from" : 0,
  "size" : 5,
  "query" : {
    "template" : {
      "id" : "templateName",
      "lang" : "mustache",
      "params" : {
        "param1" : "attributesInfo.en-US.xyz.ngram",
        "value1" : "Random company name CO., LTD OF"
      }
    }
  },
  "_source" : {
    "includes" : [],
    "excludes" : []
  }
}'

```

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 7, 2016, 4:27am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/14 "2016-11-07T04:27:12Z")

</div>

If I am giving this request, I got the match score as 1 just like the java client

```
{
  "took" : 11,
  "timed_out" : false,
  "_shards" : {
    "total" : 5,
    "successful" : 5,
    "failed" : 0
  },
  "hits" : {
    "total" : 8212,
    "max_score" : 1.0,
    "hits" : [ {
```

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 7, 2016, 11:08am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/17 "2016-11-07T11:08:06Z")

</div>

I don't understand why the following 2 commands give different results

```
curl localhost:9200/indexname/typename/_search -d '{
  "from" : 0,
  "size" : 5,
  "query" : {
    "template" : {
      "id" : "templateName",
      "lang" : "mustache",
      "params" : {
        "param1" : "attributesInfo.en-US.xyz.ngram",
        "value1" : "Random company name CO., LTD OF"
      }
    }
  },
  "_source" : {
    "includes" : [],
    "excludes" : []
  }
}'

```

and

```
curl -XGET 'localhost:9200/indexname/typename/_search/template?pretty' -d '
     {
        "template": {
            "id": "templateName" 
        },
        "params": {
         "param1" : "attributesInfo.en-US.xyz.ngram",
            "value1" : "Random company name CO., LTD OF"
        }
    }'
```

---

<div class="post-metadata">

### Author: ![dbanka](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/dbanka/32/8761_2.png) [@dbanka](https://discuss.elastic.co/u/dbanka)
#### Post date: [November 7, 2016, 11:12am UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/18 "2016-11-07T11:12:42Z")

</div>

If the method used by me using templateQuerybuilder won't work then what is the best way to use search templates via java client?

I found a method using setTemplate() method but if I use that method , setSize isn't working

```
SearchResponse searchResponse =client.prepareSearch()
                .setIndices("new")
                .setTypes("sku")
                .setTemplate(templateInstance)
                .get();
```

---

<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: [July 5, 2017, 10:06pm UTC](https://discuss.elastic.co/t/elastic-search-java-client-unable-to-handle-scores-more-than-1/64987/19 "2017-07-05T22:06:14Z")

</div>


