thinking-sphinx

ThinkingSphinx::OutOfBoundsError configuration confusion


Hitting an OutOfBoundsError as a consequence of misunderstanding the proper configuration syntax (which may also be a by-product of legacy syntax).

The manual suggests a Class search taking on WillPaginate styled parameters. Having many fields to draw from, the model is defined as

class AziendaSearch < BaseSearch
  set_per_page 10000
  accept :terms
end

the set_per_page was put at a high level because if I set it at the target of 100, the will_paginate links do not show up.

the controller may be excessively convoluted to include the ordering parameter, and thus result in a two-step process:

@azienda_search = AziendaSearch.new params
@results = @azienda_search.search
@aziendas = Azienda.order('province_id ASC').where('id IN (?)', @results).paginate :page => params[:page], :per_page => 100

the view paginates on the basis of @aziendas:

<%= will_paginate @aziendas, :previous_label => "precedente ", :next_label => " successiva" %>

My suspicion is that the search model is not properly set, but the syntax is not obvious to me given the manual's indications. page params[:page] certainly does not work...

Update BaseSearch is a Sphinx method and was in fact inherited from an older version of this applications (rails2.x...). So this may be hanging around creating all sort of syntaxic confusion.

In fact, following the manual, I am now fully uncertain as to how to best makes these statements. Should a seperate class be defined for AziendaSearch ? If not, where should the Azienda.search bloc be invoked... in the controller as such?

@azienda_search = Azienda.search(
  :max_matches => 100_000,
  :page        => params[:page],
  :per_page    => 100,
  :order       => "province_id ASC"
  )
@results = @azienda_search.search

Solution

  • I'm not sure what BaseSearch is doing with set_per_page (that's certainly not a Thinking Sphinx method), but it's worth noting that Sphinx defaults to a maximum of 1000 records. It is possible to configure Sphinx to return more, though - you need to set max_matches in your config/thinking_sphinx.yml to your preferred limit (per environment):

    production:
      max_matches: 100000
    

    And also set the limit on the relevant search requests:

    Azienda.search(
      :max_matches => 100_000,
      :page        => params[:page],
      :per_page    => 100
    )
    

    As for the doubled queries… if you add province_id as an attribute in your index definition, you'll be able to order search queries by it.

    # in your Azienda index definition:
    has province_id
    
    # And then when searching:
    Azienda.search(
      params[:azienda_search][:terms],
      :max_matches => 100_000,
      :page        => params[:page],
      :per_page    => 100,
      :order       => "province_id ASC"
    )