Ejemplo n.º 1
0
        public async Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var  parentQuery = ctx.GetSourceAs <IParentQuery>();
            bool hasIds      = ctx.GetSourceAs <IIdentityQuery>()?.Ids.Count > 0;

            if (parentQuery == null)
            {
                return;
            }

            var           options       = ctx.GetOptionsAs <IElasticQueryOptions>();
            IQueryOptions parentOptions = null;

            if (options != null && options.HasParent == false)
            {
                return;
            }

            if (options != null && options.ParentSupportsSoftDeletes && hasIds == false)
            {
                if (parentQuery.ParentQuery == null)
                {
                    parentQuery.ParentQuery = new ParentQuery();
                }

                var parentType = options.ChildType.GetParentIndexType();
                if (parentType == null)
                {
                    throw new ApplicationException("ParentIndexTypeName on child index type must match the name of the parent type.");
                }

                parentOptions = new ElasticQueryOptions(parentType);
            }

            if (parentQuery.ParentQuery == null)
            {
                return;
            }

            var parentContext = new QueryBuilderContext <object>(parentQuery.ParentQuery, parentOptions, null, ctx, ContextType.Parent);
            await _queryBuilder.BuildAsync(parentContext).AnyContext();

            if ((parentContext.Query == null || parentContext.Query.IsConditionless) &&
                (parentContext.Filter == null || parentContext.Filter.IsConditionless))
            {
                return;
            }

            ctx.Filter &= new HasParentFilter {
                Query  = parentContext.Query,
                Filter = parentContext.Filter,
                Type   = options?.ChildType?.GetParentIndexType().Name
            };
        }
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var searchQuery = ctx.GetSourceAs <ISearchQuery>();

            if (searchQuery == null)
            {
                return(Task.CompletedTask);
            }

            if (!String.IsNullOrEmpty(searchQuery.Filter))
            {
                var result = _parser.Parse(searchQuery.Filter);
                searchQuery.Filter = GenerateQueryVisitor.Run(AliasedQueryVisitor.Run(result, _aliasMap, ctx), ctx);

                ctx.Filter &= new QueryFilter {
                    Query = new QueryStringQuery {
                        Query           = searchQuery.Filter,
                        DefaultOperator = Operator.And,
                        AnalyzeWildcard = false
                    }.ToContainer()
                };
            }

            if (!String.IsNullOrEmpty(searchQuery.Criteria))
            {
                var result = _parser.Parse(searchQuery.Criteria);
                searchQuery.Criteria = GenerateQueryVisitor.Run(AliasedQueryVisitor.Run(result, _aliasMap, ctx), ctx);

                ctx.Query &= new QueryStringQuery {
                    Query           = searchQuery.Criteria,
                    DefaultOperator = searchQuery.DefaultCriteriaOperator == SearchOperator.Or ? Operator.Or : Operator.And,
                    AnalyzeWildcard = true
                };
            }

            if (!String.IsNullOrEmpty(searchQuery.Sort))
            {
                var result = _parser.Parse(searchQuery.Sort);
                var opt    = ctx.GetOptionsAs <IElasticQueryOptions>();
                TermToFieldVisitor.Run(result, ctx);
                AliasedQueryVisitor.Run(result, _aliasMap, ctx);
                var fields = GetReferencedFieldsQueryVisitor.Run(result);
                // TODO: Check referenced fields against opt.AllowedSortFields

                var sort = GetSortFieldsVisitor.Run(result, ctx);
                ctx.Search.Sort(sort);
            }

            return(Task.CompletedTask);
        }
Ejemplo n.º 3
0
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var selectedFieldsQuery = ctx.GetSourceAs <ISelectedFieldsQuery>();

            if (selectedFieldsQuery?.SelectedFields?.Count > 0)
            {
                ctx.Search.Source(s => s.Include(selectedFieldsQuery.SelectedFields.ToArray()));
                return(Task.CompletedTask);
            }

            var opt = ctx.GetOptionsAs <IElasticQueryOptions>();

            if (opt?.DefaultExcludes?.Count > 0)
            {
                ctx.Search.Source(s => s.Exclude(opt.DefaultExcludes.ToArray()));
            }

            return(Task.CompletedTask);
        }
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var sortableQuery = ctx.GetSourceAs <ISortableQuery>();

            if (sortableQuery?.SortFields == null || sortableQuery.SortFields.Count <= 0)
            {
                return(Task.CompletedTask);
            }

            var opt = ctx.GetOptionsAs <IElasticQueryOptions>();

            // TODO: Check SortFields against opt.AllowedSortFields
            //foreach (var sort in sortableQuery.SortFields.Where(s => CanSortByField(opt?.AllowedSortFields, s.Field.ToString())))
            //    ctx.Search.Sort(s => s.OnField(sort.Field)
            //        .Order(sort.Order == Foundatio.Repositories.Models.SortOrder.Ascending ? SortOrder.Ascending : SortOrder.Descending));

            ctx.Search.Sort(sortableQuery.SortFields);

            return(Task.CompletedTask);
        }
Ejemplo n.º 5
0
        public async Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            var aggregationQuery = ctx.GetSourceAs <IAggregationQuery>();

            if (String.IsNullOrEmpty(aggregationQuery?.Aggregations))
            {
                return;
            }

            var opt = ctx.GetOptionsAs <IElasticQueryOptions>();

            if (opt?.AllowedAggregationFields?.Count > 0 && !(await GetAggregationFieldsAsync(aggregationQuery.Aggregations).AnyContext()).All(f => opt.AllowedAggregationFields.Contains(f)))
            {
                throw new InvalidOperationException("All aggregation fields must be allowed.");
            }

            var result = await _parser.BuildAggregationsAsync(aggregationQuery.Aggregations, ctx).AnyContext();

            ctx.Search.Aggregations(result);
        }
        public Task BuildAsync <T>(QueryBuilderContext <T> ctx) where T : class, new()
        {
            // wait until the system filter query is being built if the query supports it
            if (ctx.Type != ContextType.SystemFilter && ctx.Source is ISystemFilterQuery)
            {
                return(Task.CompletedTask);
            }

            // dont add filter to child query system filters
            if (ctx.Parent?.Type == ContextType.Child)
            {
                return(Task.CompletedTask);
            }

            var mode = ctx.GetSourceAs <ISoftDeletesQuery>()?.SoftDeleteMode;

            // if no mode was specified, then try using the parent query mode
            if (mode == null && ctx.Parent != null)
            {
                mode = ctx.Parent.GetSourceAs <ISoftDeletesQuery>()?.SoftDeleteMode;
            }

            // default to active only if no mode has been specified
            if (mode.HasValue == false)
            {
                mode = SoftDeleteQueryMode.ActiveOnly;
            }

            // no filter needed if we want all
            if (mode.Value == SoftDeleteQueryMode.All)
            {
                return(Task.CompletedTask);
            }

            // check to see if the model supports soft deletes
            var options = ctx.GetOptionsAs <IElasticQueryOptions>();

            if (options == null || !options.SupportsSoftDeletes)
            {
                return(Task.CompletedTask);
            }

            // if we are querying for specific ids then we don't need a deleted filter
            var idsQuery = ctx.GetSourceAs <IIdentityQuery>();

            if (idsQuery != null && idsQuery.Ids.Count > 0)
            {
                return(Task.CompletedTask);
            }

            if (mode.Value == SoftDeleteQueryMode.ActiveOnly)
            {
                ctx.Filter &= new TermFilter {
                    Field = Fields.Deleted, Value = false
                }
            }
            ;
            else if (mode.Value == SoftDeleteQueryMode.DeletedOnly)
            {
                ctx.Filter &= new TermFilter {
                    Field = Fields.Deleted, Value = true
                }
            }
            ;

            return(Task.CompletedTask);
        }