示例#1
0
        private static Nest.QueryContainer[] BuildQueryContainer(string query, int?diffFromDays, int?diffToDays)
        {
            var queryNodes    = query.Split(',');
            var listOfQueries = new List <Nest.QueryContainer>();

            // build query
            foreach (var queryNode in queryNodes)
            {
                var queryBase = new Nest.MatchQuery();// MatchPhrasePrefixQuery();

                if (queryNode.IndexOf(':') >= 0)
                {
                    var typeAndValue = queryNode.Split(':');
                    var type         = typeAndValue[0].Trim().ToLower();
                    var value        = typeAndValue[1].Trim();
                    queryBase.Field      = new Nest.Field();
                    queryBase.Field.Name = type;
                    queryBase.Query      = value;
                }
                else
                {
                    // use _allField
                    queryBase.Field      = new Nest.Field();
                    queryBase.Field.Name = "_all";
                    queryBase.Query      = queryNode;
                }
                listOfQueries.Add(new Nest.QueryContainer(queryBase));
            }

            // DateTime Range
            if (diffFromDays.HasValue || diffToDays.HasValue)
            {
                var queryRange = new Nest.DateRangeQuery();
                queryRange.Field                = new Nest.Field();
                queryRange.Field.Name           = "datum";
                queryRange.GreaterThanOrEqualTo = DateTime.Now.ToUniversalTime().Date.AddDays((diffFromDays.HasValue)? diffFromDays.Value : 0);
                queryRange.LessThanOrEqualTo    = DateTime.Now.ToUniversalTime().Date.AddDays((diffToDays.HasValue)? diffToDays.Value : 0);
                listOfQueries.Add(new Nest.QueryContainer(queryRange));
            }

            return(listOfQueries.ToArray());
        }
示例#2
0
        public ActionResult Index()
        {
            var model = new HomeViewModel();

            // DateTime Range
            var queryRange = new Nest.DateRangeQuery();

            queryRange.Field                = new Nest.Field();
            queryRange.Field.Name           = "datum";
            queryRange.GreaterThanOrEqualTo = DateTime.Now.AddDays(-DateTime.Now.Day);
            queryRange.LessThanOrEqualTo    = DateTime.Now.AddDays(0);
            var rangeContainer = new Nest.QueryContainer(queryRange);

            // Name TagCloud
            var nameTagCloudModel = new TagCloudViewModel();

            nameTagCloudModel.Title = "Meist verwendete Namen";
            nameTagCloudModel.Tags  = new Dictionary <string, float>();
            var listOfQueriesTagCloud = new List <Nest.QueryContainer>();

            listOfQueriesTagCloud.Add(rangeContainer);

            var res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesTagCloud.ToArray())
                                                                                      )
                                                                                )
                                                                         .Aggregations(a =>
                                                                                       a.Terms("tagcloud", ta => ta.Field("name"))
                                                                                       )
                                                                         );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                nameTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.NameTagCloud = nameTagCloudModel;

            // DocType TagCloud
            var docTagCloudModel = new TagCloudViewModel();

            docTagCloudModel.Title = "Meist verwendete DocTypes";
            docTagCloudModel.Tags  = new Dictionary <string, float>();

            foreach (var docType in DocTypeRepository.Index())
            {
                var listOfQueriesDocType = new List <Nest.QueryContainer>();
                listOfQueriesDocType.Add(rangeContainer);

                var queryDocType = new Nest.MatchQuery();
                queryDocType.Field      = new Nest.Field();
                queryDocType.Field.Name = "type";
                queryDocType.Query      = docType.Id.ToString();
                var docTypesContainer = new Nest.QueryContainer(queryDocType);
                listOfQueriesDocType.Add(docTypesContainer);

                res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(listOfQueriesDocType.ToArray())
                                                                                      )
                                                                                )
                                                                         );
                docTagCloudModel.Tags.Add(docType.Name, res.Total);
            }
            model.DocTypeTagCloud = docTagCloudModel;

            // Ort TagCloud
            var ortTagCloudModel = new TagCloudViewModel();

            ortTagCloudModel.Title = "Meist verwendete Orte";
            ortTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("ort"))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                ortTagCloudModel.Tags.Add(nestTag.Key, nestTag.DocCount.Value);
            }
            model.OrtTagCloud = ortTagCloudModel;

            // Kategorie TagCloud
            var kategorieTagCloudModel = new TagCloudViewModel();

            kategorieTagCloudModel.Title = "Meist verwendete Kategorien";
            kategorieTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("kategorie")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         ))
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                kategorieTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.KategorieTagCloud = kategorieTagCloudModel;

            // Handel TagCloud
            var handelTagCloudModel = new TagCloudViewModel();

            handelTagCloudModel.Title = "Meist verwendete Handel";
            handelTagCloudModel.Tags  = new Dictionary <string, float>();
            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesTagCloud.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Terms("tagcloud", ta => ta.Field("handel")
                                                                                           .Aggregations(aa =>
                                                                                                         aa.Sum("summe", ts => ts.Field("preis"))
                                                                                                         )
                                                                                           )
                                                                                   )
                                                                     );

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var    nestTag = tag as Nest.KeyedBucket;
                double sum     = 0.0;
                var    sumAggs = (nestTag.Aggregations["summe"] as Nest.ValueAggregate);
                sum = sumAggs.Value.Value;

                handelTagCloudModel.Tags.Add(nestTag.Key, (int)sum);
            }
            model.HandelTagCloud = handelTagCloudModel;

            // Preis Summe

            var listOfQueriesAusgaben = new List <Nest.QueryContainer>();

            listOfQueriesAusgaben.Add(rangeContainer);

            var queryAusgabe = new Nest.MatchQuery();

            queryAusgabe.Field      = new Nest.Field();
            queryAusgabe.Field.Name = "type";
            queryAusgabe.Query      = "e0c4f438-4509-449b-a491-3fa3093b4129";
            var docTypeContainer = new Nest.QueryContainer(queryAusgabe);

            listOfQueriesAusgaben.Add(docTypeContainer);


            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesAusgaben.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("preis"))
                                                                                   )
                                                                     );
            model.SummeAusgaben = res.Aggs.Sum("summe").Value.Value;

            // Einnahmen Summe
            var listOfQueriesEinnahmen = new List <Nest.QueryContainer>();

            listOfQueriesEinnahmen.Add(rangeContainer);
            var queryEinnahme = new Nest.MatchQuery();

            queryEinnahme.Field      = new Nest.Field();
            queryEinnahme.Field.Name = "type";
            queryEinnahme.Query      = "05ce209c-e837-4fc4-b2a4-6b54bf73be46";
            docTypeContainer         = new Nest.QueryContainer(queryEinnahme);
            listOfQueriesEinnahmen.Add(docTypeContainer);

            res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                     .Index("docs")
                                                                     .From(0)
                                                                     .Size(1000)
                                                                     .Query(q => q
                                                                            .Bool(b => b
                                                                                  .Must(listOfQueriesEinnahmen.ToArray())
                                                                                  )
                                                                            )
                                                                     .Aggregations(a =>
                                                                                   a.Sum("summe", aa => aa
                                                                                         .Field("betrag"))
                                                                                   )
                                                                     );
            model.SummeEinnahmen = res.Aggs.Sum("summe").Value.Value;

            return(View(model));
        }
