# Sorting results of a search template with variable value of sorting field

**URL:** <https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078>\
**Category:** Elasticsearch\
**Created:** [April 28, 2023, 5:22pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078 "2023-04-28T17:22:14Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [April 28, 2023, 5:22pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/1 "2023-04-28T17:22:14Z")

</div>

Good day,  
I'm trying to make a search template with variable sorting parameter. I have a search template without variable sorting, it looks like this and it works:

Field1 - the field that i'm sorting with.

````auto
```PUT _scripts/name_script`

`{`

`"script"` `: {`

`"lang"` `: ` `"mustache"` `,`

`"source"` `: {`

`"sort"` `: {`

` ` `"field1"` `: [{`

` ` `"order"` `: ` `"desc"`

` ` `}`

` ` `]`

`},`

`"query"` `: {`

`"bool"` `: {`

`"must"` `: [`

`{`

`"match"` `: {`

`"field2"` `: ` `"{{query_string}}"`

`}`

`},`

`{`

`"match"` `: {`

`"field3"` `: ` `"true"`

`}`

`},`

`{`

`"match"` `: {`

`"field4"` `: ` `"true"`

`}`

`},`

`{`

`"exists"` `: {`

`"field"` `: ` `"field1"`

`}`

`}`

`]`

`}`

`}`

`},`

`"from"` `: ` `"{{from}}{{^from}}0{{/from}}"` `,`

`"size"` `: ` `"{{size}}{{^size}}10{{/size}}"`

`},`

`"params"` `: {`

`"query_string"` `: ` `"Query string"`

`}`

`}``

````

```auto

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [April 28, 2023, 5:30pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/2 "2023-04-28T17:30:55Z")

</div>

I've tried to make sorting field variable with Json but id doesn't work. Here it is:

```auto
PUT _scripts/shares_adfrontGroup_1
{
"script": {
"lang": "mustache",
"source":
"{\"query\":{\"bool\":{\"must\":[{\"match\":{\"field2\": {{#toJson}}field2{{/toJson}}}}, {\"match\":{\"field3\": {{#toJson}}field3{{/toJson}}}}, {\"match\":{\"field4\": {{#toJson}}field4{{/toJson}}}}, {\"exists\": {\"field\": {{#toJson}}field{{/toJson}}}}}]}},\"sort\": {{#toJson}}sort{{/toJson}},\"from\": {{from}},\"size\": {{size}}}"
}
}

```

and here is a query to ask the search template:

```auto
GET instruments/_search/template
{
"id": "shares_adfrontGroup_1",
"params": {
"field2": ["value2"],
"field3": ["value3"],
"field4": ["value4"],
"field": ["field1"],
"from": 0,
"size": 100,
"sort": [
{
"field1": {
"order": "desc"
}
}
]
}
}

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [April 28, 2023, 5:32pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/3 "2023-04-28T17:32:43Z")

</div>

So I need to have field1, which I use to sort search results, to be variable. How can I make it?

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [April 30, 2023, 11:15am UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/4 "2023-04-30T11:15:22Z")

</div>

Hi @orlenkoda5

This example you can use sort by parameter.

```auto
GET _render/template
{
  "source": """{"sort": {{#toJson}}sort{{/toJson}},"from":"{{from}}","size":"{{size}}","query":{"bool":{"must":{{#toJson}}clauses{{/toJson}}}}}""",
  "params": {
    "from": 0,
    "size": 100,
    "sort": {
      "field1": {
        "order": "desc"
      }
    },
    "clauses": [
      {
        "match":{
          "field2":""
        }
      },
      {
        "match":{
          "field3":""
        }
      },
      {
        "match":{
          "field4":""
        }
      },
      {
        "exists":{
          "field":""
        }
      }
      ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [May 2, 2023, 9:11am UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/5 "2023-05-02T09:11:34Z")

</div>

Hi, @RabBit_BR,

It seems that this query works, but how can I use it to search through my index?

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [May 2, 2023, 9:16am UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/6 "2023-05-02T09:16:51Z")

</div>

I tried to search like this:

```auto
GET my_index/_search
{
"source": """{"sort": {{#toJson}}sort{{/toJson}},"from":"{{from}}","size":"{{size}}","query":{"bool":{"must":{{#toJson}}clauses{{/toJson}}}}}""",
"params": {
"from": 0,
"size": 100,
"sort": {
"field1": {
"order": "desc"
}
},
"clauses": [
{
"match":{
"field2":""
}
},
{
"match":{
"field3":"true"
}
},
{
"match":{
"field4":"true"
}
},
{
"exists":{
"field":"field1"
}
}
]
}
}

```

---

<div class="post-metadata">

**Author:** ![RabBit\_BR](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rabbit_br/32/82261_2.png) [@RabBit\_BR](https://discuss.elastic.co/u/RabBit_BR)\
**Post date:** [May 2, 2023, 12:35pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/7 "2023-05-02T12:35:52Z")

</div>

Try this:

```auto
PUT _scripts/template_test
{
  "script":{
    "lang": "mustache",
    "source": """{"sort": {{#toJson}}sort{{/toJson}},"from":"{{from}}","size":"{{size}}","query":{"bool":{"must":{{#toJson}}clauses{{/toJson}}}}}"""
  }
}

GET test/_search/template
{
  "id": "template_test",
  "params": {
    "from": 2,
    "size": 4,
    "sort": {
      "field2": {
        "order": "desc"
      }
    },
    "clauses": [
      {
        "match": {
          "field2": true
        }
      },
      {
        "match": {
          "field3": false
        }
      },
      {
        "match": {
          "field4": true
        }
      },
      {
        "exists": {
          "field": "field1"
        }
      }
    ]
  }
}

```

---

<div class="post-metadata">

**Author:** ![orlenkoda5](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/orlenkoda5/32/117648_2.png) [@orlenkoda5](https://discuss.elastic.co/u/orlenkoda5)\
**Post date:** [May 4, 2023, 6:03pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/8 "2023-05-04T18:03:09Z")

</div>

Everything works. Thanks a lot!

---

<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:** [June 1, 2023, 6:03pm UTC](https://discuss.elastic.co/t/sorting-results-of-a-search-template-with-variable-value-of-sorting-field/331078/9 "2023-06-01T18:03:22Z")

</div>

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