]> git.cworth.org Git - sup/commitdiff
Merge branch 'parser-user-query-fix' into next
authorWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 18 May 2009 18:18:01 +0000 (14:18 -0400)
committerWilliam Morgan <wmorgan-sup@masanjin.net>
Mon, 18 May 2009 18:18:01 +0000 (14:18 -0400)
bin/sup-tweak-labels
lib/sup/index.rb
lib/sup/modes/search-results-mode.rb

index 8a8152d60b18db9c0a0dcfd45637e9070ece1293..538db8b38ac402604c6ef5b1f24475895ee7635d 100755 (executable)
@@ -81,19 +81,15 @@ begin
   end
   query += ' ' + opts[:query] if opts[:query]
 
-  qobj, opts = Redwood::Index.parse_user_query_string query
-  query = Redwood::Index.build_query opts.merge(:qobj => qobj)
-
-  results = index.ferret.search query, :limit => :all
-  num_total = results.total_hits
+  docs = Redwood::Index.run_query query
+  num_total = docs.size
 
   $stderr.puts "Found #{num_total} documents across #{source_ids.length} sources. Scanning..."
 
   num_changed = num_scanned = 0
   last_info_time = start_time = Time.now
-  results.hits.each do |hit|
+  docs.each do |id|
     num_scanned += 1
-    id = hit.doc
 
     m = index.build_message id
     old_labels = m.labels.clone
index 1c82e23adcdb80b042ca35e06a5c5358deda0954..d96efc85d1e6c66b08aa5270453f23c0713be497 100644 (file)
@@ -484,10 +484,27 @@ EOS
     @index_mutex.synchronize { @index.search(q, :limit => 1).total_hits > 0 }
   end
 
+  ## takes a user query string and returns the list of docids for messages
+  ## that match the query.
+  ##
+  ## messages can then be loaded from the index with #build_message.
+  ##
+  ## raises a ParseError if the parsing failed.
+  def run_query query
+    qobj, opts = Redwood::Index.parse_user_query_string query
+    query = Redwood::Index.build_query opts.merge(:qobj => qobj)
+    results = @index.search query, :limit => (opts[:limit] || :all)
+    results.hits.map { |hit| hit.doc }
+  end
+
 protected
 
-  ## do any specialized parsing
-  ## returns nil and flashes error message if parsing failed
+  class ParseError < StandardError; end
+
+  ## parse a query string from the user. returns a query object and a set of
+  ## extra flags; both of these are meant to be passed to #build_query.
+  ##
+  ## raises a ParseError if something went wrong.
   def parse_user_query_string s
     extraopts = {}
 
@@ -549,11 +566,9 @@ protected
     end
 
     if $have_chronic
-      chronic_failure = false
       subs = subs.gsub(/\b(before|on|in|during|after):(\((.+?)\)\B|(\S+)\b)/) do
-        break if chronic_failure
         field, datestr = $1, ($3 || $4)
-        realdate = Chronic.parse(datestr, :guess => false, :context => :past)
+        realdate = Chronic.parse datestr, :guess => false, :context => :past
         if realdate
           case field
           when "after"
@@ -567,11 +582,9 @@ protected
             "date:(<= #{sprintf "%012d", realdate.end.to_i}) date:(>= #{sprintf "%012d", realdate.begin.to_i})"
           end
         else
-          BufferManager.flash "Can't understand date #{datestr.inspect}!"
-          chronic_failure = true
+          raise ParseError, "can't understand date #{datestr.inspect}"
         end
       end
-      subs = nil if chronic_failure
     end
 
     ## limit:42 restrict the search to 42 results
@@ -581,15 +594,14 @@ protected
         extraopts[:limit] = lim.to_i
         ''
       else
-        BufferManager.flash "Can't understand limit #{lim.inspect}!"
-        subs = nil
+        raise ParseError, "non-numeric limit #{lim.inspect}"
       end
     end
     
-    if subs
+    begin
       [@qparser.parse(subs), extraopts]
-    else
-      nil
+    rescue Ferret::QueryParser::QueryParseException => e
+      raise ParseError, e.message
     end
   end
 
index 6fdc58a0c9da1f0df907ce8e29d786a36db9b4d7..227ee9ba7f7a469ed751a45a9bcc52a767c697b9 100644 (file)
@@ -32,8 +32,8 @@ class SearchResultsMode < ThreadIndexMode
       mode = SearchResultsMode.new qobj, extraopts
       BufferManager.spawn "search: \"#{short_text}\"", mode
       mode.load_threads :num => mode.buffer.content_height
-    rescue Ferret::QueryParser::QueryParseException => e
-      BufferManager.flash "Couldn't parse query."
+    rescue Index::ParseError => e
+      BufferManager.flash "Problem: #{e.message}!"
     end
   end
 end