class XapianDb::Database

Base class for a Xapian database

Attributes

reader[R]

Public Instance Methods

delete_doc_with_unique_term(term) click to toggle source

Delete a document identified by a unique term; this method is used by the orm adapters @param [String] term A term that uniquely identifies a document

   # File lib/xapian_db/database.rb
33 def delete_doc_with_unique_term(term)
34   writer.delete_document("Q#{term}")
35   true
36 end
delete_docs_of_class(klass) click to toggle source

Delete all docs of a specific class.

If ‘klass` tracks its descendants, then docs of any subclasses will be deleted, too. (ActiveRecord does this by default; the gem ’descendants_tracker’ offers an alternative.)

@param [Class] klass A class that has a {XapianDb::DocumentBlueprint} configuration

   # File lib/xapian_db/database.rb
44 def delete_docs_of_class(klass)
45   writer.delete_document("C#{klass}")
46   if klass.respond_to? :descendants
47     klass.descendants.each do |subclass|
48       writer.delete_document("C#{subclass}")
49     end
50   end
51   true
52 end
facets(attribute, expression) click to toggle source

A very simple implementation of facets using Xapian collapse key. @param [Symbol, String] attribute the name of an attribute declared in one ore more blueprints @param [String] expression A valid search expression (see {#search} for examples). @return [Hash<Class, Integer>] A hash containing the classes and the hits per class

    # File lib/xapian_db/database.rb
156 def facets(attribute, expression)
157  # return an empty hash if no search expression is given
158   return {} if expression.nil? || expression.strip.empty?
159   value_number         = XapianDb::DocumentBlueprint.value_number_for(attribute)
160   @query_parser        ||= QueryParser.new(self)
161   query                = @query_parser.parse(expression)
162   enquiry              = Xapian::Enquire.new(reader)
163   enquiry.query        = query
164   enquiry.collapse_key = value_number
165   facets = {}
166   enquiry.mset(0, size).matches.each do |match|
167     facet_value = match.document.value(value_number)
168     # We must add 1 to the collapse_count since collapse_count means
169     # "how many other matches are there?"
170     facets[facet_value] = match.collapse_count + 1
171   end
172   facets
173 end
find_similar_to(docs, options={}) click to toggle source

Find documents that are similar to one or more reference documents. It is basically the implementation of this suggestion: trac.xapian.org/wiki/FAQ/FindSimilar @param [Array<Xapian::Document> or Xapian::Document] docs One or more reference docs @param [Hash] options query options @option options [Class] :class an indexed class; if a class is passed, the result will

contain objects of this class only

@return [XapianDb::Resultset] The resultset

    # File lib/xapian_db/database.rb
128 def find_similar_to(docs, options={})
129   docs = [docs].flatten
130   reference = Xapian::RSet.new
131   docs.each { |doc| reference.add_document doc.docid }
132   pk_terms    = docs.map { |doc| "Q#{doc.data}" }
133   class_terms = docs.map { |doc| "C#{doc.indexed_class}" }
134 
135   relevant_terms = Xapian::Enquire.new(reader).eset(40, reference).terms.map {|e| e.name } - pk_terms - class_terms
136   relevant_terms.reject! { |term| term =~ /INDEXED_CLASS/ }
137 
138   reference_query = Xapian::Query.new Xapian::Query::OP_OR, pk_terms
139   terms_query     = Xapian::Query.new Xapian::Query::OP_OR, relevant_terms
140   final_query     = Xapian::Query.new Xapian::Query::OP_AND_NOT, terms_query, reference_query
141   if options[:class]
142     class_scope = "indexed_class:#{options[:class].name.downcase}"
143     @query_parser ||= QueryParser.new(self)
144     class_query   = @query_parser.parse(class_scope)
145     final_query   = Xapian::Query.new Xapian::Query::OP_AND, class_query, final_query
146   end
147   enquiry       = Xapian::Enquire.new(reader)
148   enquiry.query = final_query
149   Resultset.new(enquiry, :db_size => self.size, :limit => options[:limit])
150 end
size() click to toggle source

Size of the database (number of docs) @return [Integer] The number of docs in the database

   # File lib/xapian_db/database.rb
17 def size
18   reader.doccount
19 end
store_doc(doc) click to toggle source

Store a Xapian document @param [Xapian::Document] doc A Xapian document (see xapian.org/docs/sourcedoc/html/classXapian_1_1Document.html).

While you can pass any valid xapian document, you might want to use the {XapianDb::Indexer} to build a xapian doc
   # File lib/xapian_db/database.rb
24 def store_doc(doc)
25   # We always replace; Xapian adds the document automatically if
26   # it is not found
27   writer.replace_document("Q#{doc.data}", doc)
28 end