Using Websolr with Heroku to do full text search - ruby-on-rails

Hi I am having trouble trying to figure out how to implement a search form globally across my application. I have a series of posts that need to be searchable by users that are signed in and not signed in. I have added this code in my post model:
searchable do
text :content, :default_boost => 2
text :body, :default_boost => 1.5
end
However, I do not know where to go from there to create a search field across all pages and make it show the results I need. I am new to rails and would be happy to post more information if someone is willing to help me out.

First, you should add your search field like explained in this railscast: http://railscasts.com/episodes/37-simple-search-form
Since your search isn't specific to a particular model, use a generic controller name instead of ProjectsController though.
Then, you should replace the ActiveRecord finder by the use of the Sunspot DSL.
Here is an sample code to help get you started:
page = #page = params[:page] && params[:page].to_i || 1
#search = Sunspot.search(Realty) do # search_ids
per_page = params[:per_page] && params[:per_page].to_i || 10
# not adapted to your case
with(:equipments).all_of params['equip'].split(' ') if params['equip']
case params[:sort]
when "average_rating"
order_by :average_rating, :desc
when "type"
order_by :type, :asc
end
paginate :page => page, :per_page => per_page
# other criteria...
end
In your view, you can then iterate through #search.results
<%= will_paginate #search.results %>
<% #search.results.each do |hit| %>
<%# 'path' contains the stored polymorphic_path of each model object #%>
<% link_to hit.stored('path') do %>
<p><%= hit.stored('content') %></p>
<% end %>
<% end %>
Last, using WebSolR instead of a standard SolR server is quite simple, you can follow the setup instructions at https://github.com/onemorecloud/websolr-rails.
Edit:
As Nick commented, you should totally go to http://docs.heroku.com/websolr.
Thanks Nick !

Related

How do I use a button to update the order of a filtered category?

I am new to Rails, but slowly making progress. I can't quite wrap my head around how to achieve my next task.
I have a controller (IdeasController) with an index that looks like this:
def index
if params[:round].blank? && params[:challenge].blank?
#ideas = Idea.all.order(params[:sort])
# #ideas = Idea.all.order(created_at: :desc, cached_votes_up: :desc)
end
if params[:round].present?
#round_id = Round.find_by(name: params[:round]).id
#ideas = Idea.where(round_id: #round_id).order("created_at DESC")
end
if params[:challenge].present?
#challenge_id = Challenge.find_by(name: params[:challenge]).id
#ideas = Idea.where(challenge_id: #challenge_id).order("created_at DESC")
end
end
I am updating the view and filtering by category with the above :round and :challenge with the code below in my index.html.erb:
<%= link_to "All", ideas_path %>
<% Round.all.each do |round| %>
<%= link_to round.name, ideas_path(round: round.name) %>
<% end %>
<% Challenge.all.each do |challenge| %>
<%= link_to challenge.name, ideas_path(challenge: challenge.name) %>
<% end %>
Now, my problem is that I want to create a button that orders by created_at DESC or ASC. I want the button to essentially be a toggle. I also want another button to order by cached_weighted_average DESC or ASC. This is from acts_as_votable so I can sort by vote counts.
The problem I am running into is that I can create a link or button that orders by created_at or cached_weighted_average, but it replaces all of the URL that was previously filtered by :round or :challenge. For example, if a user clicks "Round 1" and sees all ideas marked for "Round 1" and then they click the link to order by cached_weighted_average, the URL replaces:
/ideas?round=Round+1
With this:
/ideas?sort=cached_weighted_average+ASC
What I want is:
/ideas?round=Round+1&?sort=cached_weighted_average+ASC
I know this is a very new question, but everything I have tried has failed so far. It feels like I am missing something very easy. What I noticed I can do easily is inside the controller I can do something like:
if params[:round].present?
#round_id = Round.find_by(name: params[:round]).id
#ideas = Idea.where(round_id: #round_id).order("cached_weighted_average DESC")
end
Which is perfect. This button just needs to switch between cached_weighted_average DESC and created_at DESC.
Any help is appreciated, thanks.
passing multiple parameters is one way to handle:
<%= link_to object.name, object_path(first: something, second: something_else) %>
then alter your conditionals to contemplate presence of multiple params.
to differentiate between round and challenge when attempting to allow the user to choose how they'd like to sort you could use the same name and then pass it different values.
something like:
params["round_or_challenge"]
this would change your conditional to something like:
if params["round_or_challenge"] == "round" && params["asc_or_desc"] == "asc"
# query
elsif params["round_or_challenge"] == "challenge"
# query
end
or whatever. it's basically the same...just pass the values you need. you can also pass the existing parameters from the view the same way you access them in the controller.
Thanks for the response, #toddmetheny. I didn't implement your solution, but your solution helped me understand passing multiple parameters a bit more.
I ended up creating a helper, sortable. I also used the url_for to append at the end of whatever the current URL might be. I liked this approach because it meant I could sort on any parameter. I'm not sure that it's the best solution, but it works.
def sortable (name, sort)
link_to name, url_for(params.merge(sort: sort))
end

