public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var dateRanges = ctx.Source.GetDateRanges(); if (dateRanges.Count <= 0) { return(Task.CompletedTask); } var resolver = ctx.GetMappingResolver(); foreach (var dateRange in dateRanges.Where(dr => dr.UseDateRange)) { var rangeQuery = new DateRangeQuery { Field = resolver.ResolveFieldName(dateRange.Field) }; if (dateRange.UseStartDate) { rangeQuery.GreaterThanOrEqualTo = dateRange.GetStartDate(); } if (dateRange.UseEndDate) { rangeQuery.LessThanOrEqualTo = dateRange.GetEndDate(); } if (!String.IsNullOrEmpty(dateRange.TimeZone)) { rangeQuery.TimeZone = dateRange.TimeZone; } ctx.Filter &= rangeQuery; } return(Task.CompletedTask); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var sortFields = ctx.Source.GetSorts(); if (sortFields.Count <= 0) { return(Task.CompletedTask); } var resolver = ctx.GetMappingResolver(); sortFields = resolver.GetResolvedFields(sortFields); ctx.Search.Sort(sortFields); return(Task.CompletedTask); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var resolver = ctx.GetMappingResolver(); var includes = ctx.Source.GetIncludes(); includes = resolver.GetResolvedFields(includes); if (includes.Count > 0) { ctx.Search.Source(s => s.Includes(i => i.Fields(includes.ToArray()))); } var excludes = ctx.Source.GetExcludes(); excludes = resolver.GetResolvedFields(excludes); if (excludes.Count > 0) { ctx.Search.Source(s => s.Excludes(i => i.Fields(excludes.ToArray()))); } return(Task.CompletedTask); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { if (!ctx.Options.ShouldUseSearchAfterPaging()) { return(Task.CompletedTask); } var resolver = ctx.GetMappingResolver(); string idField = resolver.GetResolvedField(Id) ?? "_id"; var searchRequest = ctx.Search as ISearchRequest; if (searchRequest == null) { return(Task.CompletedTask); } searchRequest.Sort ??= new List <ISort>(); var sortFields = searchRequest.Sort; // ensure id field is always added to the end of the sort fields list if (!sortFields.Any(s => resolver.GetResolvedField(s.SortKey).Equals(idField))) { sortFields.Add(new FieldSort { Field = idField }); } // reverse sort orders on all sorts if (ctx.Options.HasSearchBefore()) { sortFields.ReverseOrder(); } return(Task.CompletedTask); }
public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new() { var resolver = ctx.GetMappingResolver(); var fieldConditions = ctx.Source.SafeGetCollection <FieldCondition>(FieldConditionQueryExtensions.FieldConditionsKey); if (fieldConditions == null || fieldConditions.Count <= 0) { return(Task.CompletedTask); } foreach (var fieldValue in fieldConditions) { QueryBase query; if (fieldValue.Value == null && fieldValue.Operator == ComparisonOperator.Equals) { fieldValue.Operator = ComparisonOperator.IsEmpty; } else if (fieldValue.Value == null && fieldValue.Operator == ComparisonOperator.NotEquals) { fieldValue.Operator = ComparisonOperator.HasValue; } switch (fieldValue.Operator) { case ComparisonOperator.Equals: if (fieldValue.Value is IEnumerable && !(fieldValue.Value is string)) { query = new TermsQuery { Field = resolver.GetResolvedField(fieldValue.Field), Terms = (IEnumerable <object>)fieldValue.Value } } ; else { query = new TermQuery { Field = resolver.GetResolvedField(fieldValue.Field), Value = fieldValue.Value } }; ctx.Filter &= query; break; case ComparisonOperator.NotEquals: if (fieldValue.Value is IEnumerable && !(fieldValue.Value is string)) { query = new TermsQuery { Field = resolver.GetResolvedField(fieldValue.Field), Terms = (IEnumerable <object>)fieldValue.Value } } ; else { query = new TermQuery { Field = resolver.GetResolvedField(fieldValue.Field), Value = fieldValue.Value } }; ctx.Filter &= new BoolQuery { MustNot = new QueryContainer[] { query } }; break; case ComparisonOperator.IsEmpty: ctx.Filter &= new BoolQuery { MustNot = new QueryContainer[] { new ExistsQuery { Field = resolver.GetResolvedField(fieldValue.Field) } } }; break; case ComparisonOperator.HasValue: ctx.Filter &= new ExistsQuery { Field = resolver.GetResolvedField(fieldValue.Field) }; break; } } return(Task.CompletedTask); }