160 likes | 366 Views
Zoeken met Sitecore 7. Egbert Wietses. pionect.nl. Waar komen we vandaan?. http://marketplace.sitecore.net/en/Modules/Sitecore_Item_Buckets.aspx. Item buckets. Niet zichtbaar (standaard) Theoretisch ongelimiteerd aantal subitems Geen parent child relatie. Handig om te weten.
E N D
Zoeken met Sitecore 7 Egbert Wietses pionect.nl
Waar komen we vandaan? • http://marketplace.sitecore.net/en/Modules/Sitecore_Item_Buckets.aspx
Item buckets • Niet zichtbaar (standaard) • Theoretisch ongelimiteerd aantal subitems • Geen parent child relatie
Handig om te weten • <setting name="BucketConfiguration.BucketFolderPath" value=„yyyy\/MM\/dd\/HH\/mm”/> • <field fieldName="group" storageType="YES" indexType="TOKENIZED" vectorType="NO" boost="1f" type="System.GUID" settingType="Sitecore.ContentSearch.LuceneProvider.LuceneSearchFieldConfiguration, Sitecore.ContentSearch.LuceneProvider”> • Luke = your friendhttps://code.google.com/p/luke/
Zoeken • using (var context = ContentSearchManager.GetIndex(bucketItem as IIndexable).CreateSearchContext()) • { • var querySearch = context.GetQueryable<Models.Product>(); • querySearch = querySearch.Where(item => item._language.Equals("en")); • querySearch = querySearch.Where(item => item._templatename.Equals(MasterProductPage.TEMPLATE_NAME)); • querySearch = querySearch.Where(item => item.brand.Equals("TRUST")); • var products = querySearch.ToList(); • } • public class Product: SearchResultItem • { • public string _group { get; set; } • public string _name { get; set; } • public string _language { get; set; } • public string _templatename { get; set; } • public string color { get; set; } • public string device { get; set; } • public string feature { get; set; } • public string operatingSystem { get; set; } • public string compatibility { get; set; } • }
Linq to Sitecore = gold • All • Any • Between — with an extra overload for including or excluding the start and end ranges. • Boost — makes this part of the query more important than the other parts. • Cast — you can use this instead of Select. • Contains • Count • ElementAt • EndsWith • Equal • Facets — an extension that fetches the facets of predicates. • First • FirstOrDefault • Last • LastOrDefault • Min • Max • Match — an extension for running regular expression queries. • OrderBy • OrderByDescending • Select • Single • SingleOrDefault • Skip • Reverse • Take • ToList() • ToLookUp() • ToDictionary() • Page — an extension that does Skip and Take automatically for you. • StartsWith • Union
Facet implementatie • public static IQueryable<Facet> GetFacets(IQueryable<Product> query, string[] facets) • { • var facetGroups = new List<Facet>(); • query = facets.Aggregate(query, (current, facetName) => current.FacetOn(c => c[facetName])); • FacetResults facetResults = query.GetFacets(); • foreach (FacetCategory category in facetResults.Categories) • { • var group = new Facet(key, friendlyName); • foreach (Sitecore.ContentSearch.Linq.FacetValue facetValue in category.Values) • { • group.Values.Add(new Models.FacetValue(facetValue.Name, facetValue.Aggregate)); • } • facetGroups.Add(group); • } • return facetGroups.AsQueryable(); • }
1 Call • using (var context = ContentSearchManager.GetIndex("sitecore_master_index").CreateSearchContext()) • { • var results = context.GetQueryable<Product>().Where(prod => prod.Content.Contains("search box text").FacetOn(f => f.Color).FacetOn(f => f.Gender)).GetResults(); • int totalHits = results.TotalHits; var searchResuls = results.Hits; var facets = results.Facets; • }
namespace Sitecore.ContentSearch.Linq • { • public static class QueryableExtensions • { • public static IQueryable<TSource> FacetOn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector); • public static IQueryable<TSource> FacetOn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, int minimumResultCount); • public static IQueryable<TSource> FacetOn<TSource, TKey>(this IQueryable<TSource> source, Expression<Func<TSource, TKey>> keySelector, int minimumResultCount, IEnumerable<object> filterValues); • public static IQueryable<TSource> FacetPivotOn<TSource>(this IQueryable<TSource> source, Expression<Func<FacetPivotQuery<TSource>, FacetPivotQuery<TSource>>> keySelector); • public static IQueryable<TSource> FacetPivotOn<TSource>(this IQueryable<TSource> source, Expression<Func<FacetPivotQuery<TSource>, FacetPivotQuery<TSource>>> keySelector, int minimumResultCount); • public static IQueryable<TSource> Filter<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate); • public static FacetResults GetFacets<TSource>(this IQueryable<TSource> source); • public static SearchResults<TSource> GetResults<TSource>(this IQueryable<TSource> source); • public static SearchResults<TSource> GetResults<TSource>(this IQueryable<TSource> source, GetResultsOptions options); • public static IQueryable<TSource> Page<TSource>(this IQueryable<TSource> source, int page, int pageSize); • } • }
Linq voorbeeld • resultsQuery = resultsQuery.Where(item => • item.Path.StartsWith("/sitecore/content/sites/autohouse") • && !item.ExcludeFromSearchResults • && (item.TemplateName == ContentPage.TEMPLATE_NAME • || item.TemplateName == NewsDetailPage.TEMPLATE_NAME • || (item.TemplateName == MasterProductPage.TEMPLATE_NAME • && item.brand.Equals("Brand X") • && (item.useforsupport.Equals("1") || item.useforconsumerassortment.Equals("1"))) • || (item.TemplateName == ColorProductPage.TEMPLATE_NAME • && item.brand.Equals("Brand X") • && (item.useforsupport.Equals("1") || item.useforconsumerassortment.Equals("1"))) • ) • && item.Language.StartsWith(lang)).OrderByDescending(x => x.itemno);
Predicate Builder = one to watch • OR AND XOR • Controleren tegen lijst van iD’s
In de praktijk • Luke = your friend
Achtergrond informatie • Sitecore 7 Autohaus projecthttps://github.com/Sitecore/autohaus • Developers guide to item buckets and searchhttps://sdn.sitecore.net/Reference/Sitecore%207/Developers%20Guide%20to%20Item%20Buckets%20and%20Search.aspx