示例#3
0
        public static List <DocViewModel> GetMostRecentDocumentsForDocType(Guid docType, int diffDays)
        {
            // Filter: type
            var queryBase = new Nest.MatchQuery();

            queryBase.Field      = new Nest.Field();
            queryBase.Field.Name = "type";
            queryBase.Query      = docType.ToString();

            var queries = new List <Nest.QueryContainer>();

            queries.Add(new Nest.QueryContainer(queryBase));

            // Filter: DateRange
            var queryRange = new Nest.DateRangeQuery();

            queryRange.Field                = new Nest.Field();
            queryRange.Field.Name           = "datum";
            queryRange.GreaterThanOrEqualTo = DateTime.Now.ToUniversalTime().Date.AddDays(diffDays);
            queryRange.LessThanOrEqualTo    = DateTime.Now.ToUniversalTime().Date.AddDays(0);

            queries.Add(new Nest.QueryContainer(queryRange));

            var res = ElasticClientFactory.Client.Search <ExpandoObject>(s => s
                                                                         .Index("docs")
                                                                         .From(0)
                                                                         .Size(1000)
                                                                         .Query(q => q
                                                                                .Bool(b => b
                                                                                      .Must(queries.ToArray())
                                                                                      )
                                                                                )
                                                                         .Aggregations(a =>
                                                                                       a.Terms("tagcloud", ta => ta.Field("name"))
                                                                                       )
                                                                         );

            var list = new List <dynamic>();

            if (res.Total > 0)
            {
                list = res.Hits.Select(h => h.Source as dynamic).ToList();
            }

            var docViewModelList = new List <DocViewModel>();

            foreach (dynamic doc in list)
            {
                var     docModel = new DocViewModel();
                DocType type     = DocTypeRepository.Read(Guid.Parse(doc.type));
                docModel.Id   = Guid.Parse(doc.id);
                docModel.Type = type.Name;
                try
                {
                    docModel.Name = doc.name;
                }
                catch (Exception e)
                {
                    docModel.Name = "";
                }

                docModel.MetaTags = MetaTagRepository.ReadMany(type.MetaTags);
                docViewModelList.Add(docModel);
            }

            var docsPerNameList = new Dictionary <string, long>();

            foreach (var tag in (res.Aggregations["tagcloud"] as Nest.BucketAggregate).Items)
            {
                var nestTag = tag as Nest.KeyedBucket;
                docsPerNameList.Add(nestTag.Key, nestTag.DocCount.Value);
            }

            var rankedList = new List <DocViewModel>();
            var i          = 0;

            foreach (var docName in docsPerNameList.Keys)
            {
                if (i < 10)
                {
                    var doc = docViewModelList.FirstOrDefault(d => d.Name == docName);
                    doc.DocCountInDb = (int)docsPerNameList[docName];
                    rankedList.Add(doc);
                }
                else
                {
                    break;
                }
            }

            return(rankedList);
        }