Sha256: e3ebaea20b2762f519d72cf31898e0eaafd3e0b81e4523fca5db9c660742d30c

Contents?: true

Size: 1.86 KB

Versions: 7

Compression:

Stored size: 1.86 KB

Contents

# encoding: UTF-8

require 'xapian-fu'

module Spontaneous::Search
  class Database
    def self.create(index, revision)
      db = self.new(index, revision)
      db.create
      db
    end

    def initialize(index, revision)
      @index, @revision, @database = index, revision, nil
    end

    def create
      FileUtils.mkdir_p(directory)
      @database = XapianFu::XapianDb.new(xapian_options.merge({
        :create => true,
        :overwrite => true
      }))
    end

    def xapian_options
      options = { :dir => directory,
        :language => @index.language,
        :fields => @index.fields,
        :spelling => true }
      options[:stemmer] = @index.stemmer unless @index.stemmer.nil?
      options[:stopper] = @index.stopper unless @index.stopper.nil?
      options
    end

    def database
      @database ||= open_database
    end

    def open_database
      XapianFu::XapianDb.new(xapian_options)
    end

    def add(page)
      if @index.include?(page)
        database << @index.indexable_content(page)
      end
    end

    def close
      database.flush
    end

    def search(query, options = {})
      xapian_results = database.search(query, options)
      autocorrect = options.delete(:autocorrect)
      corrected_query = xapian_results.corrected_query
      # don't know if this is the right way to deal with the corrected query suggestion
      # but I would prefer that any searches act more like google and return a set of
      # results for a spelling correction if none have been found with the current query
      if autocorrect and xapian_results.empty? and !corrected_query.blank?
        xapian_results = database.search(corrected_query, options)
      end
      Results.new(xapian_results, corrected_query)
    end

    alias_method :<<, :add

    def directory
      S::Site.revision_dir(@revision) / "indexes" / @index.name
    end
  end # Database
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
spontaneous-0.2.0.alpha7 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha6 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha5 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha4 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha3 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha2 lib/spontaneous/search/database.rb
spontaneous-0.2.0.alpha1 lib/spontaneous/search/database.rb