Skip to content

Commit

Permalink
fix(api): improve search (#405)
Browse files Browse the repository at this point in the history
  • Loading branch information
bouassaba authored Nov 29, 2024
1 parent ecb08e7 commit bd7c806
Show file tree
Hide file tree
Showing 11 changed files with 176 additions and 55 deletions.
57 changes: 54 additions & 3 deletions api/infra/search.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,15 @@ func NewSearchManager() *SearchManager {
}
if _, err := searchClient.Index(FileSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"name", "text"},
FilterableAttributes: []string{
"id",
"workspaceId",
"type",
"parentId",
"snapshotId",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -59,6 +68,13 @@ func NewSearchManager() *SearchManager {
}
if _, err := searchClient.Index(GroupSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"name"},
FilterableAttributes: []string{
"id",
"organizationId",
"members",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -71,6 +87,15 @@ func NewSearchManager() *SearchManager {
}
if _, err := searchClient.Index(WorkspaceSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"name"},
FilterableAttributes: []string{
"id",
"storageCapacity",
"rootId",
"organizationId",
"bucket",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -83,6 +108,12 @@ func NewSearchManager() *SearchManager {
}
if _, err := searchClient.Index(OrganizationSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"name"},
FilterableAttributes: []string{
"id",
"members",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -94,7 +125,13 @@ func NewSearchManager() *SearchManager {
panic(err)
}
if _, err := searchClient.Index(UserSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"fullName", "email"},
SearchableAttributes: []string{"fullName", "username", "email"},
FilterableAttributes: []string{
"id",
"isEmailConfirmed",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -107,6 +144,16 @@ func NewSearchManager() *SearchManager {
}
if _, err := searchClient.Index(TaskSearchIndex).UpdateSettings(&meilisearch.Settings{
SearchableAttributes: []string{"name"},
FilterableAttributes: []string{
"id",
"error",
"percentage",
"isIndeterminate",
"userId",
"status",
"createTime",
"updateTime",
},
}); err != nil {
panic(err)
}
Expand All @@ -117,11 +164,15 @@ func NewSearchManager() *SearchManager {
}

type QueryOptions struct {
Limit int64
Limit int64
Filter interface{}
}

func (mgr *SearchManager) Query(index string, query string, opts QueryOptions) ([]interface{}, error) {
res, err := searchClient.Index(index).Search(query, &meilisearch.SearchRequest{Limit: opts.Limit})
res, err := searchClient.Index(index).Search(query, &meilisearch.SearchRequest{
Limit: opts.Limit,
Filter: opts.Filter,
})
if err != nil {
return nil, err
}
Expand Down
24 changes: 18 additions & 6 deletions api/search/file_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,15 @@ type FileSearch struct {
}

type fileEntity struct {
ID string `json:"id"`
Name string `json:"name"`
Text *string `json:"text,omitempty"`
ID string `json:"id"`
WorkspaceID string `json:"workspaceId"`
Name string `json:"name"`
Type string `json:"type"`
ParentID *string `json:"parentId,omitempty"`
Text *string `json:"text,omitempty"`
SnapshotID *string `json:"snapshotId,omitempty"`
CreateTime string `json:"createTime"`
UpdateTime *string `json:"updateTime,omitempty"`
}

func (f fileEntity) GetID() string {
Expand Down Expand Up @@ -132,8 +138,14 @@ func (s *FileSearch) populateTextField(files []model.File) error {

func (s *FileSearch) mapEntity(file model.File) *fileEntity {
return &fileEntity{
ID: file.GetID(),
Name: file.GetName(),
Text: file.GetText(),
ID: file.GetID(),
WorkspaceID: file.GetWorkspaceID(),
Name: file.GetName(),
Type: file.GetType(),
ParentID: file.GetParentID(),
Text: file.GetText(),
SnapshotID: file.GetSnapshotID(),
CreateTime: file.GetCreateTime(),
UpdateTime: file.GetUpdateTime(),
}
}
16 changes: 12 additions & 4 deletions api/search/group_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,12 @@ type GroupSearch struct {
}

type groupEntity struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Name string `json:"name"`
OrganizationID string `json:"organizationId"`
Members []string `json:"members"`
CreateTime string `json:"createTime"`
UpdateTime *string `json:"updateTime"`
}

func (g groupEntity) GetID() string {
Expand Down Expand Up @@ -102,7 +106,11 @@ func (s *GroupSearch) Query(query string, opts infra.QueryOptions) ([]model.Grou

func (s *GroupSearch) mapEntity(group model.Group) *groupEntity {
return &groupEntity{
ID: group.GetID(),
Name: group.GetName(),
ID: group.GetID(),
Name: group.GetName(),
OrganizationID: group.GetOrganizationID(),
Members: group.GetMembers(),
CreateTime: group.GetCreateTime(),
UpdateTime: group.GetUpdateTime(),
}
}
14 changes: 10 additions & 4 deletions api/search/organization_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ type OrganizationSearch struct {
}

type organizationEntity struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Name string `json:"name"`
Members []string `json:"members"`
CreateTime string `json:"createTime"`
UpdateTime *string `json:"updateTime,omitempty"`
}

func (o organizationEntity) GetID() string {
Expand Down Expand Up @@ -102,7 +105,10 @@ func (s *OrganizationSearch) Query(query string, opts infra.QueryOptions) ([]mod

func (s *OrganizationSearch) mapEntity(org model.Organization) *organizationEntity {
return &organizationEntity{
ID: org.GetID(),
Name: org.GetName(),
ID: org.GetID(),
Name: org.GetName(),
Members: org.GetMembers(),
CreateTime: org.GetCreateTime(),
UpdateTime: org.GetUpdateTime(),
}
}
22 changes: 18 additions & 4 deletions api/search/task_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,15 @@ type TaskSearch struct {
}

type taskEntity struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Name string `json:"name"`
Error *string `json:"error,omitempty"`
Percentage *int `json:"percentage,omitempty"`
IsIndeterminate bool `json:"isIndeterminate"`
UserID string `json:"userId"`
Status string `json:"status"`
CreateTime string `json:"createTime"`
UpdateTime *string `json:"updateTime,omitempty"`
}

func (t taskEntity) GetID() string {
Expand Down Expand Up @@ -102,7 +109,14 @@ func (s *TaskSearch) Query(query string, opts infra.QueryOptions) ([]model.Task,

func (s *TaskSearch) mapEntity(task model.Task) *taskEntity {
return &taskEntity{
ID: task.GetID(),
Name: task.GetName(),
ID: task.GetID(),
Name: task.GetName(),
Error: task.GetError(),
Percentage: task.GetPercentage(),
IsIndeterminate: task.GetIsIndeterminate(),
UserID: task.GetUserID(),
Status: task.GetStatus(),
CreateTime: task.GetCreateTime(),
UpdateTime: task.GetUpdateTime(),
}
}
26 changes: 21 additions & 5 deletions api/search/workspace_search.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,14 @@ type WorkspaceSearch struct {
}

type workspaceEntity struct {
ID string `json:"id"`
Name string `json:"name"`
ID string `json:"id"`
Name string `json:"name"`
StorageCapacity int64 `json:"storageCapacity"`
RootID *string `json:"rootId"`
OrganizationID string `json:"organizationId"`
Bucket string `json:"bucket"`
CreateTime string `json:"createTime"`
UpdateTime *string `json:"updateTime,omitempty"`
}

func (w workspaceEntity) GetID() string {
Expand Down Expand Up @@ -101,8 +107,18 @@ func (s *WorkspaceSearch) Query(query string, opts infra.QueryOptions) ([]model.
}

func (s *WorkspaceSearch) mapEntity(workspace model.Workspace) *workspaceEntity {
return &workspaceEntity{
ID: workspace.GetID(),
Name: workspace.GetName(),
entity := &workspaceEntity{
ID: workspace.GetID(),
Name: workspace.GetName(),
StorageCapacity: workspace.GetStorageCapacity(),
OrganizationID: workspace.GetOrganizationID(),
Bucket: workspace.GetBucket(),
CreateTime: workspace.GetCreateTime(),
UpdateTime: workspace.GetUpdateTime(),
}
if workspace.GetRootID() != "" {
rootID := workspace.GetRootID()
entity.RootID = &rootID
}
return entity
}
16 changes: 11 additions & 5 deletions api/service/file_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,19 +670,25 @@ func (svc *FileService) List(id string, opts FileListOptions, userID string) (*F
}
var data []model.File
if opts.Query != nil && opts.Query.Text != nil {
count, err := svc.fileRepo.Count()
if err != nil {
return nil, err
filter := fmt.Sprintf("workspaceId=\"%s\"", workspace.GetID())
if opts.Query.Type != nil {
filter += fmt.Sprintf(" AND type=\"%s\"", *opts.Query.Type)
}
hits, err := svc.fileSearch.Query(*opts.Query.Text, infra.QueryOptions{Limit: count})
hits, err := svc.fileSearch.Query(*opts.Query.Text, infra.QueryOptions{Filter: filter})
if err != nil {
return nil, err
}
for _, hit := range hits {
var f model.File
f, err := svc.fileCache.Get(hit.GetID())
if err != nil {
return nil, err
var e *errorpkg.ErrorResponse
// We don't want to break if the search engine contains files that shouldn't be there
if errors.As(err, &e) && e.Code == errorpkg.NewFileNotFoundError(nil).Code {
continue
} else {
return nil, err
}
}
data = append(data, f)
}
Expand Down
14 changes: 8 additions & 6 deletions api/service/group_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,19 +216,21 @@ func (svc *GroupService) findAll(opts GroupListOptions, userID string) ([]model.
}
}
} else {
count, err := svc.groupRepo.Count()
if err != nil {
return nil, err
}
hits, err := svc.groupSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.groupSearch.Query(opts.Query, infra.QueryOptions{})
if err != nil {
return nil, err
}
var groups []model.Group
for _, hit := range hits {
group, err := svc.groupCache.Get(hit.GetID())
if err != nil {
continue
var e *errorpkg.ErrorResponse
// We don't want to break if the search engine contains groups that shouldn't be there
if errors.As(err, &e) && e.Code == errorpkg.NewGroupNotFoundError(nil).Code {
continue
} else {
return nil, err
}
}
groups = append(groups, group)
}
Expand Down
14 changes: 8 additions & 6 deletions api/service/organization_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -190,19 +190,21 @@ func (svc *OrganizationService) findAll(opts OrganizationListOptions, userID str
return nil, err
}
} else {
count, err := svc.groupRepo.Count()
if err != nil {
return nil, err
}
hits, err := svc.orgSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.orgSearch.Query(opts.Query, infra.QueryOptions{})
if err != nil {
return nil, err
}
var orgs []model.Organization
for _, hit := range hits {
org, err := svc.orgCache.Get(hit.GetID())
if err != nil {
continue
var e *errorpkg.ErrorResponse
// We don't want to break if the search engine contains organizations that shouldn't be there
if errors.As(err, &e) && e.Code == errorpkg.NewOrganizationNotFoundError(nil).Code {
continue
} else {
return nil, err
}
}
orgs = append(orgs, org)
}
Expand Down
14 changes: 8 additions & 6 deletions api/service/task_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,19 +236,21 @@ func (svc *TaskService) findAll(opts TaskListOptions, userID string) ([]model.Ta
return nil, err
}
} else {
count, err := svc.taskRepo.Count()
if err != nil {
return nil, err
}
hits, err := svc.taskSearch.Query(opts.Query, infra.QueryOptions{Limit: count})
hits, err := svc.taskSearch.Query(opts.Query, infra.QueryOptions{})
if err != nil {
return nil, err
}
var tasks []model.Task
for _, hit := range hits {
task, err := svc.taskCache.Get(hit.GetID())
if err != nil {
continue
var e *errorpkg.ErrorResponse
// We don't want to break if the search engine contains tasks that shouldn't be there
if errors.As(err, &e) && e.Code == errorpkg.NewTaskNotFoundError(nil).Code {
continue
} else {
return nil, err
}
}
tasks = append(tasks, task)
}
Expand Down
Loading

0 comments on commit bd7c806

Please sign in to comment.