Permalink
Browse files

see if that search is much faster...

git-svn-id: http://store.sabretechllc.com/public/jonmagic/optik/trunk@293 331dd862-6008-0410-a02f-b57fe9264243
  • Loading branch information...
1 parent 89ca104 commit 05ca83d6381b3ac4481ce080e6156e8dfb77f626 dcparker committed Nov 8, 2007
Showing with 13 additions and 20 deletions.
  1. +1 −5 app/controllers/tickets_controller.rb
  2. +11 −14 app/models/ticket.rb
  3. +1 −1 app/views/tickets/_search.rhtml
@@ -44,13 +44,9 @@ def statistics
def search
@query = params[:query] || request.raw_post || request.query_string
- @result_pages = Paginator.new self, Ticket.quicksearch(@query, :count => true), 50, @params['page']
- @tickets = Ticket.quicksearch(@query,
- :limit => @result_pages.items_per_page,
- :offset => @result_pages.current.offset)
+ @tickets = Ticket.fullsearch(@query, :limit => 50)
if @tickets.blank?
flash[:notice] = 'No results found'
- else
end
@page_title = 'Search Results'
end
View
@@ -4,21 +4,19 @@ class Ticket < ActiveRecord::Base
belongs_to :state
has_many :notes
- def self.quicksearch(query, options = {})
+ def self.fullsearch(query, options = {})
if !query.to_s.strip.empty?
tokens = query.split
resultshash = {}
- tokens.each do |token|
- Ticket.find_by_sql(["SELECT tickets.* FROM tickets WHERE LOWER(tickets.description) LIKE ?", '%'+token.downcase+'%']).each do |result|
- resultshash[result.id] ||= [0, nil]
- resultshash[result.id][0] += 1
- resultshash[result.id][1] = result
- end
- Note.find_by_sql(["SELECT notes.* FROM notes WHERE notes.content LIKE ?", '%'+token.downcase+'%']).each do |result|
- resultshash[result.ticket_id] ||= [0, nil]
- resultshash[result.ticket_id][0] += 1
- resultshash[result.ticket_id][1] ||= Ticket.find(result.ticket_id)
- end
+ Ticket.find_by_sql(["SELECT tickets.* FROM tickets WHERE 0" + (" OR LOWER(tickets.description) LIKE ?") * tokens.length] + tokens.map {|t| '%'+t.downcase+'%'}).each do |result|
+ resultshash[result.id] ||= [0, nil]
+ resultshash[result.id][0] += 1
+ resultshash[result.id][1] = result
+ end
+ Note.find_by_sql(["SELECT notes.* FROM notes WHERE 0" + (" OR LOWER(notes.content) LIKE ?") * tokens.length] + tokens.map {|t| '%'+t.downcase+'%'}).each do |result|
+ resultshash[result.ticket_id] ||= [0, nil]
+ resultshash[result.ticket_id][0] += 1
+ resultshash[result.ticket_id][1] ||= Ticket.find(result.ticket_id)
end
Ticket.find_tagged_with(options.merge(:any => tokens)).each do |result|
resultshash[result.id] ||= [0, nil]
@@ -28,13 +26,12 @@ def self.quicksearch(query, options = {})
results = resultshash.keys.collect {|k| resultshash[k][1]}.sort {|b,a| resultshash[a.id][0] <=> resultshash[b.id][0] || a.updated_at <=> b.updated_at}
offset = options.delete(:offset) || 0
limit = options.delete(:limit) || 100000
- return options.delete(:count) ? resultshash.keys.length : results[offset..(offset+limit)]
+ return results[offset..(offset+limit)]
else
[]
end
end
-
# def self.search(query)
# if !query.to_s.strip.empty?
# tokens = query.split.collect {|c| "%#{c.downcase}%"}
@@ -3,7 +3,7 @@
<%= start_form_tag :action => 'search' %>
<table class="addhorpadding">
<tr>
- <td><%= text_field_tag 'query', "", "accesskey" => "f" %></td>
+ <td><%= text_field_tag 'query', params[:query], "accesskey" => "f" %></td>
<td><%= submit_tag "Search" %></td>
</tr>
</table>

0 comments on commit 05ca83d

Please sign in to comment.