Skip to content

Commit

Permalink
Filter items by Tipline search result feedback (#1852)
Browse files Browse the repository at this point in the history
* Cache the number of negative feedback

We are already storing/caching positive feedback in tiplne search
results, and here we are adding caching for negative ones as well.

* Add positive and negative search results cound fields to range filter

* Add smooch bot tests for positive/negative search results count
  • Loading branch information
jayjay-w authored Apr 8, 2024
1 parent 2ad265f commit 960900f
Show file tree
Hide file tree
Showing 6 changed files with 76 additions and 9 deletions.
19 changes: 19 additions & 0 deletions app/models/concerns/project_media_cached_fields.rb
Original file line number Diff line number Diff line change
Expand Up @@ -469,6 +469,21 @@ def title_or_description_update
}
]

cached_field :negative_tipline_search_results_count,
update_es: true,
recalculate: :recalculate_negative_tipline_search_results_count,
update_on: [
{
model: TiplineRequest,
if: proc { |tr| tr.smooch_request_type == 'irrelevant_search_result_requests' },
affected_ids: proc { |tr| [tr.associated_id] },
events: {
save: :recalculate,
destroy: :recalculate,
}
}
]

cached_field :tipline_search_results_count,
update_es: true,
recalculate: :recalculate_tipline_search_results_count,
Expand Down Expand Up @@ -658,6 +673,10 @@ def recalculate_positive_tipline_search_results_count
TiplineRequest.where(associated_type: 'ProjectMedia', associated_id: self.id, smooch_request_type: 'relevant_search_result_requests').count
end

def recalculate_negative_tipline_search_results_count
TiplineRequest.where(associated_type: 'ProjectMedia', associated_id: self.id, smooch_request_type: 'irrelevant_search_result_requests').count
end

def recalculate_tipline_search_results_count
types = ["relevant_search_result_requests", "irrelevant_search_result_requests", "timeout_search_requests"]
TiplineRequest.where(associated_type: 'ProjectMedia', associated_id: self.id, smooch_request_type: types).count
Expand Down
2 changes: 2 additions & 0 deletions app/repositories/media_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ class MediaSearch

indexes :positive_tipline_search_results_count, { type: 'long' }

indexes :negative_tipline_search_results_count, { type: 'long' }

indexes :tipline_search_results_count, { type: 'long' }
end
end
2 changes: 1 addition & 1 deletion lib/check_search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ def adjust_channel_filter

def adjust_numeric_range_filter
@options['range_numeric'] = {}
[:linked_items_count, :suggestions_count, :demand].each do |field|
[:linked_items_count, :suggestions_count, :demand, :positive_tipline_search_results_count, :negative_tipline_search_results_count].each do |field|
if @options.has_key?(field) && !@options[field].blank?
@options['range_numeric'][field] = @options[field]
end
Expand Down
39 changes: 39 additions & 0 deletions test/controllers/elastic_search_10_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -479,4 +479,43 @@ def setup

Team.current = nil
end

test "should filter by positive_tipline_search_results_count and negative_tipline_search_results_count numeric range" do
RequestStore.store[:skip_cached_field_update] = false
p = create_project
[:positive_tipline_search_results_count, :negative_tipline_search_results_count].each do |field|
query = { projects: [p.id], "#{field}": { max: 5 } }
query[field][:min] = 0
result = CheckSearch.new(query.to_json, nil, p.team_id)
assert_equal 0, result.medias.count
end
pm1 = create_project_media project: p, quote: 'Test A', disable_es_callbacks: false
pm2 = create_project_media project: p, quote: 'Test B', disable_es_callbacks: false

# Add positive search results
create_tipline_request team_id: p.team_id, associated: pm1, smooch_request_type: 'relevant_search_result_requests'
2.times { create_tipline_request(team_id: p.team_id, associated: pm2, smooch_request_type: 'relevant_search_result_requests') }

# Add negative search results
create_tipline_request team_id: p.team_id, associated: pm1, smooch_request_type: 'irrelevant_search_result_requests'
2.times { create_tipline_request(team_id: p.team_id, associated: pm2, smooch_request_type: 'irrelevant_search_result_requests') }

sleep 2

min_mapping = {
"0": [pm1.id, pm2.id],
"1": [pm1.id, pm2.id],
"2": [pm2.id],
"3": [],
}

[:positive_tipline_search_results_count, :negative_tipline_search_results_count].each do |field|
query = { projects: [p.id], "#{field}": { max: 5 } }
min_mapping.each do |min, items|
query[field][:min] = min.to_s
result = CheckSearch.new(query.to_json, nil, p.team_id)
assert_equal items.sort, result.medias.map(&:id).sort
end
end
end
end
2 changes: 1 addition & 1 deletion test/controllers/graphql_controller_2_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ def setup
test "should get cached values for list columns" do
RequestStore.store[:skip_cached_field_update] = false
t = create_team
t.set_list_columns = ["updated_at_timestamp", "last_seen", "demand", "share_count", "folder", "linked_items_count", "suggestions_count", "type_of_media", "status", "created_at_timestamp", "report_status", "tags_as_sentence", "media_published_at", "comment_count", "reaction_count", "related_count"]
t.set_list_columns = ["updated_at_timestamp", "last_seen", "demand", "share_count", "folder", "linked_items_count", "suggestions_count", "type_of_media", "status", "created_at_timestamp", "report_status", "tags_as_sentence", "media_published_at", "comment_count", "reaction_count", "related_count", "positive_tipline_search_results_count", "negative_tipline_search_results_count"]
t.save!
5.times { create_project_media team: t }
u = create_user is_admin: true
Expand Down
21 changes: 14 additions & 7 deletions test/models/bot/smooch_7_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,8 +491,9 @@ def teardown
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'relevant_search_result_requests', pm)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'relevant_search_result_requests', pm)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'timeout_search_requests', pm)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests')
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests')
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests', pm)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests', pm)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests', pm)
message = lambda do
{
type: 'text',
Expand All @@ -512,24 +513,30 @@ def teardown
}
end
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'relevant_search_result_requests', pm2)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests')
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests', pm2)
Bot::Smooch.save_message(message.call.to_json, @app_id, nil, 'irrelevant_search_result_requests', pm2)
# Verify cached field
assert_equal 5, pm.tipline_search_results_count
assert_equal 6, pm.tipline_search_results_count
assert_equal 2, pm.positive_tipline_search_results_count
assert_equal 2, pm2.tipline_search_results_count
assert_equal 3, pm.negative_tipline_search_results_count
assert_equal 3, pm2.tipline_search_results_count
assert_equal 1, pm2.positive_tipline_search_results_count
assert_equal 2, pm2.negative_tipline_search_results_count
# Verify ES values
es = $repository.find(pm.get_es_doc_id)
assert_equal 5, es['tipline_search_results_count']
assert_equal 6, es['tipline_search_results_count']
assert_equal 2, es['positive_tipline_search_results_count']
assert_equal 3, es['negative_tipline_search_results_count']
es2 = $repository.find(pm2.get_es_doc_id)
assert_equal 2, es2['tipline_search_results_count']
assert_equal 3, es2['tipline_search_results_count']
assert_equal 1, es2['positive_tipline_search_results_count']
assert_equal 2, es2['negative_tipline_search_results_count']
# Verify destroy
types = ["irrelevant_search_result_requests", "timeout_search_requests"]
TiplineRequest.where(associated_type: 'ProjectMedia', associated_id: pm.id, smooch_request_type: types).destroy_all
assert_equal 2, pm.tipline_search_results_count
assert_equal 2, pm.positive_tipline_search_results_count
assert_equal 0, pm.negative_tipline_search_results_count
end
end

Expand Down

0 comments on commit 960900f

Please sign in to comment.