Class: XapianFu::XapianDoc
Overview
A XapianDoc represents a document in a XapianDb. Searches return XapianDoc objects and they are used internally when adding new documents to the database. You usually don’t need to instantiate them yourself unless you’re doing something a bit advanced.
Constant Summary collapse
- STOPPER_STRATEGIES =
{ :none => 0, :all => 1, :stemmed => 2 }
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The arbitrary data stored in the Xapian database with this document.
-
#db ⇒ Object
The XapianDb object that this document was retrieved from, or should be stored in.
-
#fields ⇒ Object
readonly
A hash of the fields given to this object on initialize.
-
#id ⇒ Object
The unsigned integer “primary key” for this document in the Xapian database.
-
#match ⇒ Object
readonly
The Xapian::Match object for this document when returned as part of a search result.
-
#weight ⇒ Object
readonly
The search score of this document when returned as part of a search result.
Instance Method Summary collapse
-
#==(b) ⇒ Object
Compare IDs with another XapianDoc.
-
#create ⇒ Object
Add this document to the Xapian Database.
-
#initialize(doc, options = {}) ⇒ XapianDoc
constructor
Expects a Xapian::Document, a Hash-like object, or anything that with a to_s method.
- #inspect ⇒ Object
-
#language ⇒ Object
Return this document’s language which is set on initialize, inherited from the database or defaults to :english.
-
#save ⇒ Object
Add this document to the Xapian Database, or replace it if it already has an id.
-
#stemmer ⇒ Object
Return the stemmer for this document.
-
#stemmer=(s) ⇒ Object
Set the stemmer to use for this document.
-
#stopper ⇒ Object
Return the stopper for this document.
- #stopper_strategy ⇒ Object
-
#terms ⇒ Object
Return a list of terms that the db has for this document.
-
#to_xapian_document ⇒ Object
Return a Xapian::Document ready for putting into a Xapian database.
-
#update ⇒ Object
Update this document in the Xapian Database.
-
#values ⇒ Object
The XapianFu::XapianDocValueAccessor for accessing the values in this document.
-
#xapian_document ⇒ Object
The Xapian::Document for this XapianFu::Document.
Constructor Details
#initialize(doc, options = {}) ⇒ XapianDoc
Expects a Xapian::Document, a Hash-like object, or anything that with a to_s method. Anything else raises a XapianTypeError. The :weight option sets the search weight when setting up search results. The :data option sets some additional data to be stored with the document in the database. The :xapian_db option sets the XapianDb to allow saves and term enumeration.
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/xapian_fu/xapian_doc.rb', line 73 def initialize(doc, = {}) @options = @fields = {} if doc.is_a? Xapian::Match match = doc doc = match.document @match = match @weight = @match.weight end # Handle initialisation from a Xapian::Document, which is # usually a search result from a Xapian database if doc.is_a?(Xapian::Document) @xapian_document = doc @id = doc.docid # Handle initialisation from a hash-like object elsif doc.respond_to?(:has_key?) and doc.respond_to?("[]") @fields = doc @id = doc[:id] if doc.has_key?(:id) # Handle initialisation from an object with a to_xapian_fu_string method elsif doc.respond_to?(:to_xapian_fu_string) @fields = { :content => doc.to_xapian_fu_string } # Handle initialisation from anything else that can be coerced # into a string elsif doc.respond_to? :to_s @fields = { :content => doc.to_s } else raise XapianTypeError, "Can't handle indexing a '#{doc.class}' object" end @weight = [:weight] if [:weight] @data = [:data] if [:data] @db = [:xapian_db] if [:xapian_db] end |
Instance Attribute Details
#data ⇒ Object (readonly)
The arbitrary data stored in the Xapian database with this document. Returns an empty string if none available.
48 49 50 |
# File 'lib/xapian_fu/xapian_doc.rb', line 48 def data @data end |
#db ⇒ Object
The XapianDb object that this document was retrieved from, or should be stored in.
64 65 66 |
# File 'lib/xapian_fu/xapian_doc.rb', line 64 def db @db end |
#fields ⇒ Object (readonly)
A hash of the fields given to this object on initialize
44 45 46 |
# File 'lib/xapian_fu/xapian_doc.rb', line 44 def fields @fields end |
#id ⇒ Object
The unsigned integer “primary key” for this document in the Xapian database.
60 61 62 |
# File 'lib/xapian_fu/xapian_doc.rb', line 60 def id @id end |
#match ⇒ Object (readonly)
The Xapian::Match object for this document when returned as part of a search result.
56 57 58 |
# File 'lib/xapian_fu/xapian_doc.rb', line 56 def match @match end |
#weight ⇒ Object (readonly)
The search score of this document when returned as part of a search result
52 53 54 |
# File 'lib/xapian_fu/xapian_doc.rb', line 52 def weight @weight end |
Instance Method Details
#==(b) ⇒ Object
Compare IDs with another XapianDoc
149 150 151 152 153 154 155 |
# File 'lib/xapian_fu/xapian_doc.rb', line 149 def ==(b) if b.is_a?(XapianDoc) id == b.id && (db == b.db || db.dir == b.db.dir) else super(b) end end |
#create ⇒ Object
Add this document to the Xapian Database
171 172 173 |
# File 'lib/xapian_fu/xapian_doc.rb', line 171 def create self.id = db.rw.add_document(to_xapian_document) end |
#inspect ⇒ Object
157 158 159 160 161 162 |
# File 'lib/xapian_fu/xapian_doc.rb', line 157 def inspect s = ["<#{self.class.to_s} id=#{id}"] s << "weight=%.5f" % weight if weight s << "db=#{db.nil? ? 'nil' : db}" s.join(' ') + ">" end |
#language ⇒ Object
Return this document’s language which is set on initialize, inherited from the database or defaults to :english
253 254 255 256 257 258 259 260 261 262 263 264 265 266 |
# File 'lib/xapian_fu/xapian_doc.rb', line 253 def language if @language @language else @language = if ! @options[:language].nil? @options[:language] elsif db and db.language db.language else :english end end end |
#save ⇒ Object
Add this document to the Xapian Database, or replace it if it already has an id.
166 167 168 |
# File 'lib/xapian_fu/xapian_doc.rb', line 166 def save id ? update : create end |
#stemmer ⇒ Object
Return the stemmer for this document. If not set on initialize by the :stemmer or :language option, it will try the database’s stemmer and otherwise defaults to an English stemmer.
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 |
# File 'lib/xapian_fu/xapian_doc.rb', line 191 def stemmer if @stemmer @stemmer else @stemmer = if ! @options[:stemmer].nil? @options[:stemmer] elsif @options[:language] @options[:language] elsif db db.stemmer else :english end @stemmer = StemFactory.stemmer_for(@stemmer) end end |
#stemmer=(s) ⇒ Object
Set the stemmer to use for this document. Accepts any string that the Xapian::Stem class accepts (Either the English name for the language or the two letter ISO639 code). Can also be an existing Xapian::Stem object.
184 185 186 |
# File 'lib/xapian_fu/xapian_doc.rb', line 184 def stemmer=(s) @stemmer = StemFactory.stemmer_for(s) end |
#stopper ⇒ Object
Return the stopper for this document. If not set on initialize by the :stopper or :language option, it will try the database’s stopper and otherwise default to an English stopper..
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 |
# File 'lib/xapian_fu/xapian_doc.rb', line 212 def stopper if @stopper @stopper else @stopper = if ! @options[:stopper].nil? @options[:stopper] elsif @options[:language] @options[:language] elsif db db.stopper else :english end @stopper = StopperFactory.stopper_for(@stopper) end end |
#stopper_strategy ⇒ Object
236 237 238 239 240 241 242 243 244 245 246 247 248 249 |
# File 'lib/xapian_fu/xapian_doc.rb', line 236 def stopper_strategy if @stopper_strategy @stopper_strategy else @stopper_strategy = if ! @options[:stopper_strategy].nil? @options[:stopper_strategy] elsif db db.stopper_strategy else :stemmed end end end |
#terms ⇒ Object
Return a list of terms that the db has for this document.
121 122 123 124 |
# File 'lib/xapian_fu/xapian_doc.rb', line 121 def terms raise XapianFu::XapianDbNotSet unless db db.ro.termlist(id) if db.respond_to?(:ro) and db.ro and id end |
#to_xapian_document ⇒ Object
Return a Xapian::Document ready for putting into a Xapian database. Requires that the db attribute has been set up.
128 129 130 131 132 133 134 135 136 137 138 |
# File 'lib/xapian_fu/xapian_doc.rb', line 128 def to_xapian_document raise XapianFu::XapianDbNotSet unless db xapian_document.data = data # Clear and add values xapian_document.clear_values add_values_to_xapian_document # Clear and add terms xapian_document.clear_terms generate_terms xapian_document end |
#update ⇒ Object
Update this document in the Xapian Database
176 177 178 |
# File 'lib/xapian_fu/xapian_doc.rb', line 176 def update db.rw.replace_document(id, to_xapian_document) end |
#values ⇒ Object
The XapianFu::XapianDocValueAccessor for accessing the values in this document.
116 117 118 |
# File 'lib/xapian_fu/xapian_doc.rb', line 116 def values @value_accessor ||= XapianDocValueAccessor.new(self) end |
#xapian_document ⇒ Object
The Xapian::Document for this XapianFu::Document. If this document was retrieved from a XapianDb then this will have been initialized by Xapian, otherwise a new Xapian::Document.new is allocated.
144 145 146 |
# File 'lib/xapian_fu/xapian_doc.rb', line 144 def xapian_document @xapian_document ||= Xapian::Document.new end |