Module: XapianDb

Defined in:
lib/xapian_db/database.rb,
lib/xapian_db.rb,
lib/type_codec.rb,
lib/xapian_db/config.rb,
lib/xapian_db/indexer.rb,
lib/xapian_db/railtie.rb,
lib/xapian_db/resultset.rb,
lib/xapian_db/utilities.rb,
lib/xapian_db/query_parser.rb,
lib/generators/install_generator.rb,
lib/xapian_db/document_blueprint.rb,
lib/xapian_db/repositories/stemmer.rb,
lib/xapian_db/repositories/stopper.rb,
lib/xapian_db/adapters/base_adapter.rb,
lib/xapian_db/adapters/generic_adapter.rb,
lib/xapian_db/index_writers/no_op_writer.rb,
lib/xapian_db/adapters/datamapper_adapter.rb,
lib/xapian_db/index_writers/direct_writer.rb,
lib/xapian_db/index_writers/resque_worker.rb,
lib/xapian_db/index_writers/resque_writer.rb,
lib/xapian_db/index_writers/sidekiq_worker.rb,
lib/xapian_db/index_writers/sidekiq_writer.rb,
lib/xapian_db/model_extenders/active_record.rb,
lib/xapian_db/adapters/active_record_adapter.rb,
lib/xapian_db/index_writers/beanstalk_worker.rb,
lib/xapian_db/index_writers/beanstalk_writer.rb,
lib/xapian_db/index_writers/transactional_writer.rb

Overview

This writer collects index change requests but does not submit them immediately to the database. The index changes are applied when the commit method is called. This writer is intended for internal use only. Do not use it in a xapian configuration!

Author:

  • Gernot Kogler

Defined Under Namespace

Modules: Adapters, Generators, IndexWriters, ModelExtenders, Repositories, Utilities Classes: Config, Database, DocumentBlueprint, InMemoryDatabase, Indexer, PersistentDatabase, QueryParser, Railtie, Resultset, TypeCodec

Constant Summary collapse

LANGUAGE_MAP =

Supported languages

{:da => :danish,
:nl => :dutch,
:en => :english,
:fi => :finnish,
:fr => :french,
:de => :german2, # Normalises umlauts and ß
:hu => :hungarian,
:it => :italian,
:nb => :norwegian,
:nn => :norwegian,
:no => :norwegian,
:pt => :portuguese,
:ro => :romanian,
:ru => :russian,
:es => :spanish,
:sv => :swedish,
:tr => :turkish}

Class Method Summary collapse

Class Method Details

.auto_indexing_disabled(&block) ⇒ Object

Execute a block and do not update the index



165
166
167
168
169
170
# File 'lib/xapian_db.rb', line 165

def self.auto_indexing_disabled(&block)
  execute_block :writer => XapianDb::IndexWriters::NoOpWriter do
    block.call
  end

end

.create_db(options = {}) ⇒ XapianDb::Database

Create a database

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (String)

    A path to the file system. If no path is given, creates an in memory database. Overwrites an existing database!

Returns:



56
57
58
59
60
61
62
# File 'lib/xapian_db.rb', line 56

def self.create_db(options = {})
  if options[:path]
    PersistentDatabase.new(:path => options[:path], :create => true)
  else
    InMemoryDatabase.new
  end
end

.databaseXapianDb::Database

Access the configured database. See XapianDb::Config.setup for instructions on how to configure a database

Returns:



81
82
83
# File 'lib/xapian_db.rb', line 81

def self.database
  XapianDb::Config.database
end

.delete_doc_with(xapian_id, commit = true) ⇒ Object

Remove a document from the index

Parameters:

  • xapian_id (String)

    The document id



115
116
117
118
# File 'lib/xapian_db.rb', line 115

def self.delete_doc_with(xapian_id, commit=true)
  writer = @block_writer || XapianDb::Config.writer
  writer.delete_doc_with xapian_id, commit
end

.execute_block(opts, &block) ⇒ Object

execute a block of code with a given writer and handle errors

Parameters:

  • opts (Hash)

    Options

Options Hash (opts):

  • :writer (Object)

    An index writer

  • :error_message (String)

    the error message to log if an error occurs



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/xapian_db.rb', line 176

def self.execute_block(opts, &block)
  @block_writer = opts[:writer]
  begin
    block.call
  rescue Exception => ex
    if opts[:error_message]
      if defined?(Rails)
        Rails.logger.error opts[:error_message]
      else
        puts opts[:error_message]
      end
    end
    raise
  ensure
    # release the block writer
    @block_writer = nil
  end
end

.facets(attribute, expression) ⇒ Hash<Class, Integer>

Get facets from the configured database. See XapianDb::Database#facets for options

