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

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.

```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"`

`}`

`}``

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

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:

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"
}
}
]
}
}

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

Hi @orlenkoda5

This example you can use sort by parameter.

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":""
        }
      }
      ]
  }
}

Hi, @RabBit_BR,

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

I tried to search like this:

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"
}
}
]
}
}

Try this:

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"
        }
      }
    ]
  }
}
1 Like

Everything works. Thanks a lot!

1 Like

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