FIX: Incorrect search blurb when advanced search filters are used take2

Also remove include_blurbs attribute which isn't used.
This commit is contained in:
Guo Xiang Tan 2020-07-14 11:05:57 +08:00
parent 919c87a9ce
commit ce39733b1a
No known key found for this signature in database
GPG Key ID: FBD110179AAC1F20
6 changed files with 65 additions and 36 deletions

View File

@ -135,7 +135,7 @@ export function searchForTerm(term, opts) {
if (!opts) opts = {}; if (!opts) opts = {};
// Only include the data we have // Only include the data we have
const data = { term: term, include_blurbs: "true" }; const data = { term: term };
if (opts.typeFilter) data.type_filter = opts.typeFilter; if (opts.typeFilter) data.type_filter = opts.typeFilter;
if (opts.searchForId) data.search_for_id = true; if (opts.searchForId) data.search_for_id = true;
if (opts.restrictToArchetype) if (opts.restrictToArchetype)

View File

@ -35,7 +35,6 @@ class SearchController < ApplicationController
search_args = { search_args = {
type_filter: 'topic', type_filter: 'topic',
guardian: guardian, guardian: guardian,
include_blurbs: true,
blurb_length: 300, blurb_length: 300,
page: if params[:page].to_i <= 10 page: if params[:page].to_i <= 10
[params[:page].to_i, 1].max [params[:page].to_i, 1].max
@ -53,10 +52,20 @@ class SearchController < ApplicationController
search_args[:user_id] = current_user.id if current_user.present? search_args[:user_id] = current_user.id if current_user.present?
if rate_limit_errors if rate_limit_errors
result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0) result = Search::GroupedSearchResults.new(
type_filter: search_args[:type_filter],
term: @search_term,
search_context: context
)
result.error = I18n.t("rate_limiter.slow_down") result.error = I18n.t("rate_limiter.slow_down")
elsif site_overloaded? elsif site_overloaded?
result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0) result = Search::GroupedSearchResults.new(
type_filter: search_args[:type_filter],
term: @search_term,
search_context: context
)
result.error = I18n.t("search.extreme_load_error") result.error = I18n.t("search.extreme_load_error")
else else
search = Search.new(@search_term, search_args) search = Search.new(@search_term, search_args)
@ -90,7 +99,6 @@ class SearchController < ApplicationController
search_args = { guardian: guardian } search_args = { guardian: guardian }
search_args[:type_filter] = params[:type_filter] if params[:type_filter].present? search_args[:type_filter] = params[:type_filter] if params[:type_filter].present?
search_args[:include_blurbs] = params[:include_blurbs] == "true" if params[:include_blurbs].present?
search_args[:search_for_id] = true if params[:search_for_id].present? search_args[:search_for_id] = true if params[:search_for_id].present?
context, type = lookup_search_context context, type = lookup_search_context
@ -106,10 +114,19 @@ class SearchController < ApplicationController
search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[:restrict_to_archetype].present? search_args[:restrict_to_archetype] = params[:restrict_to_archetype] if params[:restrict_to_archetype].present?
if rate_limit_errors if rate_limit_errors
result = Search::GroupedSearchResults.new(search_args[:type_filter], @search_term, context, false, 0) result = Search::GroupedSearchResults.new(
type_filter: search_args[:type_filter],
term: params[:term],
search_context: context
)
result.error = I18n.t("rate_limiter.slow_down") result.error = I18n.t("rate_limiter.slow_down")
elsif site_overloaded? elsif site_overloaded?
result = GroupedSearchResults.new(search_args["type_filter"], params[:term], context, false, 0) result = GroupedSearchResults.new(
type_filter: search_args["type_filter"],
term: params[:term],
search_context: context
)
else else
search = Search.new(params[:term], search_args) search = Search.new(params[:term], search_args)
result = search.execute result = search.execute

View File

@ -154,7 +154,6 @@ class Search
@opts = opts || {} @opts = opts || {}
@guardian = @opts[:guardian] || Guardian.new @guardian = @opts[:guardian] || Guardian.new
@search_context = @opts[:search_context] @search_context = @opts[:search_context]
@include_blurbs = @opts[:include_blurbs] || false
@blurb_length = @opts[:blurb_length] @blurb_length = @opts[:blurb_length]
@valid = true @valid = true
@page = @opts[:page] @page = @opts[:page]
@ -186,11 +185,11 @@ class Search
end end
@results = GroupedSearchResults.new( @results = GroupedSearchResults.new(
@opts[:type_filter], type_filter: @opts[:type_filter],
clean_term, term: clean_term,
@search_context, blurb_term: term,
@include_blurbs, search_context: @search_context,
@blurb_length blurb_length: @blurb_length
) )
end end

View File

@ -23,19 +23,20 @@ class Search
:more_users, :more_users,
:term, :term,
:search_context, :search_context,
:include_blurbs,
:more_full_page_results, :more_full_page_results,
:error :error
) )
attr_accessor :search_log_id attr_accessor :search_log_id
def initialize(type_filter, term, search_context, include_blurbs, blurb_length) BLURB_LENGTH = 200
def initialize(type_filter:, term:, search_context:, blurb_length: nil, blurb_term: nil)
@type_filter = type_filter @type_filter = type_filter
@term = term @term = term
@blurb_term = blurb_term || term
@search_context = search_context @search_context = search_context
@include_blurbs = include_blurbs @blurb_length = blurb_length || BLURB_LENGTH
@blurb_length = blurb_length || 200
@posts = [] @posts = []
@categories = [] @categories = []
@users = [] @users = []
@ -57,7 +58,7 @@ class Search
end end
def blurb(post) def blurb(post)
GroupedSearchResults.blurb_for(post.cooked, @term, @blurb_length) GroupedSearchResults.blurb_for(post.cooked, @blurb_term, @blurb_length)
end end
def add(object) def add(object)
@ -72,7 +73,7 @@ class Search
end end
end end
def self.blurb_for(cooked, term = nil, blurb_length = 200) def self.blurb_for(cooked, term = nil, blurb_length = BLURB_LENGTH)
blurb = nil blurb = nil
cooked = SearchIndexer.scrub_html_for_search(cooked) cooked = SearchIndexer.scrub_html_for_search(cooked)
@ -91,14 +92,11 @@ class Search
end end
if term if term
terms = term.split(/\s+/) if term =~ Regexp.new(Search::PHRASE_MATCH_REGEXP_PATTERN)
phrase = terms.first term = Regexp.last_match[1]
if phrase =~ Regexp.new(Search::PHRASE_MATCH_REGEXP_PATTERN)
phrase = Regexp.last_match[1]
end end
blurb = TextHelper.excerpt(cooked, phrase, blurb = TextHelper.excerpt(cooked, term,
radius: blurb_length / 2 radius: blurb_length / 2
) )
end end

