Class: XapianFu::XapianDoc

Inherits:
Object show all
Defined in:
lib/xapian_fu/xapian_doc.rb

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

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 = {})
  @options = 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 = options[:weight] if options[:weight]
  @data = options[:data] if options[:data]
  @db = options[:xapian_db] if options[:xapian_db]
end

Instance Attribute Details

#dataObject (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

#dbObject

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

#fieldsObject (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

#idObject

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

#matchObject (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

#weightObject (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

#createObject

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

#inspectObject



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

#languageObject

Return this document's language which is set on initialize, inherited from the database or defaults to :english



232
233
234
235
236
237
238
239
240
241
242
243
244
245
# File 'lib/xapian_fu/xapian_doc.rb', line 232

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

#saveObject

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

#stemmerObject

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

#stopperObject

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

#termsObject

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_documentObject

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

#updateObject

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

#valuesObject

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_documentObject

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