Rails : search multiple attributes

I've managed to build a simple search model and have four attributes that can be searched; name, age, location and gender. The problem I am having is I can't seem to find the right code to search multiple attributes.
For example a search for "adam" should produce all users named adam, whereas a search for london should display all users from london. I can only search one attribute individually (name) so if I type in "london" it displays a blank result page.
/people/index.html.erb (search form)
<%= form_tag people_path, :method => 'get' do %>
<%= text_field_tag :search, params[:search]%>
<%= submit_tag "Search" %>
<% end %>
models/person.rb
class Person < ActiveRecord::Base
attr_accessible :age, :gender, :location, :name
def self.search(search, id)
if search
where(['name LIKE ?', "%#{search}%"])
else
scoped
end
end
end
people_controller.rb
def index
#people = Person.search(params[:search], params[:id])
end
The following code worked fine.
where('name LIKE ? OR location LIKE ?', "%#{search}%","%#{search}%")
#meagar, I fail to understand how that simple line of code "is outside the scope of Stack Overflow".
You can checkout Sunspot_rails gem for your problem, it integrate Solr search engine platform into Rails and is a battle proved solution for Rails app. In my company's website Fishtrip.cn we use solr to search for both House, Transportation retailer and tours. It might be a little bit heavy for your project, but if are looking for a powerful solution then Sunspot definitely would be one of it.

I need a very simple search gem for just searching the partyname on a sqlight3 database?

My party registry Rails 3 app needs a search function for visitors to search a partyname and then be able to click on the party and go to the show page of that party. Must be very simple ajax list that pops up without reloading etc.
The app is almost done and runs off a sqlight3 database.
Any suggestions - tried all the RailsCasts ones and nothing fits right 100%.
Thanks in advance.
I think you want two different things.
1) To create a simple search you can do something like this.
2) To autocomplete while typing (partyname for example) you can use the autocomplete gem.
You must decide which approach you want to follow.....
I hope if helps in some way...
EDIT - to show how to implement the simple search.
let's say you have a model called Party
your form (index page for example):
...
<%= form_tag parties_path, method: :get do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search", name: nil %>
<% end %>
...
#display results
<% #parties.each do |party| %>
...
<% end %>
model Party:
...
def self.search(search)
# if search is not empty
if search
find(:all, :conditions => ["partyname LIKE ?", "%#{search}%"])
# if search is empty return all
else
find(:all)
end
end
...
controle parties_controller:
#parties = Party.search(params[:search])
Try smart_search gem
gem install smart_search
Does everything that needs to be done to make your model searchable!

Thinking Sphinx Faceted Search Implementation Examples?

