Skip to content

Commit

Permalink
Set the XapianDoc id after adding to the XapianDb
Browse files Browse the repository at this point in the history
Support passing a string to XapianDoc.new

Improve initialization of XapianDoc
  • Loading branch information
johnl committed Mar 27, 2009
1 parent fa5d279 commit 792ac5d
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 17 deletions.
16 changes: 13 additions & 3 deletions lib/xapian_fu/xapian_db.rb
Expand Up @@ -56,11 +56,21 @@ def add_doc(doc)
else
fields = { :content => doc.to_s }
end

content = fields.keys.collect { |k| fields[k] }.join(' ')

if fields.respond_to?(:keys)
content = fields.keys.collect { |k| fields[k] }.join(' ')
else
content = fields.to_s
end

tg.index_text( content )
rw.add_document(xdoc)
xdoc_id = rw.add_document(xdoc)
if doc.is_a? XapianDoc
doc.id = xdoc_id
doc
else
xdoc
end
end
alias_method "<<", :add_doc

Expand Down
27 changes: 17 additions & 10 deletions lib/xapian_fu/xapian_doc.rb
@@ -1,21 +1,28 @@
module XapianFu
class XapianDoc
attr_reader :fields, :data, :id
def initialize(fields, options = {})
if fields.is_a?(Xapian::Document)
attr_reader :fields, :data, :weight, :db
attr_accessor :id
def initialize(doc, options = {})
if doc.is_a?(Xapian::Document)
@id = doc.docid
# @weight = doc.weight
begin
xdoc = fields
@data = Marshal::load(xdoc.data) unless xdoc.data.empty?
@id = xdoc.docid
@data = Marshal::load(doc.data) unless doc.data.empty?
rescue ArgumentError
@data = nil
end
elsif doc.respond_to?("[]")
@fields = doc
@id = doc[:id] if doc.respond_to?(:has_key?) and doc.has_key?(:id)
else
@fields = fields
@id = fields[:id] if fields.has_key?(:id)
@weight = options[:weight] if options[:weight]
@data = options[:data] if options[:data]
@fields = { :content => doc.to_s }
end
@weight = options[:weight] if options[:weight]
@data = options[:data] if options[:data]
end

def terms
db.terms(id) if db and id
end
end
end
21 changes: 17 additions & 4 deletions spec/index_spec.rb
Expand Up @@ -37,8 +37,10 @@
it "should index a string" do
xdb = XapianDb.new
xdb << "once upon a time"
xdb.flush
xdb.size.should == 1
xdb << XapianDoc.new("once upon a time")
xdb.size.should == 2

end

it "should retrieve documents like an array and return a XapianDoc" do
Expand All @@ -50,18 +52,29 @@

it "should provide the id of retrieved documents" do
xdb = XapianDb.new
xdb << "once upton a time"
xdb.flush
xdb << "once upon a time"
xdb.documents[1].id.should == 1
end

it "should store data in the database" do
xdb = XapianDb.new
xdb << XapianDoc.new({ :text => "once upon a time" }, :data => { :thing => 0xdeadbeef })
xdb.flush
xdb.size.should == 1
doc = xdb.documents[1]
doc.data.should == { :thing => 0xdeadbeef }
end

it "should return a XapianDoc with an id after indexing" do
xdb = XapianDb.new
doc = XapianDoc.new("once upon a time")
doc.id.should == nil
new_doc = xdb << doc
new_doc.id.should == 1
end

it "should tokenize documents" do
xdb = XapianDb.new
xdb << XapianDoc.new("once upon a time")
end

end

0 comments on commit 792ac5d

Please sign in to comment.