Skip to content

Instantly share code, notes, and snippets.

@mrdanadams
Created March 28, 2012 20:55
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mrdanadams/2230436 to your computer and use it in GitHub Desktop.
Save mrdanadams/2230436 to your computer and use it in GitHub Desktop.
Boosting search results for Facebook friends with fb_graph, omniauth, Sunspot and Solr
ids = current_user.facebook_friends if current_user
@search = Profile.solr_search do
keywords params[:q] do
boost_fields name: 10.0, user: 4.0
end
unless ids.blank?
adjust_solr_params do |params|
# See "bq (boost query)" in http://wiki.apache.org/solr/DisMaxQParserPlugin
params[:bq] = "facebook_id_s:(#{ids.join(' OR ')})^10"
end
end
paginate page: params[:page], per_page: page_size
end
@results = @search.results
searchable do
# some normal fields to search
text :name, :description
# index the ids as an array of strings (not text) as we want exact match, no stemming, etc
string :facebook_id do
user.facebook_id
end
end
# Returns a user associated with a facebook account.
def self.find_for_facebook_oauth(access_token, signed_in_resource=nil)
data = access_token.extra.raw_info
if user = User.where(:email => data.email).first
user
else # Create a user with a stub password.
user = User.new email: data.email, first_name: data.first_name, last_name: data.last_name, password: Devise.friendly_token[0,20]
facebook_id = data.id
token = access_token.credentials.token
user.facebook_info = { 'id' => facebook_id, 'token' => token }
# in the future we could save the user and get the friends in a background worker if it's too slow
fb_user = FbGraph::User.fetch facebook_id, access_token: token
user.facebook_friends = fb_user.friends.map &:identifier
user.save!
user.confirm!
user
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment