Permalink
Browse files

allow ranges and other complex conditions inside an array of conditio…

…ns by recursively calling where_query when an array is used
  • Loading branch information...
ryanb committed Jan 17, 2012
1 parent c4a8b1d commit 0ed966b8458e41765f5c29b375392e3f3eeda43a
Showing with 10 additions and 4 deletions.
  1. +5 −4 lib/xapit/server/query.rb
  2. +5 −0 spec/xapit/server/query_spec.rb
@@ -176,7 +176,7 @@ def similar_to(data)
merge(:not, ["Q#{data[:class]}-#{data[:id]}"])
end
- def where_query(conditions)
+ def where_query(conditions, operator = :and)
queries = []
terms = []
conditions.each do |name, value|
@@ -193,14 +193,15 @@ def where_query(conditions)
end
end
elsif value.kind_of?(Array)
- queries << Xapian::Query.new(xapian_operator(:or), value.map { |v| "X#{name}-#{v.to_s.downcase}" })
+ array_conditions = value.map { |v| [name, v] }
+ queries << where_query(array_conditions, :or)
else
terms << "X#{name}-#{value.to_s.downcase}"
end
end
- queries << Xapian::Query.new(xapian_operator(:and), terms) unless terms.empty?
+ queries << Xapian::Query.new(xapian_operator(operator), terms) unless terms.empty?
queries.inject(queries.shift) do |merged_query, query|
- Xapian::Query.new(xapian_operator(:and), merged_query, query)
+ Xapian::Query.new(xapian_operator(operator), merged_query, query)
end
end
@@ -76,6 +76,11 @@
query.records.map { |r| r[:id] }.should eq(%w[1 2 3])
end
+ it "fetches results matching multiple :from and :to" do
+ query = Xapit::Server::Query.new([{:where => {:priority => [{:from => 1, :to => 2}, {:from => 4, :to => 5}]}}])
+ query.records.map { |r| r[:id] }.should eq(%w[1 2 4 5])
+ end
+
it "fetches results matching exact terms" do
query = Xapit::Server::Query.new([{:all_terms => ["Xpriority-1"]}])
query.records.map { |r| r[:id] }.should eq(%w[1])

0 comments on commit 0ed966b

Please sign in to comment.