Basics
Terms & Phrases |
"test" or "hello dolly" |
c.Where("test") or c.Where("hello dolly") or c.Where("test" || "hello dolly")
|
Fields |
title:"The Right way" and text:"go" |
c.Title == "The Right way" or c.Text == "go" or c.Equals("go")
|
WildCard |
amber |
c.ContactName.Contains ("amber")
|
Prefix |
amber amber |
c.ContactName.StartsWith("amber") or c.ContactName.EndsWith("amber") |
Fuzzy |
roam or roam0.8 |
c.ContactName.Like("roam") or c.ContactName.Like("roam", 0.8)
|
Proximity |
"jakarta apache"~10 |
c.ContactName.Like("jakarta apache", 10)
|
Inclusive Range |
mod_date:[20020101 TO 20030101] |
c.ModifiedDate.Between("20020101", "20030101", Inclusion.Both)
|
Exclusive Range |
title:{Aida TO Carmen} |
c.Title.Between("Aida", "Carmen", Inclusion.None)
|
Boosting |
jakarta^4 apache |
c.Title.Equals("jakarta".Boost(4)) || c.Title.Equals(“apache”)
|
Boolean Or |
"jakarta apache" OR jakarta |
where c.Title.Equals("jakarta apache") || c.Equals("jakarta")
|
Boolean And |
"jakarta apache" AND "Apache Lucene" |
where c.Equals("jakarta apache") && c.Equals("Apache Lucene")
|
Boolean Not |
"jakarta apache" NOT "Apache Lucene" |
where c.Equals("jakarta apache") && !c.Equals("Apache Lucene")
|
Grouping |
(jakarta OR apache) AND website |
where (c.Title == "jakarta" || c.Title == "apache") && (c.Title == "website")
|
Supported IQueryable methods
Restriction Operators |
Where |
var results = from d in queryable where d.Name == "Sitecore" select d; or var results = queryable.Where(d => d.Name == "Sitecore");
|
Projection Operators |
Select |
var results = from d in queryable select d.Name; or var results = queryable.Select(d => d.Name);
|
Anonymous types |
|
results = queryable.Select(d => new { d.Name, d.Id });
|
Unsupported |
SelectMany |
Partitioning Operators |
Take |
results = queryable.Take(10);
|
Partitioning Operators |
Skip |
results = queryable.Skip(10);
|
Partitioning Operators |
Page |
results = queryable.Page(2, 100);
|
Ordering Operators |
OrderBy |
results = queryable.OrderBy(d => d.Name); |
Ordering Operators |
OrderBy Descending |
results = queryable.OrderByDescending(d => d.Name); |
Ordering Operators |
ThenBy |
results = queryable.OrderBy(d => d.Name).ThenBy(d => d.Id); |
Ordering Operators |
ThenBy Descending |
results = queryable.OrderBy(d => d.Name).ThenByDescending(d => d.Id); |
Unsupported |
Reverse |
Grouping Operators |
Unsupported |
GroupBy -Simple 1 GroupBy -Simple 2 GroupBy -Simple 3 GroupBy -Nested GroupBy -Comparer GroupBy -Comparer, Mapped |
Set Operators |
Unsupported |
Distinct Union Intersect Except |
Element Operators |
First -Simple |
results = queryable.First();
|
Element Operators |
First -Condition |
results = queryable.First(d => d.Name == "Sitecore");
|
Element Operators |
FirstOrDefault -Simple |
results = queryable.FirstOrDefault();
|
Element Operators |
FirstOrDefault -Condition |
results = queryable.FirstOrDefault(d => d.Name == "Sitecore");
|
Element Operators |
ElementAt |
results = queryable.ElementAt(10);
|
Element Operators |
Last |
'result = queryable.Last();' or result = queryable.Last(d => d.Id > 10);
|
Element Operators |
LastOrDefault |
result = queryable.LastOrDefault();
or result = queryable.LastOrDefault(d => d.Id > 10);
|
Element Operators |
Single |
result = queryable.Single(); or result = queryable.Single(d => d.Id > 10);
|
Element Operators |
SingleOrDefault |
result = queryable.SingleOrDefault(); or result = queryable.SingleOrDefault(d => d.Id > 10);
|
Quantifiers |
Any -Simple |
results = queryable.Any();
|
Quantifiers |
Any -Grouped |
results = queryable.Any(d => d.Name == "Sitecore");
|
Quantifiers |
Unsupported |
All |
Aggregate Operators |
Count -Simple |
results = queryable.Count();
|
Aggregate Operators |
Count -Conditional |
results = queryable.Count(d => d.Id < 10);
|
Aggregate Operators |
Unsupported |
Sum Min Max Average Aggregate |
Join Operators |
Unsupported |
Cross Join Group Join Cross Join with Group Join Left Outer Join |
IQueryable Extensions
Filtering |
|
results = queryable.Filter(d => d.Id > 4 && d.Id < 8);
|
Facets |
Simple Faceting |
var results = queryable.FacetOn(d => d.Name); var facets = results.GetFacets(); foreach (var category in facets.Categories) { Console.WriteLine(category .Name); foreach (var facetValue in category.Values) { Console.WriteLine("{0}: {1}", facetValue.Name, facetValue.Aggregate); } }
|
|
Pivot Faceting |
var results = queryable.FacetPivotOn(p => p.FacetOn(d => d.Name).FacetOn(d => d.Year)); var facets = results.GetFacets(); foreach (var category in facets.Categories) { Console.WriteLine(category .Name); foreach (var facetValue in category.Values) { Console.WriteLine("{0}: {1}", facetValue.Name, facetValue.Aggregate); } }
|
Boosting |
|
results = queryable.Where(d => d.Id == 7.Boost(2f)).Where(d => d.Template.Contains("o"));
|
Other |
Between |
results = queryable.Where(item => item.Price.Between(50.0f, 400.0f, Inclusion.Both)); or results = queryable.Where(item => item.Price.Between(2.0f, 12.0f, Inclusion.Both) || item.Price.Between(80.0f, 400.0f, Inclusion.Both)); or results = queryable.Where(d => d.Date.Between(new DateTime(2004, 12, 31), DateTime.Now, Inclusion.Both)); or results = queryable.Where(d => d.Id.Between(1, 4, Inclusion.Both)); or results = queryable.Where(d => d.Id.Between(1, 4, Inclusion.Lower)); or results = queryable.Where(d => d.Id.Between(1, 4, Inclusion.Upper)); or results = queryable.Where(d => d.Id.Between(1, 4, Inclusion.None));
|
Other |
string.Contains |
results = queryable.Where(d => !d.Template.Contains("Hello:));
|
Other |
string.CompareTo |
results = queryable.Where(d => !d.Name.CompareTo("Hello") == 1);
|
Other |
Equal |
results = queryable.Where(d => d.Id.Equal(4));
|
Other |
Matches |
results = queryable.Where(i => i.Template.Matches("^.*$"));
|
Other |
MatchWildcard |
results = queryable.Where(i => i.Template.Where(i => i.Template.MatchWildcard("H?li*m")));
|
Other |
Like |
results = queryable.Where(i => i.Template.Like("Citecoar"));
|
Other |
string.StartsWith |
results = queryable.Where(d => !d.Name.StartsWith("Hello"));
|
Other |
string.EndsWith |
results = queryable.Where(d => !d.Name.EndsWith("Hello"));
|
GetResults |
|
results = queryable.GetResults().Hits.Where(i => i.Document.Name.Contains("o")).Where(hit => hit.Score > 0.6);
|
GetFacets |
|
results = queryable.Where(d => d.Id > 0).FacetOn(d => d.Template, 0).GetFacets();
|
|
|
Daily Life usages
This returns the results of a query on your search index and returns it as a SearchResultItem type. |
using (var context = ContentSearchManager.GetIndex(item).CreateSearchContext()) { IQueryable<SearchResultItem> searchQuery = context.GetQueryable<SearchResultItem>().Where(item => item.Name == “Sitecore”) }
|
This converts the query to something your provider understands. For example, for Lucene it is converted to: _name:sitecore |
using (var context = ContentSearchManager.GetIndex(item).CreateSearchContext()) { IQueryable<SearchResultItem> searchQuery = context.GetQueryable<SearchResultItem>().Where(item => item[“_name”] == “Sitecore”) }
|
+(+_name:sitecore) +(title:test body:cms) or (+(+_name:sitecore) +(title:test body:cms)) |
using (var context = ContentSearchManager.GetIndex(item).CreateSearchContext()) { IQueryable<SearchResultItem> searchQuery = context.GetQueryable<SearchResultItem>().Where(item => item[“_name”] == “Sitecore”).Where(item => item.Title == “Test || item.Body.Contains(“CMS”)) } OR using (var context = ContentSearchManager.GetIndex (item).CreateSearchContext()) { IQueryable<SearchResultItem> searchQuery = context.GetQueryable<SearchResultItem> ().Where(item => item.Name == “Sitecore”).Where(item => item.Title == “Test || item.Body.Contains(“CMS”)
|
Find all the items that have “s” in their names |
var index = ContentSearchManager.GetIndex("sitecore_master_index"); using (var context = index.CreateSearchContext()) { Console.WriteLine( context.GetQueryable<SearchResultItem>().Where(resultItem=>resultItem.Name.Contains("s")) );
|
Finding the first item named Sitecore |
context.GetQueryable<SearchResultItem>().FirstOrDefault(resultItem=>resultItem.Name=="sitecore")
|
Finding the count of items in a database |
context.GetQueryable<SearchResultItem>().Count()
|
Finding all the items that use a certain template |
context.GetQueryable<SearchResultItem>().Where(resultItem=>resultItem.TemplateName=="Sample Item") |
same thing as above but this time using the template id |
context.GetQueryable<SearchResultItem>().Where(resultItem=>resultItem.TemplateId==ID.Parse("{76036F5E-CBCE-46D1 -AF0A-4143F9B557AA}"))
|
find all items that have some content in the "Page Title" field |
context.GetQueryable<SearchResultItem>().Where(resultItem=>resultItem["page_title"]!=""
|
find all items that have lorem in the body |
context.GetQueryable<SearchResultItem>().Where(resultItem=>resultItem["body"].Contains("lorem"))
|
finding an item where item.Fields["Taxonomy Items"] // is a multi list field and contains "{016424DF-865C-49DC- 9D7A-40EB8101C717}" |
context.GetQueryable<SearchResultItem>() .Where(resultItem=>resultItem["taxonomy_items"].Contains(Sitecore.ContentSearch.Utilities.IdHelper.NormalizeGuid ("{016424DF-865C-49DC-9D7A-40EB8101C717}",true)))
|
Find items based on Data Source string |
var query = LinqHelper.CreateQuery(context, UIFilterHelpers.ParseDatasourceString(txtQuery.Text.Trim())).Select(x => x.GetItem()) .AsEnumerable() .Where(x => x.Language == Sitecore.Context.Language)
|
|
|
Advanced Usages
Get Particular template's item for current website and latest version of an item only |
var searchResultItem = context.GetQueryable<SearchResultItem>() .Where(x => x.Path.Contains(sitecoreItem.Paths.FullPath)) .Where(x => x.TemplateId == Sitecore.Data.ID.Parse("YOURTEMPLATEID")) .AsEnumerable() .Where(x => x != null && x.GetItem() != null && x.Version == x.GetItem().Versions. GetLatestVersion().Version.ToString());
|
To get all details related to search index like IsClean, LastUpdated, NumberOfDocuments,NumberOfFields |
ISearchIndex isearchIndex = ContentSearchManager.GetIndex("YOURINDEXNAME");
|
The _content field aggregates all fields from the item to allow full text searching |
[IndexField(Sitecore.ContentSearch.BuiltinFields.Content)] public string ItemContent { get; set; }
|
To do your index field configuration |
<fieldMap type="Sitecore.ContentSearch.FieldMap, Sitecore.ContentSearch"> <fieldNames hint="raw:AddFieldByFieldName">
|
|
Created By
sitecorebasics.wordpress.com
Metadata
Favourited By
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets
More Cheat Sheets by kpatil