View File

@ -427,7 +427,7 @@ describe Search do
context 'searching the OP' do context 'searching the OP' do
let!(:post) { Fabricate(:post_with_long_raw_content) } let!(:post) { Fabricate(:post_with_long_raw_content) }
let(:result) { Search.execute('hundred', type_filter: 'topic', include_blurbs: true) } let(:result) { Search.execute('hundred', type_filter: 'topic') }
it 'returns a result correctly' do it 'returns a result correctly' do
expect(result.posts.length).to eq(1) expect(result.posts.length).to eq(1)
@ -449,8 +449,7 @@ describe Search do
it 'returns the post' do it 'returns the post' do
result = Search.execute('elephant', result = Search.execute('elephant',
type_filter: 'topic', type_filter: 'topic'
include_blurbs: true
) )
expect(result.posts).to contain_exactly(reply) expect(result.posts).to contain_exactly(reply)
@ -459,8 +458,7 @@ describe Search do
it 'returns the right post and blurb for searches with phrase' do it 'returns the right post and blurb for searches with phrase' do
result = Search.execute('"elephant"', result = Search.execute('"elephant"',
type_filter: 'topic', type_filter: 'topic'
include_blurbs: true
) )
expect(result.posts).to contain_exactly(reply) expect(result.posts).to contain_exactly(reply)
@ -1492,7 +1490,7 @@ describe Search do
results = Search.execute('ragis', type_filter: 'topic') results = Search.execute('ragis', type_filter: 'topic')
expect(results.posts.length).to eq(1) expect(results.posts.length).to eq(1)
results = Search.execute('Rágis', type_filter: 'topic', include_blurbs: true) results = Search.execute('Rágis', type_filter: 'topic')
expect(results.posts.length).to eq(1) expect(results.posts.length).to eq(1)
# TODO: this is a test we need to fix! # TODO: this is a test we need to fix!
@ -1514,7 +1512,7 @@ describe Search do
results = Search.execute('regis', type_filter: 'topic') results = Search.execute('regis', type_filter: 'topic')
expect(results.posts.length).to eq(0) expect(results.posts.length).to eq(0)
results = Search.execute('Régis', type_filter: 'topic', include_blurbs: true) results = Search.execute('Régis', type_filter: 'topic')
expect(results.posts.length).to eq(1) expect(results.posts.length).to eq(1)
expect(results.blurb(results.posts.first)).to include('Régis') expect(results.blurb(results.posts.first)).to include('Régis')

