title: ElasticSearch NEST, map the search response directly to a ViewModel class that is different from the ElasticSeach Index class
body: <p>I am using NEST 7.17.0 and Elasticsearch 7.16.2</p>
  <p>I'd like to know how I can map my search response directly to my ViewModel class without any transforming in C#.</p>
  <p>I have an ElasticIndex called &quot;PublicLegislationResult&quot;, and a ViewModel called &quot;ViewEntities.PublicLegislationDetail&quot; with a subset of the properties that exist in the ElasticSearch index.</p>
  <p>The properties in the ViewModel class are not necessarily the same as the ones in the main index. Ex: &quot;ItemSubCategory&quot;(index) =&gt; &quot;SubCategory&quot; (ViewModel)</p>
  <p>This search query works:</p>
  <pre><code>ISearchResponse&lt;PublicLegislationResult&gt; searchResults;
  
  searchResults = await this.Client
                       .SearchAsync&lt;PublicLegislationResult&gt;(s =&gt;
                           this.GetPublicSearchDescriptor(s)
                           .Query(q =&gt; q
                              .Bool(b =&gt; b
                                  .Must(m =&gt; m.Match(mt =&gt; mt.Field(f =&gt; f.CalendarYear).Query(searchCriteria))))));
  
  return searchResults;
  </code></pre>
  <p>But I have to additionally transform the results to my ViewModel as such</p>
  <pre><code>List&lt;PublicLegislationResult&gt; publiclegislationHitList = publicLegislationResult.Hits?.Select(t =&gt; t.Source).ToList();
  fileDetailList = publiclegislationHitList?.Select(t =&gt; new ViewEntities.PublicLegislationDetail(){
     ...
  }.ToList();
  </code></pre>
  <p>I have tried this:</p>
  <pre><code>
  ISearchResponse&lt;ViewEntities.PublicLegislationDetail&gt; searchResults;
  
  searchResults = await this.Client
                       .SearchAsync&lt;ViewEntities.PublicLegislationDetail&gt;(s =&gt;
                           this.GetPublicSearchDescriptor(s)
                           .Query(q =&gt; q
                              .Bool(b =&gt; b
                                  .Must(m =&gt; m.Match(mt =&gt; mt.Field(f =&gt; f.CalendarYear).Query(searchCriteria))))));
  return searchResults;
  </code></pre>
  <p>This partially works (surprisingly) because the ViewModel object closely matches the index and NEST is able to map the properties with exact matching names, the rest are returned as NULL</p>
  <p>Hence my question: If NEST is able to do this much out-of-the-box, is there a way to explicitly map the properties of the index to my view model so that I can skip the transformation step completely?</p>
  <p>Any help figuring this out would be greatly appreciated, thanks!</p>