Hi Pat,
Thank you for the quick reply!
On your advice I've implemented a search controller/helper and have
two methods to check the case against the models I have and return
accordingly.
Presently I am using Ajax for my search, and am able to display many
results extremely quickly.
However I still cannot paginate through these due to my routes
complaining search is post only.
My problem is that regardless of if it is using Ajax or not, when I
display my results it is because of a call to the search controller
search choice, and this needs to be a post for it to work. (Or at
least I believe so).
search_helper.rb
def find_link(result)
case result
when User
user_path(result)
end
end
search_controller.rb
def search
session[:query] = params[:query].strip if params[:query]
if session[:query] and request.xhr?
@search = ThinkingSphinx.search session[:query],
:star => true,
:max_matches => 1000,
:per_page => 1000
render :partial => "results",
:layout => false,
:locals => {:results => @search}
end
end
index.html.erb
<div>
<%= text_field_tag("query", params[:query], :autocomplete => "off" )
%>
<%= observe_field 'query',
:frequency => 2,
:update => "results",
:url => {:controller => "search", :action => "search"},
:with => "query" %>
</div>
<div id="results"></div>
_results.html.erb
<% if results.length == 0 %>
<p>Sorry, nothing matched your search query...</p>
<% elsif params[:query] == "" %>
<p>Please enter text into the text field to start the query</p>
<% else %>
<% paginated_section results do %>
<ul>
<% for result in results %>
<li><%= link_to find_name(result), find_link(result) %></li>
<% end %>
</ul>
<% end %>
<% end %>
routes.rb
# we only require search for the ajax search interface
map.search '/search', :controller => 'search', :action => 'index'
map.resources :search, :only => [ :index ], :collection => { :search
=> :post }
Really I want to be able to paginate through the results and not have
to display a large amount in the ajax view, but I can't currently see
a better way. I'd be happy to have separate pages for searching and
returns, but my previous solution wouldn't work because of the post
request problem.
I know this is less related to thinking-sphinx than a general problem,
but if you know of a better way I'd be grateful.
On a thinking-sphinx related problem, is there a way to group by the
model?
Thanks
plcs
On Dec 10, 4:39 am, Pat Allan <[email protected]> wrote:
> Hi there
>
> If you're after search results, not a search breakdown by category, then
> facets isn't the best tool for the job. You should just be able to search
> across all models:
>
> ThinkingSphinx.search @query, :star => true
>
> As for dealing with different objects, I'd recommend writing a view helper to
> determine where to send links. Of course, if all objects have a name (or
> whatever you want the link text to be), and have matching
> controllers/resources set up, then you could just loop through like so:
>
> <%- @results.each do |result| -%>
> <li><%= link_to result.name, result %></li>
> <%- end -%>
>
> And Rails will figure out the rest. This isn't particularly flexible though.
>
> As for getting *all* results, there is an inherent limit for Sphinx and how
> it works, but you can work around it to some extent (essentially, you can't
> turn pagination off, but you can request really big
> pages):http://freelancing-god.github.com/ts/en/advanced_config.html#large-re...
>
> --
> Pat
>
> On 09/12/2009, at 10:51 PM, plcs wrote:
>
>
>
> > Hi,
>
> > I've been banging my head against the nearest wall trying to find the
> > best way to create links from the results I get back from searching
> > across all my models. Essentially I want it so that there is a single
> > search controller that will search all my existing models and return
> > the results of the search, with links to the page in question.
>
> > So is facet's the way to go, or should I be using the normal Search?
>
> > Below is how I perform the facet search, and it works fine, my trouble
> > is handling the output it gives me.
>
> > def search
> > �...@query = params[:query]
> > �...@facets = ThinkingSphinx.facets @query, :all_facets => true,
> > :star => true
> > respond_to do |format|
> > format.html
> > format.xml { render :xml => @facets }
> > end
> > end
>
> > <div class="search">
> > <% @facets.each do |facet, facet_options| %>
> > <% unless facet_options.blank? %>
> > <h5><%= facet %></h5>
> > <ul>
> > <% facet_options.each do |option, count| %>
> > <li><%= link_to "#{option} (#{count})",
> > :params => {facet => option, :page => 1} %></li>
> > <% end %>
> > </ul>
> > <% end %>
> > <% end %>
> > </div>
>
> > Basically I don't want the situation where I have to check the
> > instance of the option to decide what path from routes to give it, I'd
> > really prefer it if there was a general method that'll find this for
> > me.
>
> > Should I really be using facets here, or normal search?
>
> > And as a side note, is there anyway to get ALL results from search?
> > Not just the first 10?
>
> > Thank you for your help.
>
> > plcs
>
> > --
>
> > You received this message because you are subscribed to the Google Groups
> > "Thinking Sphinx" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> > [email protected].
> > 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 post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/thinking-sphinx?hl=en.