Browse files

Add ability to pass a `PostingSource` when searching.

  • Loading branch information...
1 parent f1fd710 commit 5d8a4bc4ef748c696378656de2546ad72afb5c28 @djanowski djanowski committed Jul 24, 2012
Showing with 45 additions and 0 deletions.
  1. +2 −0 CHANGELOG.rdoc
  2. +5 −0 lib/xapian_fu/xapian_db.rb
  3. +38 −0 spec/xapian_db_spec.rb
View
2 CHANGELOG.rdoc
@@ -1,5 +1,7 @@
=== 1.5.1 (unreleased)
+* Add ability to pass a `PostingSource` when searching.
+
* Add ability to specify how many documents to sample when searching.
This allows for accurate paging links, facet counts, etc.
View
5 lib/xapian_fu/xapian_db.rb
@@ -280,6 +280,11 @@ def search(q, options = {})
accum
end
end
+
+ if options.include?(:posting_source)
+ query = Xapian::Query.new(Xapian::Query::OP_AND_MAYBE, query, Xapian::Query.new(options[:posting_source]))
+ end
+
enquiry.query = query
ResultSet.new(:mset => enquiry.mset(offset, per_page, check_at_least),
View
38 spec/xapian_db_spec.rb
@@ -775,4 +775,42 @@
xdb.search("language").map(&:id).should == [2, 1, 3]
end
end
+
+ describe "posting sources" do
+ class BoostLatest < Xapian::PostingSource
+ attr :docid
+
+ def init(db)
+ @db = db
+ @iter = db.postlist("").map(&:docid).each
+ @docid = @iter.next
+ end
+
+ def weight
+ @docid * 10
+ end
+
+ def next(minweight)
+ @docid = @iter.next
+ rescue StopIteration
+ @docid = nil
+ end
+
+ def at_end
+ @docid.nil?
+ end
+ end
+
+ it "allows to pass a custom posting source to boost results" do
+ xdb = XapianDb.new
+
+ xdb << {:id => 1, :name => "Foo"}
+ xdb << {:id => 2, :name => "Foo Bar"}
+ xdb << {:id => 3, :name => "Foo Bar Baz"}
+
+ xdb.search("foo").map(&:id).should == [1, 2, 3]
+
+ xdb.search("foo", :posting_source => BoostLatest.new).map(&:id).should == [3, 2, 1]
+ end
+ end
end

0 comments on commit 5d8a4bc

Please sign in to comment.