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

fix: fixed search #242

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
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
23 changes: 19 additions & 4 deletions controllers/search.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package controllers

import (
"github.com/getsentry/sentry-go"
. "github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
"log"
"net/http"
"strconv"

"github.com/getsentry/sentry-go"
. "github.com/mdg-iitr/Codephile/errors"
"github.com/mdg-iitr/Codephile/models"
)

// @Title Search
// @Description Endpoint to search users
// @Security token_auth read:user
// @Param count query string false "No of search objects to be returned"
// @Param query query string true "Search query"
// @Param path query string false "Field to search in"
// @Success 200 {object} []types.SearchDoc
// @Failure 400 "search query too small"
// @Failure 500 server_error
Expand All @@ -32,7 +34,20 @@ func (u *UserController) Search() {
if err != nil {
c = 500
}
results, err := models.SearchUser(query, c)
path := u.GetString("path")
//Checking for possible fields, default is "username"
var possible_path []string
var good_path bool = false
possible_path = []string{"username", "fullname", "handle.codechef", "handle.codeforces", "handle.hackerearth", "handle.hackerrank", "handle.spoj"}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-check the list of platforms. Leetcode is missing and hackerearth is extra.

for _, itr := range possible_path {
if itr == path {
good_path = true
}
}
if good_path == false {
path = "username"
}
results, err := models.SearchUser(query, c, path)
if err != nil {
hub := sentry.GetHubFromContext(u.Ctx.Request.Context())
hub.CaptureException(err)
Expand Down
14 changes: 5 additions & 9 deletions models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ func UidExists(uid bson.ObjectId) (bool, error) {
return false, nil
}

//checks if the user is verified, returns error if user doesn't exists
// checks if the user is verified, returns error if user doesn't exists
func IsUserVerified(uid bson.ObjectId) (bool, error, string) {
sess := db.NewUserCollectionSession()
defer sess.Close()
Expand Down Expand Up @@ -453,20 +453,16 @@ func PasswordResetEmail(email string, hostName string, ctx context.Context) bool
return true
}

func SearchUser(query string, c int) ([]types.SearchDoc, error) {
func SearchUser(query string, c int, path string) ([]types.SearchDoc, error) {
sess := db.NewUserCollectionSession()
defer sess.Close()

search := bson.M{
"$search": bson.M{
"index": "name_search",
"text": bson.M{
"index": "names",
"autocomplete": bson.M{
"query": query,
"path": bson.M{"wildcard": "*"},
"fuzzy": bson.M{
"maxEdits": 2,
"maxExpansions": 50,
},
"path": path,
},
},
}
Expand Down
Loading