FIX: Move count logic to the end for tag suggestions (#978)
This commit is contained in:
parent
bc0657f478
commit
80adefa1c1
|
@ -60,30 +60,29 @@ module DiscourseAi
|
||||||
.joins(:topic_tags, :tags)
|
.joins(:topic_tags, :tags)
|
||||||
.where(id: candidate_ids)
|
.where(id: candidate_ids)
|
||||||
.where("tags.id IN (?)", DiscourseTagging.visible_tags(@user.guardian).pluck(:id))
|
.where("tags.id IN (?)", DiscourseTagging.visible_tags(@user.guardian).pluck(:id))
|
||||||
.group("topics.id, tags.id, tags.name") # Group by topics.id and tags.id
|
.group("topics.id")
|
||||||
.order("array_position(ARRAY#{candidate_ids}, topics.id)")
|
.order("array_position(ARRAY#{candidate_ids}, topics.id)")
|
||||||
.pluck(
|
.pluck("array_agg(tags.name)")
|
||||||
"tags.id",
|
.map(&:uniq)
|
||||||
"tags.name",
|
|
||||||
"tags.#{count_column}",
|
|
||||||
"MIN(array_position(ARRAY#{candidate_ids}, topics.id))", # Get minimum index for ordering
|
|
||||||
)
|
|
||||||
.uniq # Ensure unique tags per topic
|
|
||||||
.map
|
.map
|
||||||
.with_index do |(id, name, count, index), idx|
|
.with_index { |tag_list, index| { tags: tag_list, score: candidates[index].last } }
|
||||||
{
|
.flat_map { |c| c[:tags].map { |t| { name: t, score: c[:score] } } }
|
||||||
id: id,
|
.map do |c|
|
||||||
name: name,
|
c[:score] = 1 / (c[:score] + 1) # inverse of the distance
|
||||||
count: count,
|
c
|
||||||
score: 1 / (candidates[idx].last + 1), # Inverse of the distance for score
|
|
||||||
}
|
|
||||||
end
|
end
|
||||||
.group_by { |tag| tag[:name] }
|
.group_by { |c| c[:name] }
|
||||||
.map do |name, tags|
|
.map { |name, scores| { name: name, score: scores.sum { |s| s[:score] } } }
|
||||||
tags.first.merge(score: tags.sum { |t| t[:score] })
|
.sort_by { |c| -c[:score] }
|
||||||
end # Aggregate scores per tag
|
|
||||||
.sort_by { |tag| -tag[:score] }
|
|
||||||
.take(5)
|
.take(5)
|
||||||
|
.then do |tags|
|
||||||
|
models = Tag.where(name: tags.map { _1[:name] }).index_by(&:name)
|
||||||
|
tags.map do |tag|
|
||||||
|
tag[:id] = models.dig(tag[:name])&.id
|
||||||
|
tag[:count] = models.dig(tag[:name])&.public_send(count_column) || 0
|
||||||
|
tag
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Loading…
Reference in New Issue