[sup-talk] [PATCH] Add a limit:<int> search operator to limit the number of results

Nicolas Pouillard nicolas.pouillard at gmail.com
Sun Nov 16 12:17:59 EST 2008


---
 lib/sup/index.rb |   20 +++++++++++++++++---
 1 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/lib/sup/index.rb b/lib/sup/index.rb
index cda5cee..8af4edd 100644
--- a/lib/sup/index.rb
+++ b/lib/sup/index.rb
@@ -282,13 +282,15 @@ EOS
     query = build_query opts
     offset = 0
     while true
-      results = @index_mutex.synchronize { @index.search query, :sort => "date DESC", :limit => EACH_BY_DATE_NUM, :offset => offset }
+      limit = (opts[:limit])? [EACH_BY_DATE_NUM, opts[:limit] - offset].min : EACH_BY_DATE_NUM
+      results = @index_mutex.synchronize { @index.search query, :sort => "date DESC", :limit => limit, :offset => offset }
       Redwood::log "got #{results.total_hits} results for query (offset #{offset}) #{query.inspect}"
       results.hits.each do |hit|
         yield @index_mutex.synchronize { @index[hit.doc][:message_id] }, lambda { build_message hit.doc }
       end
-      break if offset >= results.total_hits - EACH_BY_DATE_NUM
-      offset += EACH_BY_DATE_NUM
+      break if opts[:limit] and offset >= opts[:limit] - limit
+      break if offset >= results.total_hits - limit
+      offset += limit
     end
   end
 
@@ -561,6 +563,18 @@ protected
       end
       subs = nil if chronic_failure
     end
+
+    ## limit:42 restrict the search to 42 results
+    subs = subs.gsub(/\blimit:(\S+)\b/) do
+      lim = $1
+      if lim =~ /^\d+$/
+        extraopts[:limit] = lim.to_i
+        ''
+      else
+        BufferManager.flash "Can't understand limit #{lim.inspect}!"
+        subs = nil
+      end
+    end
     
     if subs
       [@qparser.parse(subs), extraopts]
-- 
1.5.5.rc3



More information about the sup-talk mailing list