Show Menu
Cheatography

Sitecore Search Developers Cheat Sheet by

Sitecore search related day to day snippets

Basics

Terms & Phrases
"­tes­t" or "­hello dolly"
c.Wher­e("t­est­") or c.Wher­e("hello dolly") or c.Wher­e("t­est­" || "­hello dolly")
Fields
title:­"The Right way" and text:"g­o"
c.Title == "The Right way" or c.Text == "­go" or c.Equa­ls(­"­go")
WildCard
amber
c.Cont­act­Nam­e.C­ontains ("am­ber­")
Prefix
amber amber
c.Cont­act­Nam­e.S­tar­tsW­ith­("am­ber­") or c.Cont­act­Nam­e.E­nds­Wit­h("a­mbe­r")
Fuzzy
roam or roam0.8
c.Cont­act­Nam­e.L­ike­("ro­am") or c.Cont­act­Nam­e.L­ike­("ro­am", 0.8)
Proximity
"­jakarta apache­"~10
c.Cont­act­Nam­e.L­ike­("ja­karta apache­", 10)
Inclusive Range
mod_da­te:­[20­020101 TO 20030101]
c.Modi­fie­dDa­te.B­et­wee­n("2­002­010­1", "­200­301­01", Inclus­ion.Both)
Exclusive Range
title:­{Aida TO Carmen}
c.Titl­e.B­etw­een­("Ai­da", "­Car­men­", Inclus­ion.None)
Boosting
jakarta^4 apache
c.Titl­e.E­qua­ls(­"­jak­art­a".B­oos­t(4)) || c.Titl­e.E­qua­ls(­“ap­ache”)
Boolean Or
"­jakarta apache­" OR jakarta
where c.Titl­e.E­qua­ls(­"­jakarta apache­") || c.Equa­ls(­"­jak­art­a")
Boolean And
"­jakarta apache­" AND "­Apache Lucene­"
where c.Equa­ls(­"­jakarta apache­") && c.Equa­ls(­"­Apache Lucene­")
Boolean Not
"­jakarta apache­" NOT "­Apache Lucene­"
where c.Equa­ls(­"­jakarta apache­") && !c.Equ­als­("Apache Lucene­")
Grouping
(jakarta OR apache) AND website
where (c.Title == "­jak­art­a" || c.Title == "­apa­che­") && (c.Title == "­web­sit­e")

Supported IQueryable methods

Restri­ction Operators
Where
var results = from d in queryable where d.Name == "­Sit­eco­re" select d;
or
var results = querya­ble.Wh­ere(d => d.Name == "­Sit­eco­re");
Projection Operators
Select
var results = from d in queryable select d.Name;
or
var results = querya­ble.Se­lect(d => d.Name);
Anonymous types
 
results = querya­ble.Se­lect(d => new { d.Name, d.Id });
Unsupp­orted
SelectMany
Partit­ioning Operators
Take
results =
 querya­ble.Ta­ke(10);
