# Using the Ranking API with Search Templates

**URL:** <https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072>\
**Category:** Elasticsearch\
**Created:** [January 7, 2020, 2:28pm UTC](https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072 "2020-01-07T14:28:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)\
**Post date:** [January 7, 2020, 2:28pm UTC](https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072/1 "2020-01-07T14:28:00Z")

</div>

Hi folks, hope this is the right place to ask:

For our search use case, we're developing search templates (with mustache, including conditional clauses). In production, our users have identified some problematic cases we're taking into account for further development. To test the quality of the search results, we've so far relied on manual testing. However, since our user base is about to expand and the number of critical cases might as well, we'd like to adopt a somewhat more automated approach, e.g. with the ranking API. Is there a way to call a search template from within a rank\_eval all? Otherwise it seems that we'd have to always update all the queries put toward ranking instead of just the template, which obviously is less convenient.

---

<div class="post-metadata">

**Author:** ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)\
**Post date:** [January 7, 2020, 2:32pm UTC](https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072/2 "2020-01-07T14:32:47Z")

</div>

OK just after asking I found that it is possible to use templates as described [here](https://www.elastic.co/guide/en/elasticsearch/reference/7.5/search-rank-eval.html#_template_based_ranking_evaluation), but the question of using defined search templates is still open.

Or, alternatively, can templates written in mustache be used? Along the lines of

```
GET foo_bar/_rank_eval
{
  [...]
  "templates": [
    {
      "id": "mustache_test",
      "template": {
        "script": {
          "lang": "mustache",
          "source": """
            "query": {
              "match": {
                "foo": {
                  "query": {{#bar}}
                }
              }
            }
          """
        }
      }
    }
  ]
  [...]
```

---

<div class="post-metadata">

**Author:** ![styks90](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/styks90/32/59205_2.png) [@styks90](https://discuss.elastic.co/u/styks90)\
**Post date:** [January 7, 2020, 4:34pm UTC](https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072/3 "2020-01-07T16:34:30Z")

</div>

For future reference, we found a syntax that apparently works:

```
GET foo_bar/_rank_eval
{
  "templates": [
    {
      "id": "mustache_test",
      "template": {
        "lang": "mustache",
        "source": """
        {
          "query": {
            {{#bar}}
            "match": {
              "foo": {
                "query": "{{bar}}"
              }
            }
            {{/bar}}
          }
        }
        """
      }
    }
  ],
  "requests": [...],
  "metric": {...}
  }
}
```

---

<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 4, 2020, 4:42pm UTC](https://discuss.elastic.co/t/using-the-ranking-api-with-search-templates/214072/4 "2020-02-04T16:42:40Z")

</div>

Closed on February 4, 2020, 4:42pm UTC

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