Exemple #1
0
        public static FilterContainer GenerateComparisonFilter <T>(double value, ExpressionType type, string fieldName)
            where T : class
        {
            var filterDescriptor = new FilterDescriptor <T>();

            if (type == ExpressionType.LessThan)
            {
                return(filterDescriptor.Range(x => x.Lower(value).OnField(fieldName)));
            }
            else if (type == ExpressionType.GreaterThan)
            {
                return(filterDescriptor.Range(x => x.Greater(value).OnField(fieldName)));
            }
            else if (type == ExpressionType.LessThanOrEqual)
            {
                return(filterDescriptor.Range(x => x.LowerOrEquals(value).OnField(fieldName)));
            }
            else if (type == ExpressionType.GreaterThanOrEqual)
            {
                return(filterDescriptor.Range(x => x.GreaterOrEquals(value).OnField(fieldName)));
            }
            throw new NotImplementedException();
        }
        /// <summary>
        /// Querying using ranges
        /// </summary>
        /// <param name="customer"></param>
        /// <returns></returns>
        public List <DTO.Customer> QueryUsingRanges(DTO.RangeFilter filter)
        {
            FilterContainer[] ranges = new FilterContainer[2];

            //Build Elastic range filtering object for "Enrollment Fee":
            FilterDescriptor <DTO.Customer> clause1 = new FilterDescriptor <DTO.Customer>();

            ranges[0] = clause1.Range(r => r.OnField(f =>
                                                     f.EnrollmentFee).Greater(filter.EnrollmentFeeStart)
                                      .Lower(filter.EnrollmentFeeEnd));

            //Build Elastic range filtering object for "Birthday":
            FilterDescriptor <DTO.Customer> clause2 = new FilterDescriptor <DTO.Customer>();

            ranges[1] = clause2.Range(r => r.OnField(f => f.Birthday)
                                      .Greater(filter.Birthday.ToString(DTO.Constants.BASIC_DATE)));

            //Run the combined query:
            var hits = _EsClientDAL.Current
                       .Search <DTO.Customer>(s => s
                                              .Query(q => q
                                                     .Filtered(fq => fq
                                                               .Query(qq => qq.MatchAll())
                                                               .Filter(ff => ff
                                                                       .Bool(b => b
                                                                             .Must(ranges)
                                                                             )
                                                                       )
                                                               )
                                                     )
                                              ).Hits;


            //Translate the hits and return the list
            List <DTO.Customer> typedList = hits.Select(hit => ConvertHitToCustumer(hit)).ToList();

            return(typedList);
        }
Exemple #3
0
        SearchResult <T> search <D>(string q, Expression <Func <T, bool> > filter, SearchParameters searchParameters)
        {
            SearchResult <T> searchResult = new SearchResult <T>();
            int limit = searchParameters.Limit == 0 ? 10 : searchParameters.Limit;
            int from  = (searchParameters.PageNumber - 1) * limit;
            ISearchResponse <T> result;
            //string[] types = searchParameters.Types.Select(t => t.FullName).ToArray();

            SearchDescriptor <T> descriptor = new SearchDescriptor <T>();

            descriptor = descriptor.Types(searchParameters.Types)
                         .From(from)
                         .Size(limit);
            descriptor = descriptor.Filter(f => f.Term("accountID", searchParameters.AccountId));

            if (typeof(T).Equals(typeof(Campaign)))
            {
                if (searchParameters.SortField == ContactSortFieldType.CampaignClickrate)
                {
                    descriptor = descriptor.Sort(s => s.OnField("uniqueClicks").Descending().UnmappedType(Nest.FieldType.None));
                }
                else if (!searchParameters.SortFields.IsAny())
                {
                    descriptor = descriptor.Sort(s => s.OnField("lastUpdatedOn").Descending().UnmappedType(Nest.FieldType.None));
                }
            }
            else if (typeof(T).Equals(typeof(Form)) && !searchParameters.SortFields.IsAny())
            {
                descriptor = descriptor.Sort(s => s.OnField("lastModifiedOn").Descending().UnmappedType(Nest.FieldType.None));
            }
            else if (typeof(T).Equals(typeof(Opportunity)) && !searchParameters.SortFields.IsAny())
            {
                descriptor = descriptor.Sort(s => s.OnField("createdOn").Descending().UnmappedType(Nest.FieldType.None));
            }

            if (searchParameters != null && searchParameters.SortFields.IsAny())
            {
                //todo multi-level sorting.
                string sortField = searchParameters.SortFields.First();
                if (searchParameters.SortDirection == System.ComponentModel.ListSortDirection.Ascending)
                {
                    descriptor = descriptor.Sort(s => s.OnField(sortField.ToCamelCase()).Ascending().UnmappedType(Nest.FieldType.None));
                }
                else
                {
                    descriptor = descriptor.Sort(s => s.OnField(sortField.ToCamelCase()).Descending().UnmappedType(Nest.FieldType.None));
                }
            }

            if (!string.IsNullOrEmpty(q))
            {
                descriptor = descriptor.Query(query => query.QueryString(qs => qs.Query(q)));
            }

            if (filter != null)
            {
                string createddate = typeof(T).Equals(typeof(Form)) ? "createdDate" : (typeof(T).Equals(typeof(Campaign)) ? "createdDate" : "createdOn");
                Expression <Func <T, bool> > expression = filter;
                parseExpression(expression.Body);
                FilterContainer      baseFilter         = stack.Pop();
                FilterDescriptor <T> filterD            = new FilterDescriptor <T>();
                FilterContainer      accountIDFilter    = filterD.Term("accountID", searchParameters.AccountId);
                FilterContainer      dateRangeContainer = new FilterContainer();
                if (searchParameters.StartDate.HasValue && searchParameters.EndDate.HasValue)
                {
                    FilterDescriptor <T> dateRangeFilter = new FilterDescriptor <T>();
                    dateRangeContainer = dateRangeFilter.Range(r =>
                                                               r.GreaterOrEquals(searchParameters.StartDate.Value.ToJSDate()).
                                                               LowerOrEquals(searchParameters.EndDate.Value.ToJSDate()).OnField(createddate));
                }
                descriptor = descriptor.Filter(f => f.And(baseFilter, accountIDFilter, dateRangeContainer));
            }

            if (searchParameters != null && searchParameters.Ids.IsAny())
            {
                if (!string.IsNullOrEmpty(q))
                {
                    descriptor = descriptor.Filter(ds => ds.Terms("id", searchParameters.Ids, TermsExecution.Plain));
                }
                else
                {
                    descriptor = descriptor.Query(query => query.Ids(searchParameters.Ids.Select(i => i.ToString()).ToList()));
                }
            }



            result = ElasticClient().Search <T>(body => descriptor);
            string que = result.ConnectionStatus.ToString();

            Logger.Current.Verbose(que);

            searchResult.Results   = result.Documents;
            searchResult.TotalHits = result.Total;

            return(searchResult);
        }