title: ElasticSearch C# Nest Getting top words with 5.1
body: <p>I have an ElasticSearch object with these fields:</p>
  
  <pre><code>[Keyword]
  public List&lt;string&gt; Tags { get; set; }
  [Text]
  public string Title { get; set; }
  </code></pre>
  
  <p>And, before I used to get the top Tags, in all the documents, using this code:</p>
  
  <pre><code>var Match = Driver.Search&lt;Metadata&gt;(_ =&gt; _
                    .Query(Q =&gt; Q
                    .Term(P =&gt; P.Category, (int)Category)
                       &amp;&amp; Q.Term(P =&gt; P.Type, (int)Type))
                    .FielddataFields(F =&gt; F.Fields(F1 =&gt; F1.Tags, F2 =&gt; F2.Title))
                    .Aggregations(A =&gt; A.Terms("Tags", T =&gt; T.Field(F =&gt; F.Tags)
                    .Size(Limit))));
  </code></pre>
  
  <p>But with Elastic 5.1, I get an error 400 with this hint:</p>
  
  <blockquote>
    <p>Fielddata is disabled on text fields by default. Set fielddata=true on
    [Tags] in order to load fielddata in memory by uninverting the
    inverted index.</p>
  </blockquote>
  
  <p>Then the <a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.0/fielddata.html" rel="nofollow noreferrer">ES documentation about parameter mapping</a> tells you "It usually doesn’t make sense to do so" and to "have a text field for full text searches, and an unanalyzed keyword field with doc_values enabled for aggregations".</p>
  
  <p>But the only doc with this is for 5.0, and the same page for 5.1 seem to not exist.</p>
  
  <p>Now, 5.1 has a page about <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-terms-aggregation.html" rel="nofollow noreferrer">Term Aggregation</a> that seems to cover what I need, but there is absolutely nothing to be found in C# / Nest that I can use.</p>
  
  <p>So, I'm trying to figure out how I can just get the top words, across all documents, from the Tags (where each tag is its own word; for example <em>"New York"</em> is not <em>"New"</em> and <em>"York"</em>) and the title (where each word is its own thing) in C#.</p>
  
  <hr>
  
  <p>I need to edit this post because there seems to be a deeper problem. I wrote some test code that illustrates the issue:</p>
  
  <p>Let's create a simple object:</p>
  
  <pre><code>public class MyObject
  {
      [Keyword]
      public string Id { get; set; }
      [Text]
      public string Category { get; set; }
      [Text(Fielddata = true)]
      public string Keywords { get; set; }
  }
  </code></pre>
  
  <p>create the index:</p>
  
  <pre><code>var Uri = new Uri(Constants.ELASTIC_CONNECTIONSTRING);
  var Settings = new ConnectionSettings(Uri)
  .DefaultIndex("test")
  .DefaultFieldNameInferrer(_ =&gt; _)
  .InferMappingFor&lt;MyObject&gt;(_ =&gt; _.IdProperty(P =&gt; P.Id));   
  var D = new ElasticClient(Settings);
  </code></pre>
  
  <p>fill the index with random stuff:</p>
  
  <pre><code>for (var i = 0; i &lt; 10; i++)
  {
      var O = new MyObject
      {
          Id = i.ToString(),
          Category = (i % 2) == 0 ? "a" : "b",
          Keywords = (i % 3).ToString()
      };
  
      D.Index(O);
  }
  </code></pre>
  
  <p>and do the query:</p>
  
  <pre><code>var m = D.Search&lt;MyObject&gt;(s =&gt; s
      .Query(q =&gt; q.Term(P =&gt; P.Category, "a"))
      .Source(f =&gt; f.Includes(si =&gt; si.Fields(ff =&gt; ff.Keywords)))
      .Aggregations(a =&gt; a
          .Terms("Keywords", t =&gt; t
              .Field(f =&gt; f.Keywords)
              .Size(Limit)
          )
      )
  );
  </code></pre>
  
  <p>It fails the same way as before, with a 400 and:</p>
  
  <blockquote>
    <p>Fielddata is disabled on text fields by default. Set fielddata=true on 
    [Keywords] in order to load fielddata in memory by uninverting the inverted
    index.</p>
  </blockquote>
  
  <p>but Fielddata is set to true on [Keywords], yet it keeps complaining about it.</p>
  
  <p>so, let's get crazy and modify the class this way:</p>
  
  <pre><code>public class MyObject
  {
      [Text(Fielddata = true)]
      public string Id { get; set; }
      [Text(Fielddata = true)]
      public string Category { get; set; }
      [Text(Fielddata = true)]
      public string Keywords { get; set; }
  }
  </code></pre>
  
  <p>that way everything is a Text and everything has Fielddata = true.. well, same result.</p>
  
  <p>so, either I am really not understanding something simple, or it's broken or not documented :)</p>