package models import ( "strings" "cloud.o-forge.io/core/deprecated-oc-catalog/services" "github.com/beego/beego/v2/core/logs" "go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/mongo" "go.mongodb.org/mongo-driver/mongo/options" ) type SearchResult struct { Computing []ComputingModel `json:"computing" required:"true"` Datacenter []DatacenterModel `json:"datacenter"` Storage []StorageModel `json:"storage"` Data []DataModel `json:"data"` } func FindByWord(word string) (object *SearchResult, err error) { returnObject := SearchResult{} opts := options.Find() opts.SetLimit(100) //FIXME: Decide if limit and how var cursor *mongo.Cursor if strings.TrimSpace(word) == "*" { word = "" } else { word = `(?i).*` + word + `.*` } // filter := bson.M{"$text": bson.M{"$search": word}} if cursor, err = services.MngoCollComputing.Find( services.MngoCtx, bson.M{"$or": []bson.M{ {"description": bson.M{"$regex": word}}, {"owner": bson.M{"$regex": word}}, {"license": bson.M{"$regex": word}}, }}, opts, ); err != nil { logs.Error(err) return nil, err } if err = cursor.All(services.MngoCtx, &returnObject.Computing); err != nil { logs.Error(err) return nil, err } if cursor, err = services.MngoCollDatacenter.Find( services.MngoCtx, bson.M{"$or": []bson.M{ {"name": bson.M{"$regex": word}}, {"description": bson.M{"$regex": word}}, {"owner": bson.M{"$regex": word}}, }}, opts, ); err != nil { logs.Error(err) return nil, err } if err = cursor.All(services.MngoCtx, &returnObject.Datacenter); err != nil { logs.Error(err) return nil, err } if cursor, err = services.MngoCollStorage.Find( services.MngoCtx, bson.M{"$or": []bson.M{ {"name": bson.M{"$regex": word}}, {"description": bson.M{"$regex": word}}, }}, opts, ); err != nil { logs.Error(err) return nil, err } if err = cursor.All(services.MngoCtx, &returnObject.Storage); err != nil { logs.Error(err) return nil, err } if cursor, err = services.MngoCollData.Find( services.MngoCtx, bson.M{"$or": []bson.M{ {"description": bson.M{"$regex": word}}, {"example": bson.M{"$regex": word}}, }}, opts, ); err != nil { logs.Error(err) return nil, err } if err = cursor.All(services.MngoCtx, &returnObject.Data); err != nil { logs.Error(err) return nil, err } return &returnObject, nil }