Search code examples
ruby-on-railsthinking-sphinx

ThinkingSphinx excerpt issue with associations with special chars


I'm having an issue with special characters (apostrophe, namely), only when present is a nested association, however.

I have a 'Vendor' model and an 'Event' model, where a Vendor has_many Events. Here are the index files:

vendor_index:

ThinkingSphinx::Index.define :vendor, :with => :active_record do
  indexes :name
  indexes city

  set_property :min_prefix_len => 2 
  set_property :enable_star => true
end

event_index:

ThinkingSphinx::Index.define :event, :with => :active_record do
  indexes title
  indexes subtitle
  indexes venue_name
  indexes vendor.name, :as => :vendor_name
  indexes vendor.city, :as => :vendor_city
  indexes genre.name, :as => :genre_name

  where "workflow_state = 'published'"

    set_property :min_prefix_len => 2   
  set_property :enable_star => true

end

I'm using an ExcerptorPane, like so, in my search#index action :

class SearchController < ApplicationController

  helper_method :format_autocomplete

  def index

    @events = Event.search params[:search], {:star => true , :per_page => 5, :page => params[:events_page]}
    @events.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane

    @vendors = Vendor.search params[:search], { :star => true , :per_page => 5, :page => params[:vendors_page]}
    @vendors.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane

    @users = User.search params[:search], { :star => true , :per_page => 5, :page => params[:users_page]}
    @users.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane

  end

  # methods used for ajax-y pagination
  def vendor_results
    @vendors = Vendor.search params[:search], { :star => true , :per_page => 5, :page => params[:vendors_page]}
    @vendors.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane
    respond_to do |format|
      format.js
    end
  end

  def user_results
    @users = User.search params[:search], { :star => true , :per_page => 5, :page => params[:users_page]}
    @users.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane
    respond_to do |format|
    format.js
    end   
  end

  def event_results
    @events = Event.search params[:search], { :star => true , :per_page => 5, :page => params[:events_page]}
    @events.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane
    respond_to do |format|
      format.js
    end
  end



  def get_terms
    results = ThinkingSphinx.search(params[:search], {:star => true})
    results.context[:panes] << ThinkingSphinx::Panes::ExcerptsPane

    results_json = format_autocomplete(results)

    respond_to do |format|
       format.js { render :json => results_json }
     end
  end


  private

    def format_autocomplete(r)

      bucket = [];

      r.each do |result|
        puts result.class
        if result.class.name == "Event"
          title = result.excerpts.title
          name = result.excerpts.vendor_name
          bucket << {
            :label => title,
            :value => title,
            :category => "Events",
            :subtitle => result.excerpts.subtitle,
            :url => event_url(result),
            :vendor_name => name,
            :vendor_city => result.excerpts.vendor_city,
            :genre_name => result.excerpts.genre_name,
            :venue_name => result.excerpts.venue_name
          }
        elsif result.class.name == "Vendor"
          name = result.excerpts.name
          bucket << {
            :label => name,
            :value => name,
            :category => "Vendors",
            :subtitle => result.excerpts.city,
            :url => vendor_url(result)
          }

        elsif result.class.name == "User"
          name = result.excerpts.name


 bucket << {
        :label => name,
        :value => name,
        :category => "Users",
        :subtitle => result.excerpts.city,
        :url => user_url(result)
      }
    end

  end
  bucket
end

end

I have also included a charset_table and ignore_chars in my thinking_sphinx.yml file.

Now, when I search for a Vendor with an apostrophe in their name, everything goes fine if the Vendor has no events. If a Vendor has events though, I get an error trying to render the event's vendor_name: (the full vendor name is "VIFF's Vancity Theatre, and the search query is 'viff')

sphinxql: syntax error, unexpected IDENT, expecting ')' near 's Vancity Theatre', 'event_core', '*viff*', '<span class="match">' AS before_match, '</span>' AS after_match, ' &#8230; ' AS chunk_separator)'

raised at this line in my view:

<p><%= link_to ( raw event.excerpts.vendor_name ), vendor_path(event.vendor) %></p>

I've been searching for a while, but can't find anything of help...Any ideas as to what might be causing this?

Thanks!

UPDATE:

It gets weirder ... with vendor name "Viff's Vancity Theatre", (and all events and vendors have city = 'Vancouver' ) if I search "van" or "vanc", everything renders fine, with "Vancity" marked as a match. However if I search "vanco" it breaks again. This happens when I am performing a search on specific models. When I perform a global search however (for autocomplete), I get the opposite behaviour - 'vanco' will work, but anything shorter throws back the same error. I've updated the code above with the full search_controller.rb.


Solution

  • I just received a pull request for Riddle that may contain a fix for this. Try adding the following to your Gemfile:

    gem 'riddle', '~> 1.5.6',
      :git    => 'git://github.com/pat/riddle.git',
      :branch => 'master',
      :ref    => '50d410cda6'