class XapianFu::XapianDocumentsAccessor
A XapianDocumentsAccessor
is used to provide the XapianDb#documents
interface. It is usually set up by a XapianDb
so you shouldn't need to set up your own.
Public Instance Methods
Return the document with the given id from the database or nil if it doesn't exist
# File lib/xapian_fu/xapian_documents_accessor.rb 49 def [](doc_id) 50 find(doc_id) 51 rescue XapianFu::DocNotFound 52 nil 53 end
Add a document to the index. A document can be just a hash, the keys representing field names and their values the data to be indexed. Or it can be a XapianDoc
, or any object with a to_s method.
If the document has an :id field, it is used as the primary key in the Xapian database.
If the document object reponds to the method :data, whatever it returns is marshalled and stored in the Xapian database. Any arbitrary data up to Xmeg can be stored here.
Currently, all fields are stored in the database. This will change to store only those fields requested to be stored.
# File lib/xapian_fu/xapian_documents_accessor.rb 29 def add(doc) 30 doc = XapianDoc.new(doc) unless doc.is_a? XapianDoc 31 doc.db = @xdb 32 doc.save 33 doc 34 end
Delete the given document from the database and return the document id, or nil if it doesn't exist
# File lib/xapian_fu/xapian_documents_accessor.rb 57 def delete(doc) 58 if doc.respond_to?(:to_i) 59 @xdb.rw.delete_document(doc.to_i) 60 doc.to_i 61 end 62 rescue RuntimeError => e 63 raise e unless e.to_s =~ /^DocNotFoundError/ 64 end
Return the document with the given id from the database. Raises a XapianFu::DocNotFoundError exception if it doesn't exist.
# File lib/xapian_fu/xapian_documents_accessor.rb 40 def find(doc_id) 41 xdoc = @xdb.ro.document(doc_id) 42 XapianDoc.new(xdoc, :xapian_db => @xdb) 43 rescue RuntimeError => e 44 raise e.to_s =~ /^DocNotFoundError/ ? XapianFu::DocNotFound : e 45 end
Return the document with the highest value in the specified field or nil if it doesn't exist
# File lib/xapian_fu/xapian_documents_accessor.rb 67 def max(key = :id) 68 if key == :id 69 # for :id we can use lastdocid 70 find(@xdb.ro.lastdocid) rescue nil 71 else 72 # for other values, we do a search ordered by that key in descending order 73 query = Xapian::Query.new(Xapian::Query::OP_VALUE_GE, XapianDocValueAccessor.value_key(key), "0") 74 e = Xapian::Enquire.new(@xdb.ro) 75 e.query = query 76 e.sort_by_value!(XapianDocValueAccessor.value_key(key), true) 77 r = e.mset(0, 1).matches.first 78 find(r.docid) rescue nil 79 end 80 end
Build a new XapianDoc
for this database
# File lib/xapian_fu/xapian_documents_accessor.rb 11 def new(doc = nil, options = { }) 12 options = options.merge({ :xapian_db => @xdb }) 13 XapianDoc.new(doc, options) 14 end