Exemplo n.º 1
0
        public virtual IList <FacetValue> ProcessFacetValues(FacetItem facet, IList <Sitecore.ContentSearch.Linq.FacetValue> options, IEnumerable <string> selected)
        {
            options  = options ?? new List <Sitecore.ContentSearch.Linq.FacetValue>();
            selected = selected ?? Enumerable.Empty <string>();

            var values = new List <FacetValue>();

            foreach (var option in options)
            {
                var value = new FacetValue()
                {
                    Name       = option.Name,
                    Count      = option.AggregateCount,
                    IsSelected = selected.Contains(option.Name)
                };
                values.Add(value);
            }

            values = SortFacetValues(values, facet.SortBy);

            if (facet.LimitValues > 0)
            {
                values = values.Take(facet.LimitValues).ToList();
            }

            return(values);
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Edit(int?id, string name)
        {
            using var db = AppContextFactory.DB;

            OntologyEditViewModel model = new OntologyEditViewModel();

            await TryUpdateModelAsync <OntologyEditViewModel>(model, "", i => i.FacetId, i => i.ElementName);

            if (db.FacetItems.Any(i => i.FacetId == model.FacetId && i.Name == model.ElementName))
            {
                ModelState.AddModelError("Error", "Такой элемент уже есть!");
            }

            if (!ModelState.IsValid)
            {
                return(View(new OntologyEditViewModel()
                {
                    ElementName = model.ElementName,
                    Facets = (from f in db.Facets
                              select new SelectListItem()
                    {
                        Text = f.Name,
                        Value = f.Id.ToString(),
                        Selected = model.FacetId == f.Id
                    }).ToList()
                }));
            }

            using var t = db.Database.BeginTransaction();

            try
            {
                FacetItem fi = new FacetItem();

                if (id.HasValue)
                {
                    fi = db.FacetItems.FirstOrDefault(i => i.Id == id.Value) ?? new FacetItem();
                }

                fi.FacetId = model.FacetId;
                fi.Name    = model.ElementName;

                if (fi.Id == 0)
                {
                    db.Entry(fi).State = Microsoft.EntityFrameworkCore.EntityState.Added;
                }

                await db.SaveChangesAsync();

                await t.CommitAsync();
            }
            catch (Exception ex)
            {
                await t.RollbackAsync();

                ModelState.AddModelError("Error", "В процессе сохранения произошла ошибка! Ошибка: " + ex.Message);
            }

            return(RedirectToAction("Index"));
        }
Exemplo n.º 3
0
        public virtual Facet ProcessFacet(FacetItem facet, MapResultArgs args)
        {
            // Don't even send facet if no value options exist
            var options = args.FacetResults?.FirstOrDefault(x => x.Name == facet.IndexProperty)?.Values;

            if (options == null || !options.Any())
            {
                return(null);
            }

            var facetModel = new Facet
            {
                FieldName          = facet.IndexProperty,
                ID                 = facet.ID.Guid,
                Name               = facet.DisplayName,
                Type               = facet.InnerItem.TemplateName,
                CollapsedByDefault = facet.CollapsedByDefault
            };

            var selected = args.SearchParameters.SelectedFacets?.FirstOrDefault(x => x.FacetId == facet.ID.Guid)?.SelectedValues;

            facetModel.Values = ProcessFacetValues(facet, options, selected);

            return(facetModel);
        }
Exemplo n.º 4
0
        private static dynamic GetLamdaExpression <T, TProp>(FacetItem item) where T : IContent
        {
            var expression = item.PropertyId.GetLamdaExpression <T>(item.PropertyType);

            // Add the boxing operation, but get a weakly typed expression
            var converted = Expression.Convert
                                (expression.Body, typeof(object));

            // Use Expression.Lambda to get back to strong typing
            return(Expression.Lambda <Func <T, TProp> >
                       (converted, expression.Parameters));
        }
Exemplo n.º 5
0
        public IActionResult Edit(int?id)
        {
            using var db = AppContextFactory.DB;

            OntologyEditViewModel model = new OntologyEditViewModel()
            {
                Facets = new List <SelectListItem>()
            };

            model.Facets.Add(new SelectListItem()
            {
                Selected = true, Text = "[ Выберите класс ]"
            });
            model.Facets.AddRange(from f in db.Facets
                                  where new string[] { "skills", "subjects" }.Contains(f.Code)
                                  select new SelectListItem()
            {
                Text  = f.Name,
                Value = f.Id.ToString()
            });


            FacetItem fi = new FacetItem();

            if (id.HasValue)
            {
                fi = db.FacetItems.FirstOrDefault(i => i.Id == id) ?? new FacetItem();
            }

            if (fi.Id != 0)
            {
                model.Facets.FirstOrDefault(i => i.Value == fi.FacetId.ToString()).Selected = true;
                model.FacetId     = fi.FacetId;
                model.ElementName = fi.Name;
            }

            return(View(model));
        }
 public void Init()
 {
     instance = new FacetItem();
 }
Exemplo n.º 7
0
 private static dynamic GetLamdaExpression <T>(FacetItem item) where T : IContent
 {
     return(item.PropertyId.GetLamdaExpression <T>(item.PropertyType));
 }
Exemplo n.º 8
0
        public IEnumerable <IHit> Query(int pageIndex, int pageSize, out int totalCount, out IEnumerable <FacetGroup> facetedResults)
        {
            totalCount     = 0;
            facetedResults = null;

            if (searchPaths == null || searchPaths.Count <= 0)
            {
                searchPaths.AddRange(indexPaths.Values.Select(o => o.Path));
            }

            List <LuceneHit> results = new List <LuceneHit>();

            List <IndexSearcher> subSearchs = new List <IndexSearcher>();

            searchPaths.ForEach(o => subSearchs.Add(new IndexSearcher(FSDirectory.Open(o))));

            if (facetFields != null && facetFields.Count > 0)
            {
                var         facetGroups     = new List <FacetGroup>();
                var         mainQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(query));
                MultiReader readers         = new MultiReader(subSearchs.Select(o => o.IndexReader).ToArray());

                foreach (var facetField in facetFields)
                {
                    FacetGroup fg = new FacetGroup();
                    fg.FieldName = facetFieldNameProvider.GetMapName(TypeName, facetField);
                    var items = new List <FacetItem>();

                    var allDistinctField = FieldCache_Fields.DEFAULT.GetStrings(readers, facetField).Distinct().ToArray();
                    int totalHits        = 0;

                    Parallel.ForEach(allDistinctField, fieldValue =>
                    {
                        //foreach (var fieldValue in allDistinctField)
                        //{
                        var facetQuery       = new TermQuery(new Term(facetField, fieldValue));
                        var facetQueryFilter = new CachingWrapperFilter(new QueryWrapperFilter(facetQuery));

                        var bs = new OpenBitSetDISI(facetQueryFilter.GetDocIdSet(readers).Iterator(), readers.MaxDoc);
                        bs.InPlaceAnd(mainQueryFilter.GetDocIdSet(readers).Iterator());
                        int count = (Int32)bs.Cardinality();

                        FacetItem item  = new FacetItem();
                        item.GroupValue = fieldValue;
                        item.Count      = count;

                        items.Add(item);
                        totalHits += count;
                    }
                                     );

                    fg.FacetItems = items.OrderByDescending(o => o.Count);
                    fg.TotalHits  = totalHits;

                    facetGroups.Add(fg);
                }

                facetedResults = facetGroups.OrderBy(o => o.FieldName);
            }
            ParallelMultiSearcher searcher = new ParallelMultiSearcher(subSearchs.ToArray());
            Sort sort = null;

            if (sortFields != null && sortFields.Count > 0)
            {
                sort = new Sort(sortFields.ToArray());
            }

            int maxDoc     = searcher.MaxDoc;
            int startIndex = 0;

            if (pageIndex >= 0 && pageSize > 0)
            {
                startIndex = pageIndex * pageSize;
                maxDoc     = pageSize * (pageIndex + 1);
            }
            var docs = sort == null?searcher.Search(query, null, maxDoc) : searcher.Search(query, null, maxDoc, sort);

            totalCount = docs.TotalHits;
            int endIndex = docs.TotalHits - startIndex;

            for (int i = startIndex; i < endIndex; i++)
            {
                LuceneHit h = new LuceneHit(TypeName, DocumentBuilder, searcher.Doc(docs.ScoreDocs[i].Doc));
                results.Add(h);
            }
            return(results);
        }