From 69e418facfdd5843246512971a2655b63e231a37 Mon Sep 17 00:00:00 2001 From: Sam Date: Mon, 1 Sep 2014 17:04:40 +1000 Subject: [PATCH] FEATURE: wider search with more context --- .../javascripts/discourse/lib/search.js | 2 +- .../search/topic_result.js.handlebars | 9 ++++++++- app/assets/stylesheets/desktop/header.scss | 10 ++++++++++ config/locales/server.en.yml | 2 +- lib/search.rb | 17 ++++++++++++----- lib/search/search_result.rb | 19 ++++++++++--------- lib/summarize.rb | 2 +- spec/components/search_spec.rb | 17 ++++++----------- 8 files changed, 49 insertions(+), 29 deletions(-) diff --git a/app/assets/javascripts/discourse/lib/search.js b/app/assets/javascripts/discourse/lib/search.js index 512213d02ad..f71766c1f66 100644 --- a/app/assets/javascripts/discourse/lib/search.js +++ b/app/assets/javascripts/discourse/lib/search.js @@ -21,7 +21,7 @@ Discourse.Search = { if (!opts) opts = {}; // Only include the data we have - var data = { term: term }; + var data = { term: term, include_blurbs: 'true' }; if (opts.typeFilter) data.type_filter = opts.typeFilter; if (opts.searchForId) data.search_for_id = true; diff --git a/app/assets/javascripts/discourse/templates/search/topic_result.js.handlebars b/app/assets/javascripts/discourse/templates/search/topic_result.js.handlebars index 8455ef299b5..0ee415c460e 100644 --- a/app/assets/javascripts/discourse/templates/search/topic_result.js.handlebars +++ b/app/assets/javascripts/discourse/templates/search/topic_result.js.handlebars @@ -1,3 +1,10 @@ - + + {{unbound title}} + + {{#unless Discourse.Mobile.mobileView}} + + {{{unbound blurb}}} + + {{/unless}} diff --git a/app/assets/stylesheets/desktop/header.scss b/app/assets/stylesheets/desktop/header.scss index d294bad386b..415ef103821 100644 --- a/app/assets/stylesheets/desktop/header.scss +++ b/app/assets/stylesheets/desktop/header.scss @@ -65,3 +65,13 @@ and (max-width : 570px) { } } } + +.search-link .blurb { + color: scale-color($primary, $lightness: 45%); + display: block; + font-size: 11px; +} + +.d-dropdown#search-dropdown { + width: 540px; +} diff --git a/config/locales/server.en.yml b/config/locales/server.en.yml index 9a9911491e9..13427bdede0 100644 --- a/config/locales/server.en.yml +++ b/config/locales/server.en.yml @@ -1025,7 +1025,7 @@ en: granted_badge: "You earned %{link}" search: - within_post: "#%{post_number} by %{username}: %{excerpt}" + within_post: "#%{post_number} by %{username}" types: category: 'Categories' topic: 'Results' diff --git a/lib/search.rb b/lib/search.rb index 0dc1535a370..c199b0f65f1 100644 --- a/lib/search.rb +++ b/lib/search.rb @@ -279,17 +279,24 @@ class Search end def aggregate_search - cols = ['topics.id', 'topics.title', 'topics.slug'] + cols = ['topics.id', 'topics.title', 'topics.slug', 'cooked'] topics = posts_query(@limit, aggregate_search: true) .group(*cols) .pluck('min(posts.post_number)',*cols) + topics.each do |t| + post_number, topic_id, title, slug, cooked = t + + cooked = SearchObserver::HtmlScrubber.scrub(cooked).squish + blurb = SearchResult.blurb(cooked, @term) + @results.add_result(SearchResult.new(type: :topic, - topic_id: t[1], - id: t[1], - title: t[2], - url: "/t/#{t[3]}/#{t[1]}/#{t[0]}")) + topic_id: topic_id, + id: topic_id, + title: title, + url: "/t/#{slug}/#{topic_id}/#{post_number}", + blurb: blurb)) end end diff --git a/lib/search/search_result.rb b/lib/search/search_result.rb index d14b15c0bfb..85fb5d4b540 100644 --- a/lib/search/search_result.rb +++ b/lib/search/search_result.rb @@ -1,3 +1,5 @@ +require 'sanitize' + class Search class SearchResult @@ -55,24 +57,23 @@ class Search SearchResult.new(type: :topic, topic_id: t.id, id: t.id, title: options[:custom_title] || t.title, url: t.relative_url, blurb: options[:custom_blurb]) end + def self.blurb(raw, term) + blurb = TextHelper.excerpt(raw, term.split(/\s+/)[0], radius: 100) + blurb = TextHelper.truncate(raw, length: 200) if blurb.blank? + Sanitize.clean(blurb) + end + def self.from_post(p, context, term, include_blurbs=false) custom_title = if context && context.id == p.topic_id - # TODO: rewrite this - # 1. convert markdown to text - # 2. grab full words - excerpt = TextHelper.excerpt(p.raw, term.split(/\s+/)[0], radius: 30) - excerpt = TextHelper.truncate(p.raw, length: 50) if excerpt.blank? I18n.t("search.within_post", post_number: p.post_number, - username: p.user && p.user.username, - excerpt: excerpt + username: p.user && p.user.username ) end if include_blurbs #add a blurb from the post to the search results - custom_blurb = TextHelper.excerpt(p.raw, term.split(/\s+/)[0], radius: 100) - custom_blurb = TextHelper.truncate(p.raw, length: 200) if custom_blurb.blank? + custom_blurb = blurb(p.raw, term) end if p.post_number == 1 # we want the topic link when it's the OP diff --git a/lib/summarize.rb b/lib/summarize.rb index d1d1b8cb087..d6e23595f46 100644 --- a/lib/summarize.rb +++ b/lib/summarize.rb @@ -4,7 +4,7 @@ require 'sanitize' class Summarize - + def initialize(text) @text = text end diff --git a/spec/components/search_spec.rb b/spec/components/search_spec.rb index b2f9a2d66ef..adfc90dbea2 100644 --- a/spec/components/search_spec.rb +++ b/spec/components/search_spec.rb @@ -34,17 +34,12 @@ describe Search do @post = Fabricate(:post, topic: @topic, raw: 'this fun test ') @indexed = @post.post_search_data.search_data end - it "should include body in index" do - @indexed.should =~ /fun/ - end - it "should include title in index" do - @indexed.should =~ /sam/ - end - it "should include category in index" do - @indexed.should =~ /america/ - end - it "should pick up on title updates" do + it "should index correctly" do + @indexed.should =~ /fun/ + @indexed.should =~ /sam/ + @indexed.should =~ /america/ + @topic.title = "harpi is the new title" @topic.save! @post.post_search_data.reload @@ -186,8 +181,8 @@ describe Search do it 'returns the post' do result.should be_present result[:title].should == topic.title - result[:url].should == topic.relative_url + "/2" + result[:blurb].should == "this reply has no quotes" end end