Maybe this will be helpful? It's not very well tested (I'm working on
the whole BDD thing), but it provides a way to let your users save
searches. I instantiate one of these every time a search is made
regardless of whether or not a user saves it - with a few
modifications you may be able to get the sort of behavior you want
without resorting to alias_method
class SearchQuery < ActiveRecord::Base
belongs_to :owner #class_name whatever
serialize :details, Hash
validates_presence_of :owner, :title
def self.select_options(id)
self.find_all_by_user_id(id).map do |query|
[query.title, query.id]
end
end
# key:value,value|key:value
def deserialize
@_deserialize ||= make_struct
end
def self.options_for_select(user, blank=false, blank_option='')
o = find(:all, :conditions => ['user_id = ?',user])
o = o.collect{ |c| [c.title, c.id] }
o.insert(0,[blank_option,'']) if blank
return o
end
def self.build(p = {}) # p = params from a controller.
q = { # this hash represents a query
:model => "member", # the model to search in camelcase
:keywords => "", # "keywords for search"
:conditions => {}, # { :attrib => "value" }
:date_range => [], # [ "11-17", "18-22" ]
:per_page => 50, # these values
:page => 1, # all just have
:order => "created_at DESC" # reasonable defaults.
} # To further generalize, :sort_by ought to be a SQL string.
q[:model] = p[:model] unless p[:model].blank?
q[:keywords] = p[:keywords] unless p[:keywords].blank?
unless p[:conditions].nil?
q[:conditions].merge!(p[:conditions]).symbolize_keys!.delete_if
{ |k,v| v.length == 0 }
end
q[:date_range] = p[:date_range] unless p[:date_range].blank?
q[:per_page] = p[:per_page] unless p[:per_page].nil?
q[:page] = p[:page] unless p[:page].nil?
q[:order] = p[:order] unless p[:order].nil?
return new(:details => q)
end
def execute
return [] if self.details.blank?
q = self.details
m = q[:model].camelize.constantize
Merb.logger.info("Sphinx: Searching for #{q[:model]}s")
Merb.logger.info("Sphinx: Sort Order: #{q[:order]}")
Merb.logger.info("Sphinx: Query: #{q.inspect}")
if not q[:date_range].blank?
r = m.date_range(q[:date_range].to_date_range)
r = r.search q[:keywords],
:conditions => q[:conditions],
:order => q[:order]
r = r.compact!
r = r.paginate :page => q[:page],
:per_page => q[:per_page]
else
r = m.search q[:keywords],
:conditions => q[:conditions],
:page => q[:page],
:per_page => q[:per_page],
:order => q[:order]
end
return r
end
private
def make_struct
v = value1.split('|')
hash = {}
v.each do |x|
k,v = x.split(":")
hash[k] = v.split(",")
end
OpenStruct.new(hash)
end
end
On Tue, Jan 27, 2009 at 12:00 AM, Pat Allan <[email protected]> wrote:
>
> If you get the alias happening, then you can do whatever you want with
> the query information :)
>
> --
> Pat
>
> On 27/01/2009, at 1:04 PM, Tan YL wrote:
>
> >
> > What if I log the quries into the database everytime a person
> > searches for
> > something?
> >
> > like everytime the search a word I tried to find the word in the
> > database
> > then if the db can't find it creates it and if it finds it, it
> > increments
> > the value up by one?
> >
> > -----Original Message-----
> > From: [email protected]
> > [mailto:[email protected]] On Behalf Of Pat Allan
> > Sent: Tuesday, January 27, 2009 1:28 PM
> > To: [email protected]
> > Subject: [ts] Re: How do i log the search results of thinking sphinx
> >
> >
> > You could use alias_method to track queries yourself... it's not quite
> > so elegant, though, and you'd need to hook the current user in
> > somehow.
> >
> > It's definitely not something built into Thinking Sphinx, but I'm sure
> > it's possible.
> >
> > I'm not sure of the best way to alias class-level methods - the
> > following code is for instance-level methods, but
> > ThinkingSphinx::Search.search is class-level, so this WILL NOT work.
> > It may provide some inspiration though.
> >
> > module SphinxLogger
> > def self.included(base)
> > base.class_eval do
> > alias_method :search_without_logging, :search
> >
> > def search(*args)
> > # Pull options out of the array
> > options = args.extract_options!
> >
> > query = args.join(" ")
> > # log your query here...
> >
> > # Push options back into the array
> > args << options
> > # Run the actual query in the original code
> > search_without_logging(*args)
> > end
> > end
> > end
> > end
> >
> > ThinkingSphinx::Search.send(:include, SphinxLogger)
> >
> > --
> > Pat
> >
> > On 22/01/2009, at 11:11 PM, tyliong wrote:
> >
> >>
> >> Hi,
> >>
> >> is it possible for TS to log the search query of every user in a
> >> database and update the number of times the search term was used by
> >> that particular user or any user?
> >>>
> >
> >
> >
> >
> > >
>
>
> >
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---