title: searchkick/elasticsearch set up autocomplete with multiple attributes
body: <p>I have set up my application with the searchkick gem and am working on the autocomplete feature for my Presenters index. My problem is that I am searching on multiple fields (first_name and last_name), and while I can search for a presenter by both fields - for example, if I have Presenter 'Jennifer Hall', I can type either 'Jennifer' or 'Hall' and the name 'Jennifer Hall' will appear in the autocomplete drop down, but if I type 'Jennifer ' (once I add the space), the name disappears and there are no more suggestions. My question is how can I fix this?</p>
  
  <p><strong>My Code</strong></p>
  
  <p>I have followed this tutorial:
  <a href="https://shellycloud.com/blog/2013/10/adding-search-and-autocomplete-to-a-rails-app-with-elasticsearch" rel="nofollow noreferrer">Adding search and autocomplete to a Rails app with Elasticsearch</a></p>
  
  <p>presenter.rb:</p>
  
  <pre><code>class Presenter &lt; ActiveRecord::Base
    searchkick autocomplete: ['first_name', 'last_name'],
               suggest: ['first_name', 'last_name']
  
    ...
  end
  </code></pre>
  
  <p>presenters_controller.rb:</p>
  
  <pre><code>class PresentersController &lt; ApplicationController
    def index
      if params[:query].present?
        @presenters = Presenter.search(params[:query],
                                       fields: [:first_name, :last_name],
                                       page: params[:page])
      else
        @presenters = Presenter.all.page params[:page]
      end
    end
  
    def autocomplete
      render json: Presenter.search(params[:query],
                   autocomplete: true,
                   fields: [:first_name, :last_name],
                   limit: 10).map { |presenter|
                     presenter.first_name + " " + presenter.last_name
                   }
    end
  
    ...
  end
  </code></pre>
  
  <p>I got the .map{ |presenter| etc. } idea from here:</p>
  
  <p><a href="https://stackoverflow.com/questions/21081358/how-do-i-map-multiple-attributes-in-rails-with-searchkick-gem">how do i map multiple attributes in rails with searchkick gem</a></p>
  
  <p>However, when I followed the solution exactly, I only got 'undefined' in my drop-down of suggested names. Changing it to presenter.first_name + " " + presenter.last_name made it so the full name appears in the drop-down.</p>
  
  <p>routes.rb:</p>
  
  <pre><code>  resources :presenters, :only =&gt; [:index, :show] do
      collection do 
        get :autocomplete
      end
  </code></pre>
  
  <p>index.html.haml:</p>
  
  <pre><code>.container
    = form_tag presenters_path, method: :get do 
      - if params[:query].present?
        .btn-search.btn-clearsearch
          = link_to "clear", presenters_path
      - else
        .search
          Search
      = text_field_tag :query, params[:query],
                       id: "presenter_search",
                       autocomplete: "off"
      = submit_tag "go", class: "btn-search"
  </code></pre>
  
  <p>I am using typeahead.js, so that works with:</p>
  
  <p>application.html.haml:</p>
  
  <pre><code>= javascript_include_tag 'application'
  = javascript_include_tag
    "//cdnjs.cloudflare.com/ajax/libs/typeahead.js/0.9.3/typeahead.min.js"
  </code></pre>
  
  <p>and</p>
  
  <p>presenter.js.coffee:</p>
  
  <pre><code>$ -&gt;
      $('#presenter_search').typeahead
          name: "presenter"
          remote: "/presenters/autocomplete?query=%QUERY"
  </code></pre>
  
  <p>So, once again, my problem is that if I type the first name, all options matching that first name will appear in drop-down (both first and last name), but as soon as I type the space, the entire list of suggestions goes away. I have no way of searching, say, any first and last name 'Jennifer H'. I can guess that the problem is with the mapping in the controller/autocomplete action, but typing:</p>
  
  <pre><code>.map(&amp;:first_name, &amp;:last_name)
  </code></pre>
  
  <p>or </p>
  
  <pre><code>.map(&amp;:first_name, :last_name)
  </code></pre>
  
  <p>generate errors, and </p>
  
  <pre><code>.map{ |presenter| presenter.slice(:first_name, :last_name) }
  </code></pre>
  
  <p>makes all suggestions in the drop-down read 'undefined.'</p>
  
  <p>Any suggestions on how to solve this?</p>