Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: club and event search #873

Merged
merged 38 commits into from
May 28, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
cbbd51f
Fixes + rip out piinecone
michael-brennan2005 May 11, 2024
efcc4d3
docker compose for opensearch
michael-brennan2005 May 11, 2024
54202e7
seeding
michael-brennan2005 May 14, 2024
2487089
go.work.sum
michael-brennan2005 May 14, 2024
d594117
Merge branch 'main' into club-search
michael-brennan2005 May 14, 2024
a1e14b9
remove all search stuff
michael-brennan2005 May 15, 2024
e23a28c
Merge branch 'main' into club-search
michael-brennan2005 May 15, 2024
e0cb96d
merge fixes
michael-brennan2005 May 15, 2024
f1b4b5c
debug stash - why god why
michael-brennan2005 May 17, 2024
f824bae
Basic club search is working
michael-brennan2005 May 17, 2024
389bb99
Merge branch 'main' into club-search
michael-brennan2005 May 17, 2024
5ed161b
More robust seedclubs func
michael-brennan2005 May 17, 2024
5fc0dc6
Mock data refactoring
michael-brennan2005 May 18, 2024
88a9a8d
finishing work for tonight
michael-brennan2005 May 18, 2024
08a1fce
Text search, min/members + tags filtering for clubs
michael-brennan2005 May 18, 2024
cfa57d3
get rid of debug exe
michael-brennan2005 May 18, 2024
c9c2823
Merge branch 'main' into club-search
michael-brennan2005 May 18, 2024
9c6ed85
Events search (untested)
michael-brennan2005 May 20, 2024
15ee987
Merge branch 'main' into club-search
michael-brennan2005 May 20, 2024
a071449
Event search + switch to JSON instead of query params
michael-brennan2005 May 21, 2024
4022c70
Event/club search transactions
michael-brennan2005 May 21, 2024
c04578d
cleaning
michael-brennan2005 May 21, 2024
090cea0
cleaning
michael-brennan2005 May 21, 2024
6563eaf
Merge branch 'club-search' of github.com:GenerateNU/sac into club-search
michael-brennan2005 May 21, 2024
1142248
fix go.work.sum
michael-brennan2005 May 22, 2024
be67b20
Merge branch 'main' into club-search
michael-brennan2005 May 22, 2024
a99c4f1
fix cli
michael-brennan2005 May 22, 2024
23a293f
Abstracting search
michael-brennan2005 May 22, 2024
3fc9adf
search fixes
michael-brennan2005 May 22, 2024
2cca2c6
fixes :)
michael-brennan2005 May 22, 2024
efad8d8
Merge branch 'main' into club-search
michael-brennan2005 May 28, 2024
b8bd710
fix merge issues
michael-brennan2005 May 28, 2024
9c21b8b
code review fixes
michael-brennan2005 May 28, 2024
1a3af9b
solve my linter woes
michael-brennan2005 May 28, 2024
db0af00
issue w ci
michael-brennan2005 May 28, 2024
14b1664
OH MY GOD
michael-brennan2005 May 28, 2024
ead3ae1
merge
michael-brennan2005 May 28, 2024
b5174c8
use new requst utilities | format | lint
garrettladley May 28, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions backend/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,6 @@ type Settings struct {
SuperUser SuperUserSettings
Auth AuthSettings
AWS AWSSettings
Pinecone PineconeSettings
OpenAI OpenAISettings
Resend ResendSettings
Calendar CalendarSettings
GoogleSettings OAuthSettings
Expand Down
14 changes: 0 additions & 14 deletions backend/config/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,20 +37,6 @@ func readLocal(v *viper.Viper, path string, useDevDotEnv bool) (*Settings, error
return nil, fmt.Errorf("failed to load %s/.env.template: %w", path, err)
}

pineconeSettings, err := readPineconeSettings()
if err != nil {
return nil, fmt.Errorf("failed to read Pinecone settings: %w", err)
}

settings.Pinecone = *pineconeSettings

openAISettings, err := readOpenAISettings()
if err != nil {
return nil, fmt.Errorf("failed to read OpenAI settings: %w", err)
}

settings.OpenAI = *openAISettings

awsSettings, err := readAWSSettings()
if err != nil {
return nil, fmt.Errorf("failed to read AWS settings: %w", err)
Expand Down
28 changes: 0 additions & 28 deletions backend/config/openai.go

This file was deleted.

40 changes: 0 additions & 40 deletions backend/config/pinecone.go

This file was deleted.

12 changes: 0 additions & 12 deletions backend/config/production.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,6 @@ func readProd(v *viper.Viper) (*Settings, error) {
return nil, errors.New("failed to create secret from refresh token")
}

pineconeSettings, err := readPineconeSettings()
if err != nil {
return nil, fmt.Errorf("failed to read Pinecone settings: %w", err)
}

openAISettings, err := readOpenAISettings()
if err != nil {
return nil, fmt.Errorf("failed to read OpenAI settings: %w", err)
}

awsSettings, err := readAWSSettings()
if err != nil {
return nil, fmt.Errorf("failed to read AWS settings: %w", err)
Expand Down Expand Up @@ -129,8 +119,6 @@ func readProd(v *viper.Viper) (*Settings, error) {
AccessKey: authAccessKey,
RefreshKey: authRefreshKey,
},
Pinecone: *pineconeSettings,
OpenAI: *openAISettings,
AWS: *awsSettings,
Resend: *resendSettings,
Calendar: prodSettings.Calendar,
Expand Down
6 changes: 4 additions & 2 deletions backend/constants/search.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package constants

const (
SCORE_THRESHOLD float32 = 0.7
TOP_K_RESULTS int = 25
SEARCH_URL = "http://127.0.0.1:9200"
CLUBS_INDEX = "clubs"
EVENTS_INDEX = "events"
SEARCH_QUERY_DEFAULT_MAX_MEMBERS = 16384
)
8 changes: 1 addition & 7 deletions backend/entities/clubs/base/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package base
import (
"net/http"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/gofiber/fiber/v2"
Expand Down Expand Up @@ -31,17 +30,12 @@ func NewClubController(clubService ClubServiceInterface) *ClubController {
// @Failure 500 {object} error
// @Router /clubs/ [get]
func (cl *ClubController) GetClubs(c *fiber.Ctx) error {
var queryParams models.ClubQueryParams
if err := c.QueryParser(&queryParams); err != nil {
return err
}

pagination, ok := fiberpaginate.FromContext(c)
if !ok {
return utilities.ErrExpectedPagination
}

clubs, err := cl.clubService.GetClubs(&queryParams, *pagination)
clubs, err := cl.clubService.GetClubs(*pagination)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions backend/entities/clubs/base/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
)

type ClubServiceInterface interface {
GetClubs(queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error)
GetClubs(pageInfo fiberpaginate.PageInfo) ([]models.Club, error)
GetClub(id string) (*models.Club, error)
CreateClub(clubBody CreateClubRequestBody) (*models.Club, error)
UpdateClub(id string, clubBody UpdateClubRequestBody) (*models.Club, error)
Expand All @@ -26,8 +26,8 @@ func NewClubService(serviceParams types.ServiceParams) ClubServiceInterface {
return &ClubService{serviceParams}
}

func (c *ClubService) GetClubs(queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error) {
return GetClubs(c.DB, c.Integrations.Search, queryParams, pageInfo)
func (c *ClubService) GetClubs(pagination fiberpaginate.PageInfo) ([]models.Club, error) {
return GetClubs(c.DB, pagination)
}

func (c *ClubService) CreateClub(clubBody CreateClubRequestBody) (*models.Club, error) {
Expand All @@ -40,7 +40,7 @@ func (c *ClubService) CreateClub(clubBody CreateClubRequestBody) (*models.Club,
return nil, err
}

return CreateClub(c.DB, c.Integrations.Search, clubBody.UserID, *club)
return CreateClub(c.DB, clubBody.UserID, *club)
}

func (c *ClubService) GetClub(id string) (*models.Club, error) {
Expand Down Expand Up @@ -71,7 +71,7 @@ func (c *ClubService) UpdateClub(id string, clubBody UpdateClubRequestBody) (*mo
return nil, err
}

return UpdateClub(c.DB, c.Integrations.Search, *idAsUUID, *club)
return UpdateClub(c.DB, *idAsUUID, *club)
}

func (c *ClubService) DeleteClub(id string) error {
Expand All @@ -80,5 +80,5 @@ func (c *ClubService) DeleteClub(id string) error {
return err
}

return DeleteClub(c.DB, c.Integrations.Search, *idAsUUID)
return DeleteClub(c.DB, *idAsUUID)
}
94 changes: 25 additions & 69 deletions backend/entities/clubs/base/transactions.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,21 @@ package base
import (
"errors"

"github.com/GenerateNU/sac/backend/integrations/search"
"github.com/GenerateNU/sac/backend/constants"
"github.com/GenerateNU/sac/backend/utilities"
"github.com/garrettladley/fiberpaginate"
"github.com/sahilm/fuzzy"

"github.com/GenerateNU/sac/backend/entities/models"
"github.com/GenerateNU/sac/backend/entities/users"

search "github.com/GenerateNU/sac/backend/search/base"
"github.com/google/uuid"
"gorm.io/gorm"
"gorm.io/gorm/clause"
)

func GetClubs(db *gorm.DB, pinecone search.SearchClientInterface, queryParams *models.ClubQueryParams, pageInfo fiberpaginate.PageInfo) ([]models.Club, error) {
func GetClubs(db *gorm.DB, pageInfo fiberpaginate.PageInfo) ([]models.Club, error) {
query := db.Model(&models.Club{})

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
query = query.Preload("Tags")
}

query = query.Where(queryParams.IntoWhere())
query = query.Where("name != ?", "SAC")

if queryParams.Tags != nil && len(queryParams.Tags) > 0 {
query = query.Joins("JOIN club_tags ON club_tags.club_id = clubs.id").
Where("club_tags.tag_id IN ?", queryParams.Tags). // add search function here
Group("clubs.id") // ensure unique club records
}

if queryParams.Search != "" {
clubSearch := models.NewClubSearch(queryParams.Search)
resultIDs, err := pinecone.Search(clubSearch)
if err != nil {
return nil, err
}

query = query.Where("id IN ?", resultIDs)
}

var clubs models.Clubs

if queryParams.Search != "" {
if err := query.Find(&clubs).Error; err != nil {
return nil, err
}

// len(matches) <= len(clubs), because it discardss results that don't fuzz at all.
matches := fuzzy.FindFrom(queryParams.Search, clubs)

// Copy the sorted, fuzzed matches to the top of the list, and record their indices
// in a list.
clubsSorted := models.Clubs{}
idxMap := map[int]bool{}
for _, match := range matches {
clubsSorted = append(clubsSorted, clubs[match.Index])
idxMap[match.Index] = true
}

// For the remaining clubs, append them to the bottom of the list, making sure not
// to include the fuzzed clubs again.
for i, club := range clubs {
_, exists := idxMap[i]
if !exists {
clubsSorted = append(clubsSorted, club)
}
}

return clubsSorted, nil
}
var clubs []models.Club

if err := query.Scopes(utilities.IntoScope(pageInfo, query)).Find(&clubs).Error; err != nil {
return nil, err
Expand All @@ -80,7 +26,7 @@ func GetClubs(db *gorm.DB, pinecone search.SearchClientInterface, queryParams *m
return clubs, nil
}

func CreateClub(db *gorm.DB, pinecone search.SearchClientInterface, userId uuid.UUID, club models.Club) (*models.Club, error) {
func CreateClub(db *gorm.DB, userId uuid.UUID, club models.Club) (*models.Club, error) {
user, err := users.GetUser(db, userId)
if err != nil {
return nil, err
Expand Down Expand Up @@ -120,15 +66,20 @@ func CreateClub(db *gorm.DB, pinecone search.SearchClientInterface, userId uuid.
return nil, err
}

if err := pinecone.Upsert([]search.Searchable{&club}); err != nil {
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, err
}

return &club, tx.Commit().Error
err = search.Upsert[models.Club](db, constants.CLUBS_INDEX, club.ID.String(), &club)
if err != nil {
return nil, err
}

return &club, nil
}

func UpdateClub(db *gorm.DB, pinecone search.SearchClientInterface, id uuid.UUID, club models.Club) (*models.Club, error) {
func UpdateClub(db *gorm.DB, id uuid.UUID, club models.Club) (*models.Club, error) {
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
Expand All @@ -151,15 +102,20 @@ func UpdateClub(db *gorm.DB, pinecone search.SearchClientInterface, id uuid.UUID
return nil, err
}

if pinecone.Upsert([]search.Searchable{&existingClub}) != nil {
if err := tx.Commit().Error; err != nil {
tx.Rollback()
return nil, err
}

err = search.Upsert[models.Club](db, constants.CLUBS_INDEX, club.ID.String(), &club)
if err != nil {
return nil, err
}

return &existingClub, tx.Commit().Error
}

func DeleteClub(db *gorm.DB, pinecone search.SearchClientInterface, id uuid.UUID) error {
func DeleteClub(db *gorm.DB, id uuid.UUID) error {
tx := db.Begin()
defer func() {
if r := recover(); r != nil {
Expand All @@ -177,11 +133,6 @@ func DeleteClub(db *gorm.DB, pinecone search.SearchClientInterface, id uuid.UUID
return err
}

if err := pinecone.Delete([]search.Searchable{&existingClub}); err != nil {
tx.Rollback()
return err
}

if result := tx.Delete(&models.Club{}, id); result.RowsAffected == 0 {
tx.Rollback()
if result.Error == nil {
Expand All @@ -190,5 +141,10 @@ func DeleteClub(db *gorm.DB, pinecone search.SearchClientInterface, id uuid.UUID
return result.Error
}

err = search.Delete(db, constants.CLUBS_INDEX, id.String())
if err != nil {
return err
}

return tx.Commit().Error
}
Loading
Loading