Class: Search::GroupedSearchResults
- Inherits:
-
Object
- Object
- Search::GroupedSearchResults
- 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
-
#categories ⇒ Object
readonly
Returns the value of attribute categories.
-
#error ⇒ Object
Returns the value of attribute error.
-
#extra_categories ⇒ Object
readonly
Returns the value of attribute extra_categories.
-
#groups ⇒ Object
readonly
Returns the value of attribute groups.
-
#more_categories ⇒ Object
readonly
Returns the value of attribute more_categories.
-
#more_full_page_results ⇒ Object
readonly
Returns the value of attribute more_full_page_results.
-
#more_posts ⇒ Object
readonly
Returns the value of attribute more_posts.
-
#more_users ⇒ Object
readonly
Returns the value of attribute more_users.
-
#posts ⇒ Object
readonly
Returns the value of attribute posts.
-
#search_context ⇒ Object
readonly
Returns the value of attribute search_context.
-
#search_log_id ⇒ Object
Returns the value of attribute search_log_id.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
-
#term ⇒ Object
readonly
Returns the value of attribute term.
-
#type_filter ⇒ Object
readonly
Returns the value of attribute type_filter.
-
#use_pg_headlines_for_excerpt ⇒ Object
readonly
Returns the value of attribute use_pg_headlines_for_excerpt.
-
#users ⇒ Object
readonly
Returns the value of attribute users.
Class Method Summary collapse
Instance Method Summary collapse
- #add(object) ⇒ Object
- #blurb(post) ⇒ Object
- #find_user_data(guardian) ⇒ Object
-
#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, can_lazy_load_categories: false) ⇒ GroupedSearchResults
constructor
A new instance of GroupedSearchResults.
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, can_lazy_load_categories: false) ⇒ GroupedSearchResults
Returns a new instance of GroupedSearchResults.
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 |
# File 'lib/search/grouped_search_results.rb', line 36 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, can_lazy_load_categories: false ) @type_filter = type_filter @term = term @blurb_term = blurb_term || term @search_context = search_context @blurb_length = blurb_length || BLURB_LENGTH @posts = [] @categories = [] @extra_categories = Set.new @users = [] @tags = [] @groups = [] @error = nil @is_header_search = is_header_search @use_pg_headlines_for_excerpt = use_pg_headlines_for_excerpt @can_lazy_load_categories = can_lazy_load_categories end |
Instance Attribute Details
#categories ⇒ Object (readonly)
Returns the value of attribute categories.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def categories @categories end |
#error ⇒ Object
Returns the value of attribute error.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def error @error end |
#extra_categories ⇒ Object (readonly)
Returns the value of attribute extra_categories.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def extra_categories @extra_categories end |
#groups ⇒ Object (readonly)
Returns the value of attribute groups.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def groups @groups end |
#more_categories ⇒ Object (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_results ⇒ Object (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 |
#more_posts ⇒ Object (readonly)
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_users ⇒ Object (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 |
#posts ⇒ Object (readonly)
Returns the value of attribute posts.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def posts @posts end |
#search_context ⇒ Object (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_id ⇒ Object
Returns the value of attribute search_log_id.
32 33 34 |
# File 'lib/search/grouped_search_results.rb', line 32 def search_log_id @search_log_id end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def @tags end |
#term ⇒ Object (readonly)
Returns the value of attribute term.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def term @term end |
#type_filter ⇒ Object (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 |
#use_pg_headlines_for_excerpt ⇒ Object (readonly)
Returns the value of attribute use_pg_headlines_for_excerpt.
13 14 15 |
# File 'lib/search/grouped_search_results.rb', line 13 def use_pg_headlines_for_excerpt @use_pg_headlines_for_excerpt end |
#users ⇒ Object (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
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/search/grouped_search_results.rb', line 128 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
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/search/grouped_search_results.rb', line 102 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 if can_lazy_load_categories category = case type when "posts" object.topic.category when "topics" object.category end if category extra_categories << category.parent_category if category.parent_category extra_categories << category end end end |
#blurb(post) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/search/grouped_search_results.rb', line 79 def blurb(post) opts = { term: @blurb_term, blurb_length: @blurb_length } post_search_data_version = post&.post_search_data&.version if post_search_data_version.present? && 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
67 68 69 70 71 72 73 |
# File 'lib/search/grouped_search_results.rb', line 67 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 |