Skip to content

Commit

Permalink
CV2-4062 apply team condition for PG & ES query (#1747)
Browse files Browse the repository at this point in the history
* CV2-4062: apply team_id filter for ES query

* CV2-4062: fix tests
  • Loading branch information
melsawy authored and caiosba committed Dec 1, 2023
1 parent 5d219b7 commit ba8563f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 11 deletions.
16 changes: 6 additions & 10 deletions lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def team_condition(team_id = nil)
is_shared = FeedTeam.where(feed_id: @feed.id, team_id: Team.current&.id, shared: true).last
is_shared ? feed_teams : [0] # Invalidate the query if the current team is not sharing content
else
team_id || Team.current&.id
[team_id || Team.current&.id].compact
end
end

Expand All @@ -80,12 +80,7 @@ def pusher_channel
end

def team
team_id = 0
if feed_query?
team_id = Team.current ? Team.current.id : @options['team_id'].first
else
team_id = @options['team_id']
end
team_id = feed_query? && Team.current ? Team.current.id : @options['team_id'].first
Team.find_by_id(team_id)
end

Expand Down Expand Up @@ -266,7 +261,7 @@ def alegre_file_similar_items
file_path = "check_search/#{hash}"
end
threshold = Bot::Alegre.get_threshold_for_query(@options['file_type'], ProjectMedia.new(team_id: Team.current.id))[0][:value]
results = Bot::Alegre.get_items_with_similar_media(CheckS3.public_url(file_path), [{ value: threshold }], @options['team_id'], "/#{@options['file_type']}/similarity/")
results = Bot::Alegre.get_items_with_similar_media(CheckS3.public_url(file_path), [{ value: threshold }], @options['team_id'].first, "/#{@options['file_type']}/similarity/")
results.blank? ? [0] : results.keys
end

Expand Down Expand Up @@ -343,18 +338,19 @@ def adjust_es_window_size
end

def adjust_project_filter
team_id = [@options['team_id']].flatten.first
project_group_ids = [@options['project_group_id']].flatten.reject{ |pgid| pgid.blank? }.map(&:to_i)
unless project_group_ids.empty?
project_ids = @options['projects'].to_a.map(&:to_i)
project_groups_project_ids = Project.where(project_group_id: project_group_ids, team: @options['team_id']).map(&:id)
project_groups_project_ids = Project.where(project_group_id: project_group_ids, team_id: team_id).map(&:id)

project_ids = project_ids.blank? ? project_groups_project_ids : (project_ids & project_groups_project_ids)

# Invalidate the search if empty... otherwise, adjust the projects filter
@options['projects'] = project_ids.empty? ? [0] : project_ids
end
if Team.current && !feed_query? && [@options['team_id']].flatten.size == 1
t = Team.find([@options['team_id']].flatten.first)
t = Team.find(team_id)
@options['projects'] = @options['projects'].blank? ? (Project.where(team_id: t.id).map(&:id) + [nil]) : Project.where(id: @options['projects'], team_id: t.id).map(&:id)
end
@options['projects'] += [nil] if @options['none_project']
Expand Down
2 changes: 1 addition & 1 deletion test/controllers/elastic_search_7_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ def setup
create_comment annotated: pm, text: 'item notepm', disable_es_callbacks: false
create_comment annotated: pm2, text: 'item comment', disable_es_callbacks: false
sleep 2
result = CheckSearch.new({keyword: 'item', keyword_fields: {fields: ['comments']}}.to_json)
result = CheckSearch.new({keyword: 'item', keyword_fields: {fields: ['comments']}}.to_json, nil, t.id)
assert_equal [pm.id, pm2.id], result.medias.map(&:id).sort
end

Expand Down
27 changes: 27 additions & 0 deletions test/controllers/elastic_search_9_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,32 @@ def setup
end
end

test "shoud add team filter by default" do
t = create_team
t2 = create_team
pm1 = create_project_media team: t, quote: 'test', disable_es_callbacks: false
pm2 = create_project_media team: t2, quote: 'test', disable_es_callbacks: false
ProjectMedia.where(id: [pm1.id, pm2.id]).update_all(project_id: nil)
options = {
index: CheckElasticSearchModel.get_index_alias,
body: {
script: { source: "ctx._source.project_id = params.project_id", params: { project_id: nil } },
query: { terms: { annotated_id: [pm1.id, pm2.id] } }
}
}
$repository.client.update_by_query options
sleep 2
Team.stubs(:current).returns(t)
# PG
query = { }
result = CheckSearch.new(query.to_json)
assert_equal [pm1.id], result.medias.map(&:id)
# ES
query = { keyword: 'test' }
result = CheckSearch.new(query.to_json)
assert_equal [pm1.id], result.medias.map(&:id)
Team.unstub(:current)
end

# Please add new tests to test/controllers/elastic_search_10_test.rb
end

0 comments on commit ba8563f

Please sign in to comment.