Exemplo n.º 1
0
        public static DocType Delete(Guid id)
        {
            var doc    = DocTypeRepository.Read(id);
            var resDel = ElasticClientFactory.Client
                         .Delete <DocType>(doc);

            ElasticClientFactory.Client.Refresh("doctypes");

            return(doc);
        }
Exemplo n.º 2
0
        public static DocType Update(Guid id, string name, string description, List <Guid> metaTags)
        {
            var doc = DocTypeRepository.Read(id);

            doc.Name        = name;
            doc.Description = description;
            doc.MetaTags    = (metaTags != null) ? metaTags : new List <Guid>();

            var response = ElasticClientFactory.Client.Index(doc);

            ElasticClientFactory.Client.Refresh("doctypes");
            return(doc);
        }
Exemplo n.º 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);
        }