I am putting together a repository-type rails 3 site.
I have Thinking Sphinx installed and working on my site, insomuch as I can enter urls like localhost:3000/articles?search=test&page=2 and it will return the expected results.
I'm new to Rails (and web dev in general); I think I can manage the model and controller aspects of this, but the views so far have me stumped. I have a sidebar I would like to use as the search interface. The closest I have come is this (rendered as part of a sidebar partial):
<% form_tag #search, :method => :get do %>
<%= text_field_tag :search, params[:search] %>
<%= submit_tag "Search"%>
<% end %>
The search method is in my articles#index controller, and when I test it when the browser is pointed to that page (routed as /articles), it works as expected, but with this odd url: localhost:3000/articles?utf8=✓&search=test&commit=Search. When the browser is first pointed to the root path, nothing happens.
So, I think these are the main issues I need to address:
EDIT - solved (see below)
Should I move the search methods to their own controller, or should they be part of the articles controller? For now, Article will be the only model indexed.
EDIT - solved (see below)
Does anyone have any good example code of a faceted search view using Rails 3 and Thinking Sphinx? Like I said, I am something of a neophyte and am a little flustered by the documentation that skims by the view implementation. However, I am fairly adept at reading and interpreting code as long as it is reasonably complete.
Thanks in advance!
Solved:
How do I make the 'Search' button call the index method before trying to search? (I have solved this by replacing #search with articles_path).
Solved using will_paginate, which I had trouble with before, but which seems to be working now.
Hey,
This is an extract of how my site worked before i switched to solr
Product has many categories, we tell sphinx that we want to index them as facets
class Product < ActiveRecord::Base {
has_many :categorisations, :dependent => :destroy
has_many :categories, :through => :categorisations
define_index do
indexes product_name,
indexes description
indexes categories(:name), :as => :category,:facet => true
end
}
Results Controller
class ResultsController < ApplicationController
def index
#facets = Product.facets params[:qt], :conditions => {:category => params[:category}},:page => params[:page], :per_page => 20
#products = #facets.for
end
end
And then in the view you can do something like
<% #facets.each do |facet, facet_options| %>
<span><%= facet %></span>
<ul>
<% facet_options.each do |option, count| %>
<li><%= link_to "#{option} (#{count})",
:params => {facet => option, :page => 1} %></li>
<% end %>
</ul>
<% end %>

Display search query in results of basic search in Rails

I built a basic search form that queries one column in one table of my app. I followed episode 37 Railscast: http://railscasts.com/episodes/37-simple-search-form
Here's my problem. I want to display the search query that the user makes in the view that displays the search results. In my app, the search queries the zip code column of my profile model, and returns a list of profiles that contain the right zip code. On the top of the list of profiles returned from the search, I want it to say "Profiles located in [zip code that was queried]."
I'm sure I can do this because the queried zip code gets passed into the url displaying the results. So if the url can pick it up, there must be some way to display it in the view on the page as well. But I don't how.
Please keep in mind that I'm not using any search pluggins and I don't want to use any for now. This is my first app, so I don't want to add complexity where it's not needed.
Per Ryan's instructions in the Railscast, here's my setup:
PROFILES CONTROLLER
def index
#profiles = Profile.search(params[:search])
end
PROFILE MODEL
def self.search(search)
if search
find(:all, :conditions => ['zip LIKE ?', "%#{search}%"])
else
find(:all)
end
end
PROFILE/INDEX.HTML.ERB
<% form_tag ('/profiles', :method => :get) do %>
<%= text_field_tag :search, params[:search], :maxlength => 5 %>
<%= submit_tag "Go", :name => nil %>
<% end %>
The search itself is working perfectly, so that's not an issue. I just need to know how to display the queried zip code in the view displaying the results.
Thanks!
Just set it to an instance variable and use that.
def index
#search = params[:search]
#profiles = Profile.search(#search)
end
In your view, you can reference #search.
Also, as a friendly tip, please use an indent of 2 spaces for Rails code. It's the standard way to do it, and others who are reading your code will appreciate it.

Resources