Returns:

  • (Hash<Class, Integer>)

    A hash containing the classes and the hits per class



102
103
104
# File 'lib/xapian_db.rb', line 102

def self.facets(attribute, expression)
  XapianDb::Config.database.facets attribute, expression
end

.index(obj, commit = true, changed_attrs: []) ⇒ Object

Update an object in the index

Parameters:

  • obj (Object)

    An instance of a class with a blueprint configuration



108
109
110
111
# File 'lib/xapian_db.rb', line 108

def self.index(obj, commit=true, changed_attrs: [])
  writer = @block_writer || XapianDb::Config.writer
  writer.index obj, commit, changed_attrs: changed_attrs
end

.open_db(options = {}) ⇒ XapianDb::Database

Open a database

Parameters:

  • options (Hash) (defaults to: {})

Options Hash (options):

  • :path (String)

    A path to the file system. If no path is given, creates an in memory database. If a path is given, then database must exist.

Returns:



70
71
72
73
74
75
76
# File 'lib/xapian_db.rb', line 70

def self.open_db(options = {})
  if options[:path]
    PersistentDatabase.new(:path => options[:path], :create => false)
  else
    InMemoryDatabase.new
  end
end

.rebuild_xapian_index(options = {}) ⇒ Boolean

Rebuild the xapian index for all configured blueprints

Parameters:

  • options (Hash) (defaults to: {})

    Options for reindexing

Options Hash (options):

  • :verbose (Boolean) — default: false

    Should the reindexing give status informations?

Returns:

  • (Boolean)

    Did we reindex anything?



144
145
146
147
148
149
150
151
152
153
# File 'lib/xapian_db.rb', line 144

def self.rebuild_xapian_index(options={})
  configured_classes = XapianDb::DocumentBlueprint.configured_classes
  return false unless configured_classes.size > 0
  configured_classes.each do |klass|
    if klass.respond_to?(:rebuild_xapian_index)
      XapianDb::Config.writer.reindex_class(klass, options)
    end
  end
  true
end

.reindex(object, commit = true, changed_attrs: []) ⇒ Object

Update or delete a xapian document belonging to an object depending on the ignore_if logic(if present)

Parameters:

  • object (Object)

    An instance of a class with a blueprint configuration



122
123
124
125
126
127
128
129
130
# File 'lib/xapian_db.rb', line 122

def self.reindex(object, commit=true, changed_attrs: [])
  writer = @block_writer || XapianDb::Config.writer
  blueprint = XapianDb::DocumentBlueprint.blueprint_for object.class.name
  if blueprint.should_index?(object)
    writer.index object, commit, changed_attrs: changed_attrs
  else
    writer.delete_doc_with object.xapian_id, commit
  end
end

.reindex_class(klass, options = {}) ⇒ Object

Reindex all objects of a given class

Parameters:

  • klass (Class)

    The class to reindex

  • options (Hash) (defaults to: {})

    Options for reindexing

Options Hash (options):

  • :verbose (Boolean) — default: false

    Should the reindexing give status informations?



136
137
138
# File 'lib/xapian_db.rb', line 136

def self.reindex_class(klass, options={})
  XapianDb::Config.writer.reindex_class klass, options
end

.search(expression, options = {}) ⇒ XapianDb::Resultset

Query the configured database. See XapianDb::Database#search for options

Returns:



88
89
90
91
92
93
94
95
96
97
# File 'lib/xapian_db.rb', line 88

def self.search(expression, options={})
  order = options.delete :order
  if order
    attr_names             = [order].flatten
    undefined_attrs        = attr_names - XapianDb::DocumentBlueprint.attributes
    raise ArgumentError.new "invalid order clause: attributes #{undefined_attrs.inspect} are not defined" unless undefined_attrs.empty?
    options[:sort_indices] = attr_names.map {|attr_name| XapianDb::DocumentBlueprint.value_number_for(attr_name) }
  end
  XapianDb::Config.database.search(expression, options)
end

.setup(&block) ⇒ Object

Global configuration for XapianDb. See XapianDb::Config.setup for available options



46
47
48
49
# File 'lib/xapian_db.rb', line 46

def self.setup(&block)
  XapianDb::Config.setup(&block)
  @writer = XapianDb::Config.writer
end

.transaction(&block) ⇒ Object

Execute a block as a transaction



156
157
158
159
160
161
162
# File 'lib/xapian_db.rb', line 156

def self.transaction(&block)
  writer = XapianDb::IndexWriters::TransactionalWriter.new
  execute_block :writer => writer, :error_message => "error in XapianDb transaction block, transaction aborted" do
    block.call
    writer.commit_using XapianDb::Config.writer
  end
end