title: How to set up a elasticsearch facet search on a nested rails model
body: <p>Is it possible to setup a facet search on a nested model? I have a user model that has a profile model. I can search the user model for terms in the profile model. Now I want to filter the search results (users) by the location attribute in the profile model.</p>
  
  <p>User Model:</p>
  
  <pre><code>class User&lt; ActiveRecord::Base
    has_one :profile
  
    include Tire::Model::Search
    include Tire::Model::Callbacks
  
    mapping do
     indexes :profiles do
      indexes :first_name
      indexes :last_name
      indexes :summary, type: 'string'
      indexes :location, type: 'string', boost: 50
      indexes :subjects, type: 'string', boost: 100
      indexes :education, type: 'string'
     end
    end
  
    def self.search(params)
      tire.search(load: true, page: params[:page], per_page: 10) do |s|
        s.query { string params[:query]} if params[:query].present?
        s.facet "locations" do
          terms :location???
        end
      end
    end
  
    # Rest of class omitted
  end
  </code></pre>
  
  <p>In the search method I do not know what to put down for the "terms". <code>terms :location???</code></p>
  
  <p>View:</p>
  
  <pre><code>  &lt;% @allusers.facets['locations'??]['terms'].each do |facet| %&gt; **error on this line, locations cannot be nil**
        &lt;li&gt;
        &lt;%= link_to_unless_current Profile.find(facet['term']).location, params.merge(id: facet['term']) %&gt;
          &lt;% if params[:location??] == facet['term'].to_s %&gt;
            (&lt;%= link_to "remove", location: nil %&gt;)
          &lt;% else %&gt;
            (&lt;%= facet['count'] %&gt;)
          &lt;% end %&gt;
        &lt;/li&gt;
      &lt;% end %&gt;
  </code></pre>
  
  <p>In the view I do not know what to put down for the params in place of <code>[:location]</code></p>