Skip to content

Commit

Permalink
CV2-5085 move get_items_from_similar_text calls to use sync endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
DGaffney committed Sep 4, 2024
1 parent a77f00c commit 03efd22
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 9 deletions.
8 changes: 2 additions & 6 deletions app/models/bot/alegre.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def similar_items_ids_and_scores(team_ids, thresholds = {})
ALL_TEXT_SIMILARITY_FIELDS.each do |field|
text = self.send(field)
next if text.blank?
threads << Thread.new { ids_and_scores.merge!(Bot::Alegre.get_similar_texts(team_ids, text, Bot::Alegre::ALL_TEXT_SIMILARITY_FIELDS, thresholds[:text]).to_h) }
threads << Thread.new { ids_and_scores.merge!(Bot::Alegre.get_items_from_similar_text(team_ids, text, Bot::Alegre::ALL_TEXT_SIMILARITY_FIELDS, thresholds[:text]).to_h) }
end
threads.map(&:join)
end
Expand Down Expand Up @@ -206,7 +206,7 @@ def self.get_items_from_similar_text(team_id, text, fields = nil, threshold = ni
threshold ||= self.get_threshold_for_query('text', nil, true)
models ||= [self.matching_model_to_use(team_ids)].flatten
Hash[self.get_similar_items_from_api(
'/text/similarity/search/',
'/similarity/sync/text',
self.similar_texts_from_api_conditions(text, models, fuzzy, team_ids, fields, threshold),
threshold
).collect{|k,v| [k, v.merge(model: v[:model]||Bot::Alegre.default_matching_model)]}]
Expand Down Expand Up @@ -719,8 +719,4 @@ def self.is_text_too_short?(pm, length_threshold)
is_short
end

class <<self
alias_method :get_similar_texts, :get_items_from_similar_text
end

end
2 changes: 1 addition & 1 deletion app/resources/api/v2/report_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.apply_text_similarity_filter(organization_ids, threshold, filters)
ids = nil
unless text.blank?
fields = filters[:similarity_fields].blank? ? nil : filters[:similarity_fields].to_a.flatten
ids_and_scores = Bot::Alegre.get_similar_texts(organization_ids, text[0], fields, [{ value: threshold }], nil, filters.dig(:fuzzy, 0))
ids_and_scores = Bot::Alegre.get_items_from_similar_text(organization_ids, text[0], fields, [{ value: threshold }], nil, filters.dig(:fuzzy, 0))
RequestStore.store[:scores] = ids_and_scores # Store the scores so we can return them
ids = ids_and_scores.keys.uniq
ids = [0] if ids.blank?
Expand Down
4 changes: 2 additions & 2 deletions test/controllers/graphql_controller_5_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,14 @@ def setup
pm = create_project_media project: p, media: m
pm2 = create_project_media project: p, media: m2
create_claim_description project_media: pm2
Bot::Alegre.stubs(:get_similar_texts).returns({ pm2.id => 0.9, pm.id => 0.8 })
Bot::Alegre.stubs(:get_items_from_similar_text).returns({ pm2.id => 0.9, pm.id => 0.8 })

query = 'query { project_media(ids: "' + [pm.id, p.id, t.id].join(',') + '") { similar_items(first: 10000) { edges { node { dbid, claim_description { id, fact_check { id } } } } } } }'
post :create, params: { query: query, team: t.slug }
assert_response :success
assert_equal pm2.id, JSON.parse(@response.body)['data']['project_media']['similar_items']['edges'][0]['node']['dbid']

Bot::Alegre.unstub(:get_similar_texts)
Bot::Alegre.unstub(:get_items_from_similar_text)
end

test "should create and update flags and content warning" do
Expand Down

0 comments on commit 03efd22

Please sign in to comment.