lib/pg_search/features/trigram.rb in pg_search-2.1.7 vs lib/pg_search/features/trigram.rb in pg_search-2.2.0

- old
+ new

@@ -2,36 +2,60 @@ module PgSearch module Features class Trigram < Feature def self.valid_options - super + [:threshold] + super + %i[threshold word_similarity] end def conditions if options[:threshold] Arel::Nodes::Grouping.new( similarity.gteq(options[:threshold]) ) else Arel::Nodes::Grouping.new( - Arel::Nodes::InfixOperation.new("%", normalized_document, normalized_query) + Arel::Nodes::InfixOperation.new( + infix_operator, + normalized_query, + normalized_document + ) ) end end def rank Arel::Nodes::Grouping.new(similarity) end private + def word_similarity? + options[:word_similarity] + end + + def similarity_function + if word_similarity? + 'word_similarity' + else + 'similarity' + end + end + + def infix_operator + if word_similarity? + '<%' + else + '%' + end + end + def similarity Arel::Nodes::NamedFunction.new( - "similarity", + similarity_function, [ - normalized_document, - normalized_query + normalized_query, + normalized_document ] ) end def normalized_document