Class: Search::GroupedSearchResults

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization
Defined in:
lib/search/grouped_search_results.rb

Defined Under Namespace

Classes: TextHelper

Constant Summary collapse

BLURB_LENGTH =
200
OMISSION =
"..."
SCRUB_HEADLINE_REGEXP =
%r{<span(?: \w+="[^"]+")* class="#{Search::HIGHLIGHT_CSS_CLASS}"(?: \w+="[^"]+")*>([^<]*)</span>}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(type_filter:, term:, search_context:, blurb_length: nil, blurb_term: nil, is_header_search: false, use_pg_headlines_for_excerpt: SiteSetting.use_pg_headlines_for_excerpt) ⇒ GroupedSearchResults

Returns a new instance of GroupedSearchResults.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/search/grouped_search_results.rb', line 34

def initialize(
  type_filter:,
  term:,
  search_context:,
  blurb_length: nil,
  blurb_term: nil,
  is_header_search: false,
  use_pg_headlines_for_excerpt: SiteSetting.use_pg_headlines_for_excerpt
)
  @type_filter = type_filter
  @term = term
  @blurb_term = blurb_term || term
  @search_context = search_context
  @blurb_length = blurb_length || BLURB_LENGTH
  @posts = []
  @categories = []
  @users = []
  @tags = []
  @groups = []
  @error = nil
  @is_header_search = is_header_search
  @use_pg_headlines_for_excerpt = use_pg_headlines_for_excerpt
end

Instance Attribute Details

#categoriesObject (readonly)

Returns the value of attribute categories.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def categories
  @categories
end

#errorObject

Returns the value of attribute error.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def error
  @error
end

#groupsObject (readonly)

Returns the value of attribute groups.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def groups
  @groups
end

#more_categoriesObject (readonly)

Returns the value of attribute more_categories.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def more_categories
  @more_categories
end

#more_full_page_resultsObject (readonly)

Returns the value of attribute more_full_page_results.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def more_full_page_results
  @more_full_page_results
end

Returns the value of attribute more_posts.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def more_posts
  @more_posts
end

#more_usersObject (readonly)

Returns the value of attribute more_users.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def more_users
  @more_users
end

#postsObject (readonly)

Returns the value of attribute posts.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def posts
  @posts
end

#search_contextObject (readonly)

Returns the value of attribute search_context.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def search_context
  @search_context
end

#search_log_idObject

Returns the value of attribute search_log_id.



30
31
32
# File 'lib/search/grouped_search_results.rb', line 30

def search_log_id
  @search_log_id
end

#tagsObject (readonly)

Returns the value of attribute tags.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def tags
  @tags
end

#termObject (readonly)

Returns the value of attribute term.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def term
  @term
end

#type_filterObject (readonly)

Returns the value of attribute type_filter.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def type_filter
  @type_filter
end

#usersObject (readonly)

Returns the value of attribute users.



13
14
15
# File 'lib/search/grouped_search_results.rb', line 13

def users
  @users
end

Class Method Details

.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/search/grouped_search_results.rb', line 106

def self.blurb_for(cooked: nil, term: nil, blurb_length: BLURB_LENGTH, scrub: true)
  blurb = nil

  if scrub
    cooked = SearchIndexer::HtmlScrubber.scrub(cooked)

    urls = Set.new
    cooked.scan(Discourse::Utils::URI_REGEXP) { urls << $& }
    urls.each do |url|
      begin
        case File.extname(URI(url).path || "")
        when Oneboxer::VIDEO_REGEX
          cooked.gsub!(url, I18n.t("search.video"))
        when Oneboxer::AUDIO_REGEX
          cooked.gsub!(url, I18n.t("search.audio"))
        end
      rescue URI::InvalidURIError
      end
    end
  end

  if term
    term = Regexp.last_match[1] if term =~ Regexp.new(Search::PHRASE_MATCH_REGEXP_PATTERN)

    blurb = TextHelper.excerpt(cooked, term, radius: blurb_length / 2)
  end

  blurb = TextHelper.truncate(cooked, length: blurb_length) if blurb.blank?
  Sanitize.clean(blurb)
end

Instance Method Details

#add(object) ⇒ Object



95
96
97
98
99
100
101
102
103
104
# File 'lib/search/grouped_search_results.rb', line 95

def add(object)
  type = object.class.to_s.downcase.pluralize
  if !@is_header_search && public_send(type).length == Search.per_filter
    @more_full_page_results = true
  elsif @is_header_search && public_send(type).length == Search.per_facet
    instance_variable_set("@more_#{type}".to_sym, true)
  else
    (self.public_send(type)) << object
  end
end

#blurb(post) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
# File 'lib/search/grouped_search_results.rb', line 74

def blurb(post)
  opts = { term: @blurb_term, blurb_length: @blurb_length }

  if post.post_search_data.version >= SearchIndexer::MIN_POST_BLURB_INDEX_VERSION &&
       !Search.segment_chinese? && !Search.segment_japanese?
    if use_pg_headlines_for_excerpt
      scrubbed_headline = post.headline.gsub(SCRUB_HEADLINE_REGEXP, '\1')
      prefix_omission = scrubbed_headline.start_with?(post.leading_raw_data) ? "" : OMISSION
      postfix_omission = scrubbed_headline.end_with?(post.trailing_raw_data) ? "" : OMISSION
      return "#{prefix_omission}#{post.headline}#{postfix_omission}"
    else
      opts[:cooked] = post.post_search_data.raw_data
      opts[:scrub] = false
    end
  else
    opts[:cooked] = post.cooked
  end

  GroupedSearchResults.blurb_for(**opts)
end

#find_user_data(guardian) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/search/grouped_search_results.rb', line 62

def find_user_data(guardian)
  if user = guardian.user
    topics = @posts.map(&:topic)
    topic_lookup = TopicUser.lookup_for(user, topics)
    topics.each { |ft| ft.user_data = topic_lookup[ft.id] }
  end
end