-
-
Save karmi/83c93a8ba6a720979543 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This module contains the query definitions for searching mentions. | |
# | |
# Its instance methods return Tire's boolean queries as lambdas. | |
# | |
# They can be mixed together and used like this: | |
# | |
# Tire.search('dummy') do |search| | |
# search.query do |query| | |
# query.boolean &main_query | |
# query.boolean &keywords_query | |
# end | |
# end | |
# | |
# It relies on a contract from object it's being mixed into: | |
# | |
# * It has an +account+ method, returning Account model instance | |
# * It has a +params+ method, returning a Hash | |
# | |
# | |
# | |
# Concretely, in the controller, we have something like: | |
# | |
# @mentions = @account.mentions.search(nil, :page => params[:page], :per_page => session[:per_page]) do |search| | |
# search.query do |query| | |
# query.boolean &keywords_query | |
# query.boolean &main_query | |
# query.boolean &sources_query | |
# end | |
# | |
# search.size session[:per_page].to_i | |
# search.from( params[:page].to_i <= 1 ? 0 : (session[:per_page].to_i * (params[:page].to_i-1)) ) | |
# search.facet('timeline') { date :published_at } | |
# search.facet('sentiment') { terms :sentiment, :size => 10 } | |
# search.facet('authors') { terms :author, :size => 5 } unless params[:author] | |
# search.sort { published_at :desc } | |
# | |
# search.highlight highlight_options | |
# end | |
module MentionSearch | |
def main_query | |
lambda do |b| | |
b.must { |query| query.string "published_at:#{params[:published_at]}" } if params[:published_at] | |
b.must { |query| query.string "published_at:[#{Time.parse(@custom_date_range.date_from).to_s(:lucene)} TO #{Time.parse(@custom_date_range.date_to).end_of_day.to_s(:lucene)}]" } if params[:custom_date_range] | |
b.must { |query| query.string "hidden:#{ params[:hidden] ? 'true' : 'false' }" } | |
b.must { |query| query.string "sentiment:#{ params[:sentiment] }" } if params[:sentiment] | |
b.must { |query| query.string "author:\"#{params[:author]}\"" } unless params[:author].blank? | |
b.must { |query| query.string params[:q], :fields => ['title','content','author'] } unless params[:q].blank? | |
end | |
end | |
def sources_query | |
lambda do |b| | |
b.must { |query| query.string "(#{__sources_from_params.map { |s| "source:#{s}" }.join(" OR ")})" } | |
end unless __sources_from_params.empty? | |
end | |
# ... MORE QUERIES ... | |
private | |
def __keywords_from_params | |
case | |
when params[:keywords] then account.keywords.select { |k| params[:keywords].include? k.name } | |
when params[:topic] then account.topics.find(params[:topic]).keywords | |
else account.keywords | |
end | |
end | |
def __sources_from_params | |
category = account.categories.find(params[:category]) | |
sources = category ? category.sources : [] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment