Пример #1
0
        public SearchResults(string searchStr, string[] facets)
        {
            // if we don't have a searchStr for some reason...
              if (searchStr == string.Empty) searchStr = "*";

              List<Item> ResultsList = new List<Item>();

              using (var context = SiteConfiguration.GetSearchContext(Sitecore.Context.Item))
              {
            // Start the search query building
            var query = context.GetQueryable<SitecoreItem>().Where(item => item.Path.StartsWith(Sitecore.Context.Site.StartPath));

            // we will split the spaces and require all words to be in the index.
            foreach (string word in searchStr.Split(' '))
            {
              query = query.Where(item => item.Title.Contains(word) || item.Content.Contains(word));
            }

            if (facets != null)
            {
              foreach (string facet in facets)
              {
            if (facet.Contains("|"))
            {
              string[] values = facet.Split('|');
              string val = values[1];
              if (values[0] == SiteConfiguration.GetDictionaryText("Type")) { query = query.Where(item => item.TemplateName.Equals(val)); }
              if (values[0] == SiteConfiguration.GetDictionaryText("Tags")) { query = query.Where(item => item.Tags.Equals(val)); }
            }
              }
            }

            var results = query
              .Filter(item => item.Language == Sitecore.Context.Language.Name)
              .Filter(item => item.HasPresentation)
              .Filter(item => item.ShowInSearchResults)
              .FacetOn(item => item.Tags).FacetOn(item => item.TemplateName)
              .GetResults();

            this.Results = new List<SimpleItem>();
            this.Facets = new List<Facet>();

            this.SearchString = searchStr;
            foreach (SearchHit<SitecoreItem> result in results.Hits)
            {
              this.Results.Add(new SimpleItem(result.Document.GetItem()));
            }

            foreach (FacetCategory fc in results.Facets.Categories)
            {
              Facet f = new Facet();
              f.Items = new List<FacetItem>();

              if (fc.Name == "_templatename")
              {
            f.FacetName = SiteConfiguration.GetDictionaryText("Type");
            foreach (var a in fc.Values)
            {
              f.Items.Add(new FacetItem(a.Name, String.Format("{0} ({1})", a.Name, a.AggregateCount), false));
            }
              }

              if (fc.Name == "__semantics")
              {
            f.FacetName = SiteConfiguration.GetDictionaryText("Tags");
            foreach (var a in fc.Values)
            {
              Item tag = Sitecore.Context.Database.GetItem(new ID(a.Name));
              f.Items.Add(new FacetItem(a.Name, String.Format("{0} ({1})", tag.Name, a.AggregateCount), false));
            }
              }

              // set the correct facet entries to selected
              if (facets != null)
              {
            foreach (string facet in facets)
            {
              if (facet.Contains("|"))
              {
                string[] values = facet.Split('|');
                if (values[0] == f.FacetName)
                {
                  foreach (FacetItem fi in f.Items) { if (fi.Id == values[1]) fi.Selected = true; }
                }
              }
            }
              }

              if (f.Items.Count > 0) this.Facets.Add(f);
            }
              }
        }