# C# NEST 5 Search with template

**URL:** <https://discuss.elastic.co/t/c-nest-5-search-with-template/104074>\
**Category:** Elasticsearch\
**Created:** [October 16, 2017, 10:07am UTC](https://discuss.elastic.co/t/c-nest-5-search-with-template/104074 "2017-10-16T10:07:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Itay\_Itzhaki](https://avatars.discourse-cdn.com/v4/letter/i/e95f7d/32.png) [@Itay\_Itzhaki](https://discuss.elastic.co/u/Itay_Itzhaki)\
**Post date:** [October 16, 2017, 10:07am UTC](https://discuss.elastic.co/t/c-nest-5-search-with-template/104074/1 "2017-10-16T10:07:05Z")

</div>

how can i use the ElasticClient SearchTemplateAsync method with skip, size, aggregations , and so forth..  
in the latest c# driver searching with templates is based on the method SearchTemplateAsync rather than SearchAsync. however, it seems that the searchtemplatedescriptor cannot be configured with basic stuff like skip and size. when i try to use the old SearchAsync with .Template, i get an obsolete error.

---

<div class="post-metadata">

**Author:** ![forloop](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/forloop/32/9021_2.png) [@forloop](https://discuss.elastic.co/u/forloop)\
**Post date:** [October 18, 2017, 12:58pm UTC](https://discuss.elastic.co/t/c-nest-5-search-with-template/104074/2 "2017-10-18T12:58:23Z")

</div>

You can create search templates by serializing searches created through the fluent API or object initializer syntax

```
var client = new ElasticClient();

var query = new SearchDescriptor<Question>()
    .Query(q => q
        .QueryString(m => m
            .Fields(f => f
                .Field(ff => ff.Title)
                .Field(ff => ff.Body)
            )
            .Query("{{query_string}}")
        )
    )
    .Aggregations(a => a
        .Terms("tags", ta => ta
            .Field(f => f.Tags)
        )
    );
    
var template = client.Serializer.SerializeToString(query);

client.PutSearchTemplate("template-name", ps => ps
    .Template(template)
);

var searchResponse = client.SearchTemplate<Question>(st => st
    .Index("posts")
    .Id("template-name")
    .Params(p => p
        .Add("query_string", "\"Jon Skeet\" AND \"Chuck Norris\"")  
    )
);

```

There are some methods within the fluent API that are constrained to take specific types e.g. From() and .Size() can only accept an int. You can also parameterize these with something like

```
var client = new ElasticClient();

var query = new SearchDescriptor<Question>()
    .Query(q => q
        .QueryString(m => m
            .Fields(f => f
                .Field(ff => ff.Title)
                .Field(ff => ff.Body)
            )
            .Query("{{query_string}}")
        )
    )
    .Aggregations(a => a
        .Terms("tags", ta => ta
            .Field(f => f.Tags)
        )
    );
    
var template = client.Serializer.SerializeToString(query);
var search = JObject.Parse(template);
search.Add("from", "{{from}}{{^from}}0{{/from}}");
search.Add("size", "{{size}}{{^size}}10{{/size}}");
template = client.Serializer.SerializeToString(search);

client.PutSearchTemplate("template-name", ps => ps
    .Template(template)
);

client.GetSearchTemplate("template-name");
    
var searchResponse = client.SearchTemplate<Question>(st => st
    .Index("posts")
    .Id("template-name")
    .Params(p => p
        .Add("query_string", "\"Jon Skeet\" AND \"Chuck Norris\"") 
        .Add("size", 50)
    )
);
```

---

<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:** [November 15, 2017, 12:58pm UTC](https://discuss.elastic.co/t/c-nest-5-search-with-template/104074/3 "2017-11-15T12:58:49Z")

</div>

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