# Score filtered results with a template

**URL:** https://discuss.elastic.co/t/score-filtered-results-with-a-template/79461
**Category:** Elasticsearch
**Created:** [March 21, 2017, 3:59pm UTC](https://discuss.elastic.co/t/score-filtered-results-with-a-template/79461 "2017-03-21T15:59:15Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![Wullschi](https://avatars.discourse-cdn.com/v4/letter/w/9dc877/32.png) [@Wullschi](https://discuss.elastic.co/u/Wullschi)
#### Post date: [March 21, 2017, 3:59pm UTC](https://discuss.elastic.co/t/score-filtered-results-with-a-template/79461/1 "2017-03-21T15:59:15Z")

</div>

Hi,

I'm new to elasticsearch and see myself confronted with a problem that I can't solve on my own.

I have a simple template, that I index on my server. This template is supposed to retrieve documents with specific tags.  
Every tag has a scoring value, that I determined in an earlier query. I now want to score all the retrieved documents depending on the score of their tags with the following logic:

```
"template":{
    "query": {
      "function_score": {
        "query": {
          "constant_score": {
            "filter": {
              "terms": {
                "tags": [1,2]
              }
            }
          }
        },
        "functions": 
        [ 
          {
            "filter": { "term": { "tags": 1 }}, 
            "weight": 0.7
          },
          {
            "{filter": { "term": { "tags": 2 }}, 
            "weight": 1.2
          }
        ],
        "score_mode": "sum"
      }
    }
  }

```

I would like to inject the "functions" part to the template dynamically because I do not know the number of tags and their weight when indexing the template. I tried something like this, which does not work:

```
POST /_search/template/mytemplate
{
  "template":{
    "query": {
      "function_score": {
        "query": {
          "constant_score": {
            "filter": {
              "terms": {
                "tags": [1,2]
              }
            }
          }
        },
        "functions": "{{my_parameter}}",
        "score_mode": "sum"
      }
    }
  }
}

POST /index/type/_search/template
{
   "id": "mytemplate",
   "parameter": {
      "my_parameter": "[{\"filter\": { \"term\": { \"tags\": 1 }},\"weight\": 0.7},{\"{filter\": { \"term\": { \"tags\": 2 }}, \"weight\": 1.2}]"
   }    
}

```

Is there any simple solution to that, or do I have to do the scoring with a script?

---

<div class="post-metadata">

### Author: ![Wullschi](https://avatars.discourse-cdn.com/v4/letter/w/9dc877/32.png) [@Wullschi](https://discuss.elastic.co/u/Wullschi)
#### Post date: [March 22, 2017, 9:24am UTC](https://discuss.elastic.co/t/score-filtered-results-with-a-template/79461/2 "2017-03-22T09:24:12Z")

</div>

Found a solution here:

> <https://github.com/elastic/elasticsearch/issues/21648>

I wasn't aware that this can only be done with an inline query.

---

<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: [April 19, 2017, 9:24am UTC](https://discuss.elastic.co/t/score-filtered-results-with-a-template/79461/3 "2017-04-19T09:24:26Z")

</div>

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