Score filtered results with a template

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?

Found a solution here:

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

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