Restricting Response Fields when using a Search Template

Hi,
I can't for the life of me figure out how you can restrict what fields are returned when using a search template. I have the following template.

  "template": {
    "query": {
      "filtered": {
        "query": {
          "bool": {
            "should": [
              {
                "match": {
                  "title": {
                    "query": "{{useCase}}",
                    "boost": 0.5
                  }
                }
              },
              {
                "boosting": {
                  "positive": {
                    "match": {
                      "label_names": {
                        "query": "{{label}}",
                        "boost": 0.75, 
                        "analyzer": "camel_analyzer"
                      }
                    }
                  },
                  "negative": {
                    "match": {
                      "label_names": "device, device info, device information"
                    }
                  },
                  "negative_boost": 1
                }
              },
              {
                "match": {
                  "body": {
                    "query": "{{useCase}}",
                    "boost": 0.5
                  }
                }
              }
            ]
          }
        }
      }
    }
  }

So most of my searching refers to this syntax
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-request-fields.html

It seems like adding the field to usage request would be the logical place so that you could at run time control what you want to return.

GET myindex/_search/template
{
    "fields" : ["title", "label_names"],
    "id": "articles", 
    "params": {
        "useCase": "my search string",
        "label": "label" 
    }
}

but fields its not allowed here, nor is it allowed in the template above the query sections. which is very confusing because based on the docs it seems a like you should be able to stick it there but it seems like query in a template is not a real query.

Hey,

not sure I got the requirement a hundred percent right, but what about source filtering

See this example

PUT /foo/bar/1
{
  "foo": {
    "a":"a",
    "b":"b"
  }
}

PUT /foo/bar/2
{
  "foo": {
    "a":"a2",
    "b":"b2"
  }
}

GET foo/_search/template
{
    "inline" : {
      "_source": "{{filter}}"
    },
    "params" : {
        "filter" : "foo.a*"
    }
}

--Alex

So the idea was to store all our query logic in a search template. Then at run time you can return just the fields you need depending on how it will be used. eg if your just rendering the title and image of a document then just return those fields. theirs no need sending a huge body field if it won't be used. The other use that i am facing at the moment is just for testing so i can focus on the important fields.

So why is that syntax so different to the way the documentation recommends doing it when you not using a search template? The thing that confuses me is that in this templates you have 2 fields that are called "query"

 "template": {
"query": {
  "filtered": {
    "query": {

But you can't use any of its sibling fields as suggested in the documentation such as this.

{
    "fields": ["user", "message", ...],
    "query": ...,
    "size": ...
}

If you add fields or size in the same scope as either "query" fields then it returns an error about those fields being invalid. In addition to this aren't bool, match and boosting technically queries as well? but you can't seem to add those fields within the same scope as them either.

Hey,

so the fields mechanism requires a special mapping (setting a field to be stored), where as the source filtering is able to extract data from the JSON. The fields mechanism is also renamed in 5.0.

--Alex