Beispiel #1
0
        protected override Expression VisitMember(MemberExpression expression)
        {
            Visit(expression.Expression);
            PropertyName = _propertyNameInferrerParser.Parser(expression.Member.Name);
            PropertyType = expression.Type;

            if (expression.Type == typeof(bool))
            {
                if (!(Value is bool))
                {
                    Value = true;
                }

                if (NodeType == null)
                {
                    VisitBoolProperty(ExpressionType.Equal);
                }
            }

            return(expression);
        }
Beispiel #2
0
        public IEnumerable <T> ExecuteCollection <T>(QueryModel queryModel)
        {
            var queryAggregator = _elasticGeneratorQueryModelVisitor.GenerateElasticQuery <T>(queryModel);

            var documents = _elasticClient.Search <IDictionary <string, object> >(descriptor =>
            {
                descriptor.Index(_dataId);

                if (queryModel.SelectClause != null && queryModel.SelectClause.Selector is MemberExpression memberExpression)
                {
                    descriptor.Source(x => x.Includes(f => f.Field(_propertyNameInferrerParser.Parser(memberExpression.Member.Name))));
                }

                if (queryAggregator.Skip != null)
                {
                    descriptor.From(queryAggregator.Skip);
                }
                else
                {
                    descriptor.Size(10000);
                }

                if (queryAggregator.Take != null)
                {
                    descriptor.Take(queryAggregator.Take);
                    descriptor.Size(queryAggregator.Take);
                }

                if (queryAggregator.QueryContainers.Any())
                {
                    descriptor.Query(q => q.Bool(x => x.Must(queryAggregator.QueryContainers.ToArray())));
                }
                else
                {
                    descriptor.MatchAll();
                }


                if (queryAggregator.OrderByExpressions.Any())
                {
                    descriptor.Sort(d =>
                    {
                        foreach (var orderByExpression in queryAggregator.OrderByExpressions)
                        {
                            var property = _propertyNameInferrerParser.Parser(orderByExpression.PropertyName) +
                                           orderByExpression.GetKeywordIfNecessary();
                            d.Field(property,
                                    orderByExpression.OrderingDirection == OrderingDirection.Asc
                                    ? SortOrder.Ascending
                                    : SortOrder.Descending);
                        }

                        return(d);
                    });
                }

                if (queryAggregator.GroupByExpressions.Any())
                {
                    descriptor.Aggregations(a =>
                    {
                        a.Composite("composite", c =>
                                    c.Sources(so =>
                        {
                            queryAggregator.GroupByExpressions.ForEach(gbe =>
                            {
                                var property = _propertyNameInferrerParser.Parser(gbe.PropertyName) + gbe.GetKeywordIfNecessary();
                                so.Terms($"group_by_{gbe.PropertyName}", t => t.Field(property));
                            });

                            return(so);
                        })
                                    .Aggregations(aa => aa
                                                  .TopHits("data_composite", th => th)
                                                  )
                                    );

                        return(a);
                    });
                }

                return(descriptor);
            });

            if (queryModel.SelectClause?.Selector is MemberExpression)
            {
                return(JsonConvert.DeserializeObject <IEnumerable <T> >(
                           JsonConvert.SerializeObject(documents.Documents.SelectMany(x => x.Values), Formatting.Indented)));
            }

            if (queryAggregator.GroupByExpressions.Any())
            {
                var docDeserializer = new Func <object, TK>(input =>
                                                            JsonConvert.DeserializeObject <TK>(JsonConvert.SerializeObject(input, Formatting.Indented)));

                var originalGroupingType     = queryModel.GetResultType().GenericTypeArguments.First();
                var originalGroupingGenerics = originalGroupingType.GetGenericArguments();
                var originalKeyGenerics      = originalGroupingGenerics.First();

                var genericListType = typeof(List <>).MakeGenericType(originalGroupingType);
                var values          = (IList)Activator.CreateInstance(genericListType);

                var composite = documents.Aggregations.Composite("composite");

                foreach (var bucket in composite.Buckets)
                {
                    var key  = GenerateKey(bucket.Key, originalKeyGenerics);
                    var list = bucket.TopHits("data_composite").Documents <object>().Select(docDeserializer).ToList();

                    var grouping         = typeof(Grouping <,>);
                    var groupingGenerics = grouping.MakeGenericType(originalGroupingGenerics);
                    var groupingInstance = Activator.CreateInstance(groupingGenerics, key, list);
                    values.Add(groupingInstance);
                }

                return(values.Cast <T>());
            }

            var result = JsonConvert.DeserializeObject <IEnumerable <T> >(
                JsonConvert.SerializeObject(documents.Documents, Formatting.Indented));

            return(result);
        }