Merge pull request #390 from ZogStriP/min-search-term-length-site-setting

added the min-search-term-length site setting
This commit is contained in:
Robin Ward
2013-03-08 06:58:45 -08:00
8 changed files with 25 additions and 18 deletions

View File

@@ -1,9 +1,5 @@
module Search
def self.min_search_term_length
3
end
def self.per_facet
5
end
@@ -97,15 +93,15 @@ module Search
end
end
def self.query(term, type_filter=nil)
def self.query(term, type_filter=nil, min_search_term_length=3)
return nil if term.blank?
sanitized_term = PG::Connection.escape_string(term.gsub(/[:()&!]/,'')) # Instead of original term.gsub(/[^0-9a-zA-Z_ ]/, '')
# We are stripping only symbols taking place in FTS and simply sanitizing the rest.
sanitized_term = PG::Connection.escape_string(term.gsub(/[:()&!]/,''))
# really short terms are totally pointless
return nil if sanitized_term.blank? || sanitized_term.length < self.min_search_term_length
return nil if sanitized_term.blank? || sanitized_term.length < min_search_term_length
terms = sanitized_term.split
terms.map! {|t| "#{t}:*"}
@@ -176,12 +172,14 @@ module Search
result = grouped.map do |type, results|
more = type_filter.blank? && (results.size > Search.per_facet)
results = results[0..([results.length, Search.per_facet].min - 1)] if type_filter.blank?
{type: type,
name: I18n.t("search.types.#{type}"),
more: more,
results: results}
{
type: type,
name: I18n.t("search.types.#{type}"),
more: more,
results: results
}
end
result
end