From 6e57576ca319371e90df5849cb1c3ddc323518f9 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Thu, 14 Nov 2024 02:47:53 +0800 Subject: [PATCH 1/2] fix(matching-service): :ambulance: fix some issues with matching algorithm Fix the matched difficulties and topics so that it will be an OR if either is empty. Also fixes an issue with difficulties matching using wrong variable topics. --- .../matching-service/processes/findmatches.go | 36 ++++++++++++++----- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/apps/matching-service/processes/findmatches.go b/apps/matching-service/processes/findmatches.go index 8b104cb03d..b74bf6801d 100644 --- a/apps/matching-service/processes/findmatches.go +++ b/apps/matching-service/processes/findmatches.go @@ -74,18 +74,38 @@ func foundMatch(tx *redis.Tx, ctx context.Context, currentUser *models.MatchRequ } var matchedTopics []string - for _, topic := range currentUser.Topics { + if len(currentUser.Topics) == 0 || len(currentUser.Topics) == 0 { + // Ensure that matchedTopics will not be empty unless both users are empty + for _, topic := range currentUser.Topics { + matchedTopics = append(matchedTopics, topic) + } for _, otherTopic := range matchedUser.Topics { - if topic == otherTopic { - matchedTopics = append(matchedTopics, topic) + matchedTopics = append(matchedTopics, otherTopic) + } + } else { + for _, topic := range currentUser.Topics { + for _, otherTopic := range matchedUser.Topics { + if topic == otherTopic { + matchedTopics = append(matchedTopics, topic) + } } } } var matchedDifficulties []string - for _, topic := range currentUser.Difficulties { - for _, otherTopic := range matchedUser.Difficulties { - if topic == otherTopic { - matchedDifficulties = append(matchedDifficulties, topic) + if len(currentUser.Difficulties) == 0 || len(currentUser.Difficulties) == 0 { + // Ensure that matchedDifficulties will not be empty unless both users are empty + for _, diff := range currentUser.Difficulties { + matchedDifficulties = append(matchedDifficulties, diff) + } + for _, otherDiff := range matchedUser.Difficulties { + matchedDifficulties = append(matchedDifficulties, otherDiff) + } + } else { + for _, diff := range currentUser.Difficulties { + for _, otherDiff := range matchedUser.Difficulties { + if diff == otherDiff { + matchedDifficulties = append(matchedDifficulties, diff) + } } } } @@ -200,7 +220,7 @@ func doDifficultyMatching(tx *redis.Tx, ctx context.Context, currentUser *models if err != nil { return nil, err } - if len(otherUser.Topics) == 0 { + if len(otherUser.Difficulties) == 0 { foundUsers = append(foundUsers, otherUsername) } From e1109139d448e6c8f80aa5e615d3fb9f3cdb15e8 Mon Sep 17 00:00:00 2001 From: tituschewxj Date: Thu, 14 Nov 2024 07:09:22 +0800 Subject: [PATCH 2/2] fix: use matched user instead --- apps/matching-service/processes/findmatches.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/apps/matching-service/processes/findmatches.go b/apps/matching-service/processes/findmatches.go index b74bf6801d..c43a8d98eb 100644 --- a/apps/matching-service/processes/findmatches.go +++ b/apps/matching-service/processes/findmatches.go @@ -74,7 +74,7 @@ func foundMatch(tx *redis.Tx, ctx context.Context, currentUser *models.MatchRequ } var matchedTopics []string - if len(currentUser.Topics) == 0 || len(currentUser.Topics) == 0 { + if len(currentUser.Topics) == 0 || len(matchedUser.Topics) == 0 { // Ensure that matchedTopics will not be empty unless both users are empty for _, topic := range currentUser.Topics { matchedTopics = append(matchedTopics, topic) @@ -92,7 +92,7 @@ func foundMatch(tx *redis.Tx, ctx context.Context, currentUser *models.MatchRequ } } var matchedDifficulties []string - if len(currentUser.Difficulties) == 0 || len(currentUser.Difficulties) == 0 { + if len(currentUser.Difficulties) == 0 || len(matchedUser.Difficulties) == 0 { // Ensure that matchedDifficulties will not be empty unless both users are empty for _, diff := range currentUser.Difficulties { matchedDifficulties = append(matchedDifficulties, diff)