Re: [ts] Sortable links using Thinking Sphinx

126 views
Skip to first unread message

Pat Allan

unread,
Aug 16, 2012, 8:03:56 PM8/16/12
to thinkin...@googlegroups.com
Hi Frank

The .order method on models is for SQL queries, not Sphinx searches, so you can't mix the two together. If you want to use sort_column and sort_direction, then pass the string you're building with the :order option (which it seems you're currently doing with :created_at):

Shruffle.search params[:search], :order => "#{sort_column} #{sort_direction}"

Also, I'd recommend reading up on the differences between attributes and fields - you probably don't want/need price, org_percent or created_at to be fields. Attributes are inherently sortable (indeed, when fields are marked as sortable, Thinking Sphinx is just creating an attribute in the background to fulfil that purpose).
http://freelancing-god.github.com/ts/en/sphinx_basics.html

Hope this helps.

--
Pat

On 16/08/2012, at 3:22 PM, frankphilips wrote:

> Hi,
>
> I'm a noob to Rails, and I need some help. In Railscast 240, Ryan talks about creating sortable table columns with ajax search: http://railscasts.com/episodes/240-search-sort-paginate-with-ajax
>
> However when I try to combine Thinking Sphinx rather then using the standard search Ryan mentions, it breaks my sortable table columns. I'm sure there is a quick way to fix this. Please help! Thanks :)
>
> Here's my code:
>
> MODEL:
>
> define_index do
>
> indexes :title, sortable: true
> indexes :desc, sortable: true
> indexes email
> indexes :org, sortable: true
> indexes org_percent
> indexes :price, sortable:true
> indexes :city, sortable: true
> indexes :created_at, sortable: true
>
> has created_at, updated_at
>
> end
>
> CONTROLLER:
>
> helper_method :sort_column, :sort_direction
> def index
> @shruffles = Shruffle.order(sort_column + ' ' + sort_direction).search(params[:search], :order => :created_at,
> :sort_mode => :desc)
>
> APPLICATION HELPER:
>
> def sortable(column, title = nil)
> title ||= column.titleize
> css_class = (column == sort_column) ? "current #{sort_direction}" : nil
> direction = (column == sort_column && sort_direction == "asc") ? "desc" : "asc"
> link_to title, {:sort => column, :direction => direction}, {:class => css_class}
> end
>
> VIEW:
>
> <%= hidden_field_tag :direction, params[:direction] %>
> <%= hidden_field_tag :sort, params[:sort] %>
>
> <table class="pretty">
> <tr align="left">
>
> <th><%= sortable "title" %></th>
> <th><%= sortable "price" %></th>
> <th><%= sortable "city" %></th>
> <th><%= sortable "org", 'Non-Profit' %></th>
> <th><%= image_tag "ico-heart.png" %> <%= sortable "org_percent", 'Donation %' %></th>
> <th><%= sortable "created_at", 'Date' %></th>
> </tr>
>
> <% for shruffle in @shruffles %>
>
> <tr>
> <td><%= link_to shruffle.title, shruffle %></td>
> <td class="price"><%= number_to_currency(shruffle.price) %></td>
> <td><%= shruffle.city %></td>
> <td><%= shruffle.org %></td>
> <td><%= shruffle.org_percent %>%</td>
> <td><%= shruffle.created_at.strftime("%b %d, %Y") %></td>
> </tr>
> <% end %>
> </table>
>
> -Frank
>
> --
> You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/Ls4y1X2qT5YJ.
> To post to this group, send email to thinkin...@googlegroups.com.
> To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/thinking-sphinx?hl=en.
>



frankphilips

unread,
Aug 17, 2012, 12:34:31 PM8/17/12
to thinkin...@googlegroups.com
Hi Pat,

I took your advice and incorporated your exact code.  I've also read up on attributes and fields and have fixed accordingly. However, now I'm getting this error:

"index shruffle_core: sort-by attribute 'created_at' not found"

I do have has created_at in my model.What am I doing wrong now? Thanks for the help!

-Frank

Pat Allan

unread,
Aug 17, 2012, 1:29:07 PM8/17/12
to thinkin...@googlegroups.com
Have you re-indexed Sphinxand restarted the daemon since making those changes? If not, one task does it all: rake ts:rebuild

Cheers

-- 
Pat

To view this discussion on the web visit https://groups.google.com/d/msg/thinking-sphinx/-/zvniTZRgm4QJ.

frankphilips

unread,
Aug 17, 2012, 2:38:12 PM8/17/12
to Thinking Sphinx
Did that, but now I'm getting this:

index shruffle_core: invalid sorting order 'desc_sort'
> > > To view this discussion on the web visithttps://groups.google.com/d/msg/thinking-sphinx/-/Ls4y1X2qT5YJ.
> > > To post to this group, send email to thinkin...@googlegroups.com.
> > > To unsubscribe from this group, send email to thinking-sphi...@googlegroups.com.
> > > For more options, visit this group athttp://groups.google.com/group/thinking-sphinx?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups "Thinking Sphinx" group.
> > To view this discussion on the web visithttps://groups.google.com/d/msg/thinking-sphinx/-/zvniTZRgm4QJ.

Pat Allan

unread,
Aug 17, 2012, 2:42:04 PM8/17/12
to thinkin...@googlegroups.com
Looks like your sort_direction method isn't returning the right details - should be ASC or DESC, much like SQL. Can you confirm what the generated value for the :order option is?

--
Pat

frankphilips

unread,
Aug 17, 2012, 2:48:50 PM8/17/12
to Thinking Sphinx
  def sort_column
    Shruffle.column_names.include?(params[:sort]) ? params[:sort] :
"created_at"
  end

  def sort_direction
    %w[asc desc].include?(params[:direction]) ? params[:direction] :
"desc"
  end


When I change the desc to asc, it works, but the columns don't change
on click. Check out http://shruffle.net

frankphilips

unread,
Aug 17, 2012, 2:51:59 PM8/17/12
to Thinking Sphinx
Also my Ajax search is all messed up :-/

It doesn't sort per key any more...Only when I type the whole
string ...eg: "Macbook"

frankphilips

unread,
Aug 17, 2012, 2:54:37 PM8/17/12
to Thinking Sphinx
Nvm, I fixed the sortable links.....it was an issue in my models.
Still trying to fix the ajax search though. Thanks for your help.

-Frank

Pat Allan

unread,
Aug 17, 2012, 3:03:13 PM8/17/12
to thinkin...@googlegroups.com
Is it that you want partial word or wildcard matching?
http://freelancing-god.github.com/ts/en/common_issues.html#wildcards

Also, to automatically apply wildcards to your searches, use the :star => true option in your search call.

--
Pat

frankphilips

unread,
Aug 17, 2012, 4:12:06 PM8/17/12
to Thinking Sphinx
Pat, I'm not seeing sphinx.yml ...where do I put this:

development:
enable_star: 1
min_infix_len: 1
test:
enable_star: 1
min_infix_len: 1
production:
enable_star: 1
min_infix_len: 1


On Aug 17, 2:03 pm, "Pat Allan" <p...@freelancing-gods.com> wrote:
> Is it that you want partial word or wildcard matching?http://freelancing-god.github.com/ts/en/common_issues.html#wildcards

frankphilips

unread,
Aug 17, 2012, 4:23:48 PM8/17/12
to Thinking Sphinx
Nvm,

I fixed it by creating the file config/sphinx.yml and putting this:

development:
bin_path: "/usr/local/bin"
searchd_binary_name: searchd
indexer_binary_name: indexer
enable_star: 1
min_infix_len: 1

All works well now! Thanks so much for all your help!!!!
Reply all
Reply to author
Forward
0 new messages