lib/pg_search/features/tsearch.rb in pg_search-0.2.2 vs lib/pg_search/features/tsearch.rb in pg_search-0.3
- old
+ new
@@ -44,25 +44,34 @@
# Add tsearch prefix operator if we're using a prefix search.
tsquery_sql = "#{tsquery_sql} || #{connection.quote(':*')}" if @options[:prefix]
"to_tsquery(:dictionary, #{tsquery_sql})"
- end.join(" && ")
+ end.join(@options[:any_word] ? ' || ' : ' && ')
end
def tsdocument
- if @options[:tsvector_column]
- @options[:tsvector_column].to_s
- else
- @columns.map do |search_column|
- tsvector = "to_tsvector(:dictionary, #{@normalizer.add_normalization(search_column.to_sql)})"
- search_column.weight.nil? ? tsvector : "setweight(#{tsvector}, #{connection.quote(search_column.weight)})"
- end.join(" || ")
- end
+ @columns.map do |search_column|
+ tsvector = "to_tsvector(:dictionary, #{@normalizer.add_normalization(search_column.to_sql)})"
+ search_column.weight.nil? ? tsvector : "setweight(#{tsvector}, #{connection.quote(search_column.weight)})"
+ end.join(" || ")
end
+
+ # From http://www.postgresql.org/docs/8.3/static/textsearch-controls.html
+ # 0 (the default) ignores the document length
+ # 1 divides the rank by 1 + the logarithm of the document length
+ # 2 divides the rank by the document length
+ # 4 divides the rank by the mean harmonic distance between extents (this is implemented only by ts_rank_cd)
+ # 8 divides the rank by the number of unique words in document
+ # 16 divides the rank by 1 + the logarithm of the number of unique words in document
+ # 32 divides the rank by itself + 1
+ # The integer option controls several behaviors, so it is a bit mask: you can specify one or more behaviors
+ def normalization
+ @options[:normalization] || 0
+ end
def tsearch_rank
- ["ts_rank((#{tsdocument}), (#{tsquery}))", interpolations]
+ ["ts_rank((#{tsdocument}), (#{tsquery}), #{normalization})", interpolations]
end
def dictionary
@options[:dictionary] || :simple
end