title: Index a dynamic object using NEST
body: <p>I am building an API application that essentially allows a user to build a document, which can be structured however they want, that will be stored in Elasticsearch. Essentially, I'm providing a simple interface for users to access our Elasticsearch instance. I'm trying to keep the implementation as simple as possible. Here's what I'm dealing with so far.</p>
  
  <p>The object for the expected body:</p>
  <pre><code>public class DocumentModel
  {
      public string Index { get; set; }
      public string Type { get; set; }
      public string Id { get; set; }
      [ElasticProperty(Type = FieldType.Nested)]
      public dynamic Document { get; set; }
  }
  </code></pre>
  <p>Simple implementation:</p>
  <pre><code>[HttpPost]
  [Route(&quot;&quot;)]
  public IHttpActionResult Post(DocumentModel document)
  {
      Uri nodeLocation = new Uri(&quot;http://localhost:9200&quot;);
      IConnectionPool connectionPool = new SniffingConnectionPool(new List&lt;Uri&gt; { nodeLocation });
      ConnectionSettings settings = new ConnectionSettings(connectionPool);
      ElasticClient esClient = new ElasticClient(settings);
  
      IIndexResponse result = esClient.Index(document, i =&gt; i
          .Index(document.Index)
          .Type(document.Type)
          .Id(document.Id));
  
      return Ok(result.IsValid);
  }
  </code></pre>
  <p>This works fine, but it includes the Index, Type, and Id in the source. What I'd really like to do is simply provide those three pieces of information when indexing, but actually just index document.Document, which is of a dynamic type. But, that seems to disagree with Nest, as it throws an error in the IDE and at compile time:</p>
  <blockquote>
  <p>&quot;An anonymous function or method group cannot be used as a constituent value of a dynamically bound operation&quot;</p>
  <p>&quot;Cannot use a lambda expression as an argument to a dynamically dispatched operation without first casting it to a delegate or expression tree type&quot;.</p>
  </blockquote>
  <p>How can I index just <code>document.Document</code>? Is there a better way to handle an incoming JSON document of unknown structure than using a dynamic type?</p>