Esempio n. 1
0
        public static FilterDefinition <MongoContentEntity> Build(ODataUriParser query, Schema schema)
        {
            var search = query.ParseSearch();

            if (search != null)
            {
                return(Filter.Text(SearchTermVisitor.Visit(search.Expression).ToString()));
            }

            var filter = query.ParseFilter();

            if (filter != null)
            {
                return(FilterVisitor.Visit(filter.Expression, schema));
            }

            return(null);
        }
Esempio n. 2
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!",
            });
        }
Esempio n. 3
0
        public FilterDefinition <T> GetFromCompareType <T>(FilterCondition s)
        {
            FilterDefinitionBuilder <T> filter = Builders <T> .Filter;

            if (s.Value == null)
            {
                return(filter.Empty);
            }

            switch (s.CompareType)
            {
            case TableCompareType.EQ:
                return(filter.Eq(s.Key, s.Value));

            case TableCompareType.GT:
                return(filter.Gt(s.Key, s.Value));

            case TableCompareType.GTE:
                return(filter.Gte(s.Key, s.Value));

            case TableCompareType.LT:
                return(filter.Lt(s.Key, s.Value));

            case TableCompareType.LTE:
                return(filter.Lte(s.Key, s.Value));

            case TableCompareType.NE:
                return(filter.Ne(s.Key, s.Value));

            case TableCompareType.REGEX:
            {
                return(filter.Regex(s.Key, s.Value.ToString()));
            }

            case TableCompareType.TEXT:
                return(filter.Text(s.Value.ToString()));

            default:
                return(filter.Eq(s.Key, s.Value));
            }
        }
Esempio n. 4
0
        public static FilterDefinition <MongoContentEntity> Build(ODataUriParser query, Schema schema)
        {
            SearchClause search;

            try
            {
                search = query.ParseSearch();
            }
            catch (ODataException ex)
            {
                throw new ValidationException("Query $search clause not valid.", new ValidationError(ex.Message));
            }

            if (search != null)
            {
                return(Filter.Text(SearchTermVisitor.Visit(search.Expression).ToString()));
            }

            FilterClause filter;

            try
            {
                filter = query.ParseFilter();
            }
            catch (ODataException ex)
            {
                throw new ValidationException("Query $filter clause not valid.", new ValidationError(ex.Message));
            }

            if (filter != null)
            {
                return(FilterVisitor.Visit(filter.Expression, schema));
            }

            return(null);
        }
Esempio n. 5
0
 public object Text(string search, string language = null, bool caseSensitive = false, bool diacriticSensitive = false)
 {
     return(InternalBuilder.Text(search, new TextSearchOptions {
         Language = language, CaseSensitive = caseSensitive, DiacriticSensitive = diacriticSensitive
     }));
 }
Esempio n. 6
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);
        }