Sha256: 67944ef5cc5e629007de493203c11cf5b523ae37b77166a671e34a72d942cb46

Contents?: true

Size: 1.69 KB

Versions: 10

Compression:

Stored size: 1.69 KB

Contents

module Searchlogic
  module Condition
    class Keywords < Base
      # Because be default it joins with AND, so padding an array just gives you more options. Joining with and is no different than combining all of the words.
      self.join_arrays_with_or = true
      
      BLACKLISTED_WORDS = ('a'..'z').to_a + ["about", "an", "are", "as", "at", "be", "by", "com", "de", "en", "for", "from", "how", "in", "is", "it", "la", "of", "on", "or", "that", "the", "the", "this", "to", "und", "was", "what", "when", "where", "who", "will", "with", "www"] # from ranks.nl        
      ALLOWED_CHARACTERS = 'àáâãäåßéèêëìíîïñòóôõöùúûüýÿ\-_\.@'
      
      class << self
        def condition_names_for_column
          super + ["kwords", "kw"]
        end
      end
      
      def to_conditions(value)
        strs = []
        subs = []
        
        search_parts = value.gsub(/,/, " ").split(/ /)
        replace_non_alnum_characters!(search_parts)
        search_parts.uniq!
        remove_blacklisted_words!(search_parts)
        return if search_parts.blank?
        
        search_parts.each do |search_part|
          strs << "#{column_sql} #{like_condition_name} ?"
          subs << "%#{search_part}%"
        end
        
        [strs.join(" AND "), *subs]
      end
      
      private
        def replace_non_alnum_characters!(search_parts)
          search_parts.each do |word|
            word.downcase!
            word.gsub!(/[^[:alnum:]#{ALLOWED_CHARACTERS}]/, '')
          end
        end
        
        def remove_blacklisted_words!(search_parts)
          search_parts.delete_if { |word| word.blank? || BLACKLISTED_WORDS.include?(word.downcase) }
        end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
searchlogic-1.5.10 lib/searchlogic/condition/keywords.rb
searchlogic-1.6.0 lib/searchlogic/condition/keywords.rb
searchlogic-1.5.7 lib/searchlogic/condition/keywords.rb
searchlogic-1.6.1 lib/searchlogic/condition/keywords.rb
searchlogic-1.5.6 lib/searchlogic/condition/keywords.rb
searchlogic-1.5.8 lib/searchlogic/condition/keywords.rb
searchlogic-1.5.9 lib/searchlogic/condition/keywords.rb
searchlogic-1.6.3 lib/searchlogic/condition/keywords.rb
searchlogic-1.6.2 lib/searchlogic/condition/keywords.rb
searchlogic-1.6.4 lib/searchlogic/condition/keywords.rb