View File

@ -53,7 +53,7 @@ describe SearchController do
get "/search/query.json", headers: { get "/search/query.json", headers: {
"HTTP_X_REQUEST_START" => "t=#{start_time.to_f}" "HTTP_X_REQUEST_START" => "t=#{start_time.to_f}"
}, params: { }, params: {
term: "hi there", include_blurb: true term: "hi there"
} }
expect(response.status).to eq(409) expect(response.status).to eq(409)
@ -80,7 +80,7 @@ describe SearchController do
term = "hello\0hello" term = "hello\0hello"
get "/search/query.json", params: { get "/search/query.json", params: {
term: term, include_blurb: true term: term
} }
expect(response.status).to eq(400) expect(response.status).to eq(400)
@ -88,7 +88,7 @@ describe SearchController do
it "can search correctly" do it "can search correctly" do
get "/search/query.json", params: { get "/search/query.json", params: {
term: 'awesome', include_blurb: true term: 'awesome'
} }
expect(response.status).to eq(200) expect(response.status).to eq(200)
@ -101,6 +101,24 @@ describe SearchController do
expect(data['topics'][0]['id']).to eq(awesome_post.topic_id) expect(data['topics'][0]['id']).to eq(awesome_post.topic_id)
end end
it "can search correctly with advanced search filters" do
awesome_post.update!(
raw: "#{"a" * Search::GroupedSearchResults::BLURB_LENGTH} elephant"
)
get "/search/query.json", params: { term: 'order:views elephant' }
expect(response.status).to eq(200)
data = response.parsed_body
expect(data.dig("grouped_search_result", "term")).to eq('order:views elephant')
expect(data['posts'].length).to eq(1)
expect(data['posts'][0]['id']).to eq(awesome_post.id)
expect(data['posts'][0]['blurb']).to include('elephant')
expect(data['topics'][0]['id']).to eq(awesome_post.topic_id)
end
it 'performs the query with a type filter' do it 'performs the query with a type filter' do
get "/search/query.json", params: { get "/search/query.json", params: {
@ -141,7 +159,6 @@ describe SearchController do
end end
it "should return the right result" do it "should return the right result" do
get "/search/query.json", params: { get "/search/query.json", params: {
term: user_post.topic_id, term: user_post.topic_id,
type_filter: 'topic', type_filter: 'topic',