Example #1
0
        public async Task <dynamic> Get([FromQuery] string search)
        {
            var userCollection = MongoWrapper.Database.GetCollection <Models.User>(nameof(Models.User));

            var userProjectionBuilder = new ProjectionDefinitionBuilder <Models.User>();
            var userProjection        = userProjectionBuilder
                                        .MetaTextScore("MetaScore".WithLowercaseFirstCharacter())
                                        .Include(user => user._id)
                                        .Include(user => user.FullName)
                                        .Include(user => user.Avatar)
                                        .Include(user => user.About)
                                        .Include("_t");

            var userFilterBuilder = new FilterDefinitionBuilder <Models.User>();
            var userFilter        = userFilterBuilder.And
                                    (
                userFilterBuilder.Text(search, new TextSearchOptions
            {
                CaseSensitive      = false,
                DiacriticSensitive = false,
            }),
                GeneralUtils.NotDeactivated(userFilterBuilder)
                                    );

            var userSortBuilder = new SortDefinitionBuilder <Models.User>();
            var userSort        = userSortBuilder.MetaTextScore("MetaScore".WithLowercaseFirstCharacter());

            var users = (await userCollection.FindAsync(userFilter, new FindOptions <Models.User>
            {
                Sort = userSort,
                Limit = 50,
                AllowPartialResults = true,
                Projection = userProjection,
            }));

            return(new ResponseBody
            {
                Code = ResponseCode.GenericSuccess,
                Success = true,
                Data = users.ToEnumerable().Select(u => u.BuildUserResponse()),
                Message = "Usuários encontrados com sucesso!",
            });
        }
Example #2
0
        private async Task <IEnumerable <T> > FetchEntities <T>
        (
            string metaPhrase,
            ProjectionDefinition <T> projection,
            IMongoCollection <T> collection,
            double daysTolerance,
            int limit,
            GeoJsonPoint <GeoJson3DGeographicCoordinates> targetPosition = null
        ) where T : IIdentifiable <ObjectId>, ILocatable, IMetaScored
        {
            var referenceDate = DateTime.UtcNow;

            var filterBuilder = new FilterDefinitionBuilder <T>();
            var filter        = filterBuilder.And
                                (
                filterBuilder.Gt
                (
                    post => post._id,
                    new ObjectId(referenceDate.Subtract(TimeSpan.FromDays(daysTolerance)), 0, 0, 0)
                ),
                filterBuilder.Or
                (
                    filterBuilder.Text(metaPhrase, new TextSearchOptions
            {
                CaseSensitive      = false,
                DiacriticSensitive = false,
            })
                    ,
                    filterBuilder.Exists(p => p._id)
                )
                                );

            var sortBuilder = new SortDefinitionBuilder <T>();
            var sort        = sortBuilder.Combine
                              (
                sortBuilder.MetaTextScore("metaScore"),
                sortBuilder.Descending(p => p._id)
                              );

            var cursor = await collection.FindAsync(filter, new FindOptions <T>
            {
                AllowPartialResults = true,
                Limit      = limit,
                Sort       = sort,
                Projection = projection
            });

            var enumerable = cursor.ToEnumerable();

            if (targetPosition != null)
            {
                enumerable = enumerable.OrderBy
                             (
                    item =>
                    item.Position.Coordinates.ToGeoCoordinate().GetDistanceTo(targetPosition.Coordinates.ToGeoCoordinate())
                    -
                    Math.Pow(item.MetaScore, 2)
                             );
            }

            return(enumerable);
        }