Partit­ioning Operators
Skip
results = querya­ble.Sk­ip(10);
Partit­ioning Operators
Page
results = querya­ble.Pa­ge(2, 100);
Ordering Operators
OrderBy
results = querya­ble.Or­derBy(d => d.Name);
Ordering Operators
OrderBy Descending
results = querya­ble.Or­der­ByD­esc­end­ing(d => d.Name);
Ordering Operators
ThenBy
results = querya­ble.Or­derBy(d => d.Name­).T­henBy(d => d.Id);
Ordering Operators
ThenBy Descending
results = querya­ble.Or­derBy(d => d.Name­).T­hen­ByD­esc­end­ing(d => d.Id);
Unsupp­orted
Reverse
Grouping Operators
Unsupp­orted
GroupBy -Simple 1 GroupBy -Simple 2 GroupBy -Simple 3 GroupBy -Nested GroupBy -Comparer GroupBy -Comparer, Mapped
Set Operators
Unsupp­orted
Distinct Union Intersect Except
Element Operators
First -Simple
results = querya­ble.Fi­rst();
Element Operators
First -Condition
results = querya­ble.Fi­rst(d => d.Name == "­Sit­eco­re");
Element Operators
FirstO­rDe­fault -Simple
results = querya­ble.Fi­rst­OrD­efa­ult();
Element Operators
FirstO­rDe­fault -Condition
results = querya­ble.Fi­rst­OrD­efa­ult(d => d.Name == "­Sit­eco­re");
Element Operators
ElementAt
results = querya­ble.El­eme­ntA­t(10);
Element Operators
Last
'result = querya­ble.La­st();' or
result = querya­ble.Last(d => d.Id > 10);
Element Operators
LastOr­Default
result = querya­ble.La­stO­rDe­fau­lt();
or
result = querya­ble.La­stO­rDe­fault(d => d.Id > 10);
Element Operators
Single
result = querya­ble.Si­ngle();
or
result = querya­ble.Si­ngle(d => d.Id > 10);
Element Operators
Single­OrD­efault
result = querya­ble.Si­ngl­eOr­Def­ault();
or
result = querya­ble.Si­ngl­eOr­Def­ault(d => d.Id > 10);
Quanti­fiers
Any -Simple
results = querya­ble.Any();
Quanti­fiers
Any -Grouped
results = querya­ble.Any(d => d.Name == "­Sit­eco­re");
Quanti­fiers
Unsupp­orted
All
Aggregate Operators
Count -Simple
results = querya­ble.Co­unt();
Aggregate Operators
Count -Condi­tional
results = querya­ble.Co­unt(d => d.Id < 10);
Aggregate Operators
Unsupp­orted
Sum Min Max Average Aggregate
Join Operators
Unsupp­orted
Cross Join Group Join Cross Join with Group Join Left Outer Join

IQueryable Extensions

Filtering
 
results = querya­ble.Fi­lter(d => d.Id > 4 && d.Id < 8);
Facets
Simple Faceting
var results = querya­ble.Fa­cetOn(d => d.Name); var facets = result­s.G­etF­ace­ts(); foreach (var category in facets.Ca­teg­ories) { Consol­e.W­rit­eLi­ne(­cat­egory .Name); foreach (var facetValue in catego­ry.V­alues) { Consol­e.W­rit­eLi­ne(­"{0}: {1}", facetV­alu­e.Name, facetV­alu­e.A­ggr­egate); } }
 
Pivot Faceting
var results = querya­ble.Fa­cet­Piv­otOn(p => p.Face­tOn(d => d.Name­).F­ace­tOn(d => d.Year)); var facets = result­s.G­etF­ace­ts(); foreach (var category in facets.Ca­teg­ories) { Consol­e.W­rit­eLi­ne(­cat­egory .Name); foreach (var facetValue in catego­ry.V­alues) { Consol­e.W­rit­eLi­ne(­"{0}: {1}", facetV­alu­e.Name, facetV­alu­e.A­ggr­egate); } }
Boosting
 
results = querya­ble.Wh­ere(d => d.Id == 7.Boos­t(2­f)).Wh­ere(d => d.Temp­lat­e.C­ont­ain­s("o­"));
Other
Between
results = querya­ble.Wh­ere­(item => item.P­ric­e.B­etw­een­(50.0f, 400.0f, Inclus­ion.Bo­th));
or
results = querya­ble.Wh­ere­(item => item.P­ric­e.B­etw­een­(2.0f, 12.0f, Inclus­ion.Both) || item.P­ric­e.B­etw­een­(80.0f, 400.0f, Inclus­ion.Bo­th));
or
results = querya­ble.Wh­ere(d => d.Date.Be­twe­en(new DateTi­me(­2004, 12, 31), DateTi­me.Now, Inclus­ion.Bo­th));
or
results = querya­ble.Wh­ere(d => d.Id.B­etw­een(1, 4, Inclus­ion.Bo­th));
or
results = querya­ble.Wh­ere(d => d.Id.B­etw­een(1, 4, Inclus­ion.Lo­wer));
or
results = querya­ble.Wh­ere(d => d.Id.B­etw­een(1, 4, Inclus­ion.Up­per));
or
results = querya­ble.Wh­ere(d => d.Id.B­etw­een(1, 4, Inclus­ion.No­ne));
Other
string.Co­ntains
results = querya­ble.Wh­ere(d => !d.Tem­pla­te.C­on­tai­ns(­"­Hel­lo:));
Other
string.Co­mpareTo
results = querya­ble.Wh­ere(d => !d.Nam­e.C­omp­are­To(­"­Hel­lo") == 1);
Other
Equal
results = querya­ble.Wh­ere(d => d.Id.E­qua­l(4));
Other
Matches
results = querya­ble.Wh­ere(i => i.Temp­lat­e.M­atc­hes­("^.*­$"));
Other
MatchW­ildcard
results = querya­ble.Wh­ere(i => i.Temp­lat­e.W­here(i => i.Temp­lat­e.M­atc­hWi­ldc­ard­("H?­li*­m")));
Other
Like
results = querya­ble.Wh­ere(i => i.Temp­lat­e.L­ike­("Ci­tec­oar­"));
Other
string.St­art­sWith
results = querya­ble.Wh­ere(d => !d.Nam­e.S­tar­tsW­ith­("He­llo­"));
Other
string.En­dsWith
results = querya­ble.Wh­ere(d => !d.Nam­e.E­nds­Wit­h("H­ell­o"));
GetResults
 
results = querya­ble.Ge­tRe­sul­ts(­).H­its.Wh­ere(i => i.Docu­men­t.N­ame.Co­nta­ins­("o")­).W­her­e(hit => hit.Score > 0.6);
GetFacets
 
results = querya­ble.Wh­ere(d => d.Id > 0).Fac­etOn(d => d.Temp­late, 0).Get­Fac­ets();
 

Daily Life usages

This returns the results of a query on your search index and returns it as a Search­Res­ultItem type.
using (var context = Conten­tSe­arc­hMa­nag­er.G­et­Ind­ex(­ite­m).C­re­ate­Sea­rch­Con­text()) { IQuery­abl­e<S­ear­chR­esu­ltI­tem>   search­Query = contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(item => item.Name == “Sitec­ore”) }
This converts the query to something your provider unders­tands. For example, for Lucene it is converted to: _name:­sit­ecore
using (var context = Conten­tSe­arc­hMa­nag­er.G­et­Ind­ex(­ite­m).C­re­ate­Sea­rch­Con­text()) { IQuery­abl­e<S­ear­chR­esu­ltI­tem>   search­Query = contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(item => item[“­_name”] == “Sitec­ore”) }
+(+_na­me:­sit­ecore) +(titl­e:test body:cms) or (+(+_n­ame­:si­tecore) +(titl­e:test body:cms))
using (var context = Conten­tSe­arc­hMa­nag­er.G­et­Ind­ex(­ite­m).C­re­ate­Sea­rch­Con­text()) { IQuery­abl­e<S­ear­chR­esu­ltI­tem>   search­Query = contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(item => item[“­_name”] == “Sitec­ore­”).W­he­re(item =>   item.Title == “Test || item.B­ody.Co­nta­ins­(“C­MS”)) } OR using (var context = Conten­tSe­arc­hMa­nag­er.G­et­Index  (item).Cr­eat­eSe­arc­hCo­nte­xt()) { IQuery­abl­e<S­ear­chR­esu­ltI­tem> search­Query = contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>  ().Whe­re(item => item.Name == “Sitec­ore­”).W­he­re(item => item.Title == “Test || item.B­ody.Co­nta­ins­(“CMS”)
Find all the items that have “s” in their names
var index = Conten­tSe­arc­hMa­nag­er.G­et­Ind­ex(­"­sit­eco­re_­mas­ter­_in­dex­"); using (var context = index.C­re­ate­Sea­rch­Con­text()) {     Consol­e.W­rit­eLine( contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(r­esu­ltI­tem­=>r­esu­ltI­tem.Na­me.C­on­tai­ns(­"­s"))   );
Finding the first item named Sitecore
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).F­irs­tOr­Def­aul­t(r­esu­ltI­tem­=>r­esu­ltI­tem.Na­me=­="si­tec­ore­")
Finding the count of items in a database
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).C­ount()
Finding all the items that use a certain template
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(r­esu­ltI­tem­=>r­esu­ltI­tem.Te­mpl­ate­Nam­e==­"­Sample Item")
same thing as above but this time using the template id
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(r­esu­ltI­tem­=>r­esu­ltI­tem.Te­mpl­ate­Id=­=ID.Pa­rse­("{7­603­6F5­E-C­BCE­-46D1  -AF0A-­414­3F9­B55­7AA­}"))
find all items that have some content in the "Page Title" field
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(r­esu­ltI­tem­=>r­esu­ltI­tem­["pa­ge_­tit­le"]­!=""
find all items that have lorem in the body
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>(­).W­her­e(r­esu­ltI­tem­=>r­esu­ltI­tem­["bo­dy"].Co­nta­ins­("lo­rem­"))
finding an item where item.F­iel­ds[­"­Tax­onomy Items"] // is a multi list field and contains "­{01­642­4DF­-86­5C-­49DC- 9D7A-4­0EB­810­1C7­17}­"
contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>() .Where­(re­sul­tIt­em=­>re­sul­tIt­em[­"­tax­ono­my_­ite­ms"].Co­nta­ins­(Si­tec­ore.Co­nte­ntS­ear­ch.U­ti­lit­ies.Id­Hel­per.No­rma­liz­eGuid  ("{0­164­24D­F-8­65C­-49­DC-­9D7­A-4­0EB­810­1C7­17}­"­,tr­ue)))
Find items based on Data Source string
 var query = LinqHe­lpe­r.C­rea­teQ­uer­y(c­ontext,                     UIFilt­erH­elp­ers.Pa­rse­Dat­aso­urc­eSt­rin­g(t­xtQ­uer­y.T­ext.Tr­im(­))).Se­lect(x => x.GetI­tem())                     .AsEnu­mer­able()                     .Where(x => x.Language == Siteco­re.C­on­tex­t.L­ang­uage)
 

Advanced Usages

Get Particular template's item for current website and latest version of an item only
var search­Res­ultItem = contex­t.G­etQ­uer­yab­le<­Sea­rch­Res­ult­Ite­m>()                                 .Where(x => x.Path.Co­nta­ins­(si­tec­ore­Ite­m.P­ath­s.F­ull­Path))                                 .Where(x => x.Temp­lateId == Siteco­re.D­at­a.I­D.P­ars­e("Y­OUR­TEM­PLA­TEI­D"))                                 .AsEnu­mer­able()                                 .Where(x => x != null && x.GetI­tem() != null &&                                      x.Version == x.GetI­tem­().V­er­sions.                                     GetLat­est­Ver­sio­n().Ve­rsi­on.T­oS­tri­ng());
To get all details related to search index like IsClean, LastUp­dated, Number­OfD­ocu­men­ts,­Num­ber­OfF­ields
ISearc­hIndex isearc­hIndex =                                     Conten­tSe­arc­hMa­nag­er.G­et­Ind­ex(­"­YOU­RIN­DEX­NAM­E");
The _content field aggregates all fields from the item to allow full text searching
[Index­Fie­ld(­Sit­eco­re.C­on­ten­tSe­arc­h.B­uil­tin­Fie­lds.Co­ntent)]     public string ItemCo­ntent     {         get;         set;     }
To do your index field config­uration
<fi­eldMap type="S­ite­cor­e.C­ont­ent­Sea­rch.Fi­eldMap, Siteco­re.C­on­ten­tSe­arc­h">   <fi­eld­Names hint="r­aw:­Add­Fie­ldB­yFi­eld­Nam­e">
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          More Cheat Sheets by kpatil

          SXA Renderings Cheat Sheet