From ee9b898838199d10e5d1c4c3418b054b862b9fed Mon Sep 17 00:00:00 2001 From: Caio Almeida <117518+caiosba@users.noreply.github.com> Date: Sun, 10 Nov 2024 14:09:26 -0300 Subject: [PATCH] Search for similar items only if `show_similar` flag is explicitly passed (#2124) * Search for similar items only if `show_similar` flag is explicitly passed. Fixes: CV2-5608. --- lib/check_search.rb | 1 + test/controllers/elastic_search_5_test.rb | 4 ++-- test/controllers/graphql_controller_2_test.rb | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/lib/check_search.rb b/lib/check_search.rb index 8d2e0964f..1edc1b874 100644 --- a/lib/check_search.rb +++ b/lib/check_search.rb @@ -282,6 +282,7 @@ def medias_query core_conditions << { terms: { get_search_field => @options['project_media_ids'] } } unless @options['project_media_ids'].blank? core_conditions << { terms: { team_id: [@options['team_id']].flatten } } if @options['team_id'].is_a?(Array) core_conditions << { terms: { archived: @options['archived'] } } + core_conditions << { term: { sources_count: 0 } } unless @options['show_similar'] custom_conditions << { terms: { read: @options['read'].map(&:to_i) } } if @options.has_key?('read') custom_conditions << { terms: { cluster_teams: @options['cluster_teams'] } } if @options.has_key?('cluster_teams') custom_conditions << { terms: { unmatched: @options['unmatched'] } } if @options.has_key?('unmatched') diff --git a/test/controllers/elastic_search_5_test.rb b/test/controllers/elastic_search_5_test.rb index 7a343503f..e37507c3e 100644 --- a/test/controllers/elastic_search_5_test.rb +++ b/test/controllers/elastic_search_5_test.rb @@ -23,10 +23,10 @@ def setup sleep 2 result = CheckSearch.new({}.to_json, nil, t.id) assert_equal [parent.id], result.medias.map(&:id).sort - result = CheckSearch.new({show_similar: true}.to_json, nil, t.id) + result = CheckSearch.new({ show_similar: true }.to_json, nil, t.id) assert_equal [parent.id, child_1.id, child_2.id], result.medias.map(&:id).sort result = CheckSearch.new({ keyword: 'child_media' }.to_json, nil, t.id) - assert_equal [parent.id], result.medias.map(&:id) + assert_equal [], result.medias.map(&:id) result = CheckSearch.new({ keyword: 'child_media', show_similar: true }.to_json, nil, t.id) assert_equal [child_1.id, child_2.id], result.medias.map(&:id).sort end diff --git a/test/controllers/graphql_controller_2_test.rb b/test/controllers/graphql_controller_2_test.rb index 4b0d33cdb..27df89c0d 100644 --- a/test/controllers/graphql_controller_2_test.rb +++ b/test/controllers/graphql_controller_2_test.rb @@ -379,7 +379,7 @@ def setup assert_response :success assert_equal 4, JSON.parse(@response.body)['data']['search']['number_of_results'] - query = 'query CheckSearch { search(query: "{\"keyword\":\"Foo\",\"show_similar\":false}") { number_of_results } }' + query = 'query CheckSearch { search(query: "{\"keyword\":\"Foo\",\"show_similar\":true}") { number_of_results } }' post :create, params: { query: query, team: 'team' } assert_response :success assert_equal 2, JSON.parse(@response.body)['data']['search']['number_of_results']