Sha256: b5184ecde43c28fd062bee4f157a48f8209ce494d17d84896669930c845b2538

Contents?: true

Size: 1.52 KB

Versions: 1

Compression:

Stored size: 1.52 KB

Contents

module ActiveSearch
  module Mongoid
    class FullTextSearchQuery

      def initialize(name, query, conditions, options)
        @name     = name
        @query    = query
        @filter   = prepare_filter(conditions)
        @options  = options
        @language = self.class.locale_to_language(conditions[:locale] || I18n.locale)
      end

      def run
        results = session.command({
          'text'      => @name,
          'search'    => @query,
          'language'  => @language,
          'filter'    => @filter
        })

        ResultsSet.new(results, @options[:page], @options[:per_page])
      end

      def session
        ::Mongoid.session('default')
      end

      def self.locale_to_language(locale)
        {
          dk: 'danish',
          nl: 'dutch',
          en: 'english',
          fi: 'finnish',
          fr: 'french',
          de: 'german',
          hu: 'hungarian',
          it: 'italian',
          nb: 'norwegian',
          br: 'portuguese',
          pt: 'portuguese',
          ro: 'romanian',
          ru: 'russian',
          es: 'spanish',
          se: 'swedish',
          tr: 'turkish'
        }[locale.to_sym] || 'english'
      end

      protected

      def prepare_filter(conditions)
        {}.tap do |filter|
          conditions.each do |key, value|
            if key.to_sym == :locale
              filter['locale'] = value.to_s unless value == false
            else
              filter["stored.#{key}"] = value
            end
          end
        end
      end

    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
activesearch-0.3.2 lib/activesearch/mongoid/full_text_search_query.rb