Skip to content

Commit

Permalink
[karmi#222] Added a prefix query
Browse files Browse the repository at this point in the history
  • Loading branch information
tobowers authored and karmi committed Jun 8, 2012
1 parent 9e1fa9d commit 995ea29
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 0 deletions.
8 changes: 8 additions & 0 deletions lib/tire/search/query.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,14 @@ def string(value, options={})
@value
end

def prefix(field, value, options={})
if options[:boost]
@value = { :prefix => { field => { :prefix => value, :boost => options[:boost] } } }
else
@value = { :prefix => { field => value } }
end
end

def custom_score(options={}, &block)
@custom_score ||= Query.new(&block)
@value[:custom_score] = options
Expand Down
43 changes: 43 additions & 0 deletions test/integration/prefix_query_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test_helper'

module Tire

class PrefixQueryTest < Test::Unit::TestCase
include Test::Integration

context "Prefix queries" do

should "search by a prefix" do
s = Tire.search('articles-test') do
query do
# "on" => "One"
prefix :title, "on"
end
end

assert_equal 1, s.results.size
assert_equal ['One'], s.results.map(&:title)
end

should "allow to specify boost" do
s = Tire.search('articles-test') do
query do
boolean do
# "on" => "One", boost it
should { prefix :title, "on", :boost => 2.0 }
should { all }
end
end
sort { by :_score }
end

assert_equal 5, s.results.size
assert_equal 'One', s.results.first.title

end

end

end

end
11 changes: 11 additions & 0 deletions test/unit/search_query_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,17 @@ class QueryTest < Test::Unit::TestCase
end
end

context "Prefix query" do
should "allow search for a prefix" do
assert_equal( { :prefix => { :user => "foo" } }, Query.new.prefix(:user, "foo") )
end

should "allow setting boost for prefix" do
assert_equal( { :prefix => {:user => {:prefix => "foo", :boost => 2.0 } } },
Query.new.prefix(:user, "foo", :boost => 2.0) )
end
end

end
end

Expand Down

0 comments on commit 995ea29

Please sign in to comment.