From 9031760dc98fc879291bb9ae9502fc66ff41270d Mon Sep 17 00:00:00 2001 From: wmorgan Date: Wed, 27 Dec 2006 17:39:07 +0000 Subject: [PATCH] improved user query handling git-svn-id: svn://rubyforge.org/var/svn/sup/trunk@100 5c8cc53c-5e98-4d25-b20a-d8db53a31250 --- bin/sup | 16 ++++++++++++---- lib/sup/index.rb | 29 +++++++++++++++++------------ 2 files changed, 29 insertions(+), 16 deletions(-) diff --git a/bin/sup b/bin/sup index c0ea097..708e76e 100644 --- a/bin/sup +++ b/bin/sup @@ -153,10 +153,18 @@ begin when :search text = bm.ask :search, "query: " next unless text && text !~ /^\s*$/ - short_text = text.length < 20 ? text : text[0 ... 20] + "..." - mode = SearchResultsMode.new text - bm.spawn "search: \"#{short_text}\"", mode - mode.load_more_threads mode.buffer.content_height + + begin + qobj = Index.parse_user_query_string text + short_text = text.length < 20 ? text : text[0 ... 20] + "..." + log "built query from #{text.inspect}: #{qobj}" + mode = SearchResultsMode.new qobj + bm.spawn "search: \"#{short_text}\"", mode + mode.load_more_threads mode.buffer.content_height + rescue Ferret::QueryParser::QueryParseException => e + bm.flash "Couldn't parse query." + end + when :list_labels b = bm.spawn_unless_exists("Label List") { LabelListMode.new } b.mode.load_in_background diff --git a/lib/sup/index.rb b/lib/sup/index.rb index 88345d2..e321161 100644 --- a/lib/sup/index.rb +++ b/lib/sup/index.rb @@ -2,8 +2,8 @@ require 'thread' require 'fileutils' -require 'ferret' -#require_gem 'ferret', ">= 0.10.13" +#require 'ferret' +require_gem 'ferret', ">= 0.10.13" module Redwood @@ -23,9 +23,9 @@ class Index def initialize dir=BASE_DIR @dir = dir - @mutex = Mutex.new @sources = {} @sources_dirty = false + @qparser ||= Ferret::QueryParser.new :default_field => :body, :analyzer => Ferret::Analysis::WhiteSpaceAnalyzer.new self.class.i_am_the_instance self end @@ -112,7 +112,7 @@ class Index ## in scotland, frikkin' huuuge. EACH_BY_DATE_NUM = 100 def each_id_by_date opts={} - return if @index.size == 0 # otherwise ferret barfs + return if @index.size == 0 # otherwise ferret barfs ###TODO: remove this once my ferret patch is accepted query = build_query opts offset = 0 while true @@ -308,19 +308,24 @@ EOS protected - ## TODO: convert this to query objects rather than strings + def parse_user_query_string str; @qparser.parse str; end def build_query opts + + query = Ferret::Search::BooleanQuery.new + query.add_query opts[:qobj], :must if opts[:qobj] labels = ([opts[:label]] + (opts[:labels] || [])).compact - query = "" - query += labels.map { |t| "+label:#{t}" }.join(" ") - query += " #{opts[:content]}" if opts[:content] + labels.each { |t| query.add_query Ferret::Search::TermQuery.new("label", t.to_s), :must } if opts[:participants] - query += "+(" + - opts[:participants].map { |p| "from:#{p.email} OR to:#{p.email}" }.join(" OR ") + ")" + q2 = Ferret::Search::BooleanQuery.new + opts[:participants].each do |p| + q2.add_query Ferret::Search::TermQuery.new("from", p.email), :should + q2.add_query Ferret::Search::TermQuery.new("to", p.email), :should + end + query.add_query q2, :must end - query += " -label:spam" unless opts[:load_spam] || labels.include?(:spam) - query += " -label:killed" unless opts[:load_killed] || labels.include?(:killed) + query.add_query Ferret::Search::TermQuery.new("label", "spam"), :must_not unless opts[:load_spam] || labels.include?(:spam) + query.add_query Ferret::Search::TermQuery.new("label", "killed"), :must_not unless opts[:load_killed] || labels.include?(:killed) query end -- 2.45.2