Skip to content

Commit

Permalink
CV2-5608: remove show_parent? method (#2116)
Browse files Browse the repository at this point in the history
* CV2-5608: remove show_parent? method

* CV2-5608: search should target secondary items and show items based on show_similar option

* CV2-5608: fix tests

* CV2-5608: add more documentation
  • Loading branch information
melsawy authored Nov 4, 2024
1 parent 4629593 commit aae65fd
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 51 deletions.
19 changes: 4 additions & 15 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ def get_pg_results_for_media
custom_conditions[k] = [@options[v]].flatten if @options.has_key?(v)
end
core_conditions.merge!({ archived: @options['archived'] })
core_conditions.merge!({ sources_count: 0 }) unless should_include_related_items?
# Use sources_count condition for PG query to get either parent or child based on show_similar option
core_conditions.merge!({ sources_count: 0 }) unless @options['show_similar']
range_filter(:pg, custom_conditions)
relation = ProjectMedia
if @options['operator'].upcase == 'OR'
Expand Down Expand Up @@ -271,30 +272,18 @@ def alegre_file_similar_items
results.blank? ? [0] : results.keys
end

def should_include_related_items?
@options['show_similar'] || show_parent?
end

def show_parent?
search_keys = ['verification_status', 'tags', 'rules', 'language', 'fc_language', 'request_language', 'report_language', 'team_tasks', 'assigned_to', 'channels', 'report_status']
!@options['projects'].blank? && !@options['keyword'].blank? && (search_keys & @options.keys).blank?
end

def get_search_field
field = 'annotated_id'
field = 'parent_id' if !@options['show_similar'] && show_parent?
field
@options['show_similar'] ? 'annotated_id' : 'parent_id'
end

def medias_query(include_related_items = self.should_include_related_items?)
def medias_query
core_conditions = []
custom_conditions = []
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'] } }
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')
core_conditions << { term: { sources_count: 0 } } unless include_related_items
custom_conditions << { terms: { unmatched: @options['unmatched'] } } if @options.has_key?('unmatched')
custom_conditions.concat keyword_conditions
custom_conditions.concat tags_conditions
Expand Down
44 changes: 13 additions & 31 deletions test/controllers/elastic_search_5_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,40 +13,22 @@ def setup
end
end

test "should search for parent items only" do
test "should match secondary items and show items based on show_similar option" do
t = create_team
p = create_project team: t
p2 = create_project team: t
pm1 = create_project_media disable_es_callbacks: false, project: p
pm2 = create_project_media disable_es_callbacks: false, project: p
sleep 2
result = CheckSearch.new({}.to_json, nil, t.id)
assert_equal [pm1.id, pm2.id].sort, result.medias.map(&:id).sort
r = create_relationship source_id: pm1.id, target_id: pm2.id, relationship_type: Relationship.confirmed_type
parent = create_project_media team: t, disable_es_callbacks: false
child_1 = create_project_media team: t, quote: 'child_media a', disable_es_callbacks: false
child_2 = create_project_media team: t, quote: 'child_media b', disable_es_callbacks: false
create_relationship source_id: parent.id, target_id: child_1.id, relationship_type: Relationship.confirmed_type
create_relationship source_id: parent.id, target_id: child_2.id, relationship_type: Relationship.confirmed_type
sleep 2
result = CheckSearch.new({ projects: [p.id] }.to_json, nil, t.id)
assert_equal [pm1.id], result.medias.map(&:id)
result = CheckSearch.new({}.to_json, nil, t.id)
assert_equal [pm1.id].sort, result.medias.map(&:id)
result = CheckSearch.new({ show_similar: true }.to_json, nil, t.id)
assert_equal [pm1.id, pm2.id].sort, result.medias.map(&:id).sort
end

test "should match secondary items but surface the main ones" do
# This case only happen when browsing a list and seach by keyword
t = create_team
p = create_project team: t
pm = create_project_media disable_es_callbacks: false, project: p
pm1 = create_project_media disable_es_callbacks: false, project: p
pm2 = create_project_media quote: 'target_media', disable_es_callbacks: false, project: p
r = create_relationship source_id: pm1.id, target_id: pm2.id, relationship_type: Relationship.confirmed_type
sleep 2
result = CheckSearch.new({ projects: [p.id] }.to_json, nil, t.id)
assert_equal [pm.id, pm1.id], result.medias.map(&:id).sort
result = CheckSearch.new({ projects: [p.id], keyword: 'target_media' }.to_json, nil, t.id)
assert_equal [pm1.id], result.medias.map(&:id)
result = CheckSearch.new({ projects: [p.id], keyword: 'target_media', tags: ['test'] }.to_json, nil, t.id)
assert_empty result.medias.map(&:id)
assert_equal [parent.id], result.medias.map(&:id).sort
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)
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

test "should reindex data" do
Expand Down
9 changes: 4 additions & 5 deletions test/controllers/graphql_controller_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -356,12 +356,11 @@ def setup
authenticate_with_user(u)
t = create_team slug: 'team'
create_team_user user: u, team: t
p = create_project team: t
pm1 = create_project_media project: p, quote: 'Test 1 Bar', disable_es_callbacks: false
pm2 = create_project_media project: p, quote: 'Test 2 Foo', disable_es_callbacks: false
pm1 = create_project_media team: t, quote: 'Test 1 Bar', disable_es_callbacks: false
pm2 = create_project_media team: t, quote: 'Test 2 Foo', disable_es_callbacks: false
create_relationship source_id: pm1.id, target_id: pm2.id, relationship_type: Relationship.confirmed_type, disable_es_callbacks: false
pm3 = create_project_media project: p, quote: 'Test 3 Bar', disable_es_callbacks: false
pm4 = create_project_media project: p, quote: 'Test 4 Foo', disable_es_callbacks: false
pm3 = create_project_media team: t, quote: 'Test 3 Bar', disable_es_callbacks: false
pm4 = create_project_media team: t, quote: 'Test 4 Foo', disable_es_callbacks: false
create_relationship source_id: pm3.id, target_id: pm4.id, relationship_type: Relationship.confirmed_type, disable_es_callbacks: false
sleep 1

Expand Down

0 comments on commit aae65fd

Please sign in to comment.