The CrownPeak Search G2 platform is a highly-scalable, high-performance, enterprise-grade indexing and query platform, based upon the Apache Lucene Software Library & Apache SolrCloud.
It does Realtime Indexing, which provides a direct connection between the CMS and the Search G2 platform. This results in assets that can be built within the CMS for delivery directly into the Search G2 platform upon publication.
Here we will look at a very basic implementation and see how to connect all the things together.
Prerequisites:
Understanding of C#, Javascript and Crownpeak CMS API
Now that we have some initial background done, lets start with the steps to implement SearchG2 -
1) The first step is to create a Crownpeak Search Collection. The Collection will contain website index definition and corresponding crawling rules. Please contact Crownpeak support to set up the same.
Once you have the collection name setup, place it inside the web.config file under the appSettings node:
(Each type of workflow has unique collection name, So If you have multiple environment setup then you can condition it accordingly)
< %
if (context.PublishingStatus.Name.ToLower().Equals("live")) { % >
<%
}
else if (context.PublishingStatus.Name.ToLower().Equals("dev")) { % >
<%
} % >
2) The next thing you need to do in Crownpeak is, to add insert, update and delete files to the template, from which you want to fetch data in the search result. Create methods inside a CMS Library file (A user defined c# class file) to create, update and delete Search fields and call them from inside the insert, update and delete template files, with specific operation type, for example:
// This function is called inside template searchG2 templates.
// It is defined inside c# class file under Library, in our case we have Library file named as SearchLibrary.cs
public static void UpdateSearchG2(SearchG2JsonParams.OperationType type, Asset asset, SearchG2Context context) {
var doc = new SearchG2JsonParams {
Id = asset.BranchId.ToString(),
Operation = type,
Overwrite = true
};
if (type != SearchG2JsonParams.OperationType.Delete) {
asset.GetContent();
doc.Add("custom_i_asset_id", AssetPropertyNames.Id);
doc.Add("custom_i_template_id", AssetPropertyNames.TemplateId);
doc.Add("custom_s_template_label", AssetPropertyNames.TemplateLabel);
doc.AddFixed("custom_s_url", GetLink(asset, context, true));
doc.AddFixed("custom_s_local_url", GetLink(asset, context));
Out.DebugWriteLine("Event Detail Template 3");
Util.Log(asset, "Event Detail Template 0.1");
switch (asset.TemplateLabel) {
case "Content Detail Template":
UpdateNewsArticle(doc, asset, context); // This method will contain template specific fields
break;
case "Event Detail Template":
UpdateEvents(doc, asset, context);
break;
// TODO: Other supported templates will go here
}
doc.AddFixedIfMissing("custom_s_content_type", "text/html");
doc.AddIfMissing("custom_dt_created", AssetPropertyNames.CreateDate);
doc.AddIfMissing("custom_dt_modified", AssetPropertyNames.ModifiedDate);
doc.AddFixedIfMissing("custom_dt_published", DateTime.UtcNow.ToString("o"));
var title = asset["html_title"];
if (string.IsNullOrWhiteSpace(title)) title = asset["meta_title"];
if (string.IsNullOrWhiteSpace(title)) title = asset.Label;
doc.AddFixedIfMissing("title", title);
doc.AddFixedIfMissing("custom_s_title", title);
doc.AddFixedIfMissing("content", Util.StripHtml(asset.Show()));
}
context.JsonParams.Add(doc);
Util.Log(asset, "This is Search G2 {0} operation: {1}", type, context.GetJson());
}
//Below line should be placed inside insert template i.e. inside search_g2_insert.aspx
SearchLibrary.UpdateSearchG2(SearchG2JsonParams.OperationType.Create, asset, context);
//For search_g2_update.aspx and search_g2_delete.aspx templates option Update and Delete should be passed for SearchG2JsonParams.OperationType parameter respectively
3) In order to implement your own search in asp.net C#, first you should reference below dlls in your application.
i. CrownPeak.SearchG2.dll
ii. SolrNet.dll
Add below additional namespaces to your asp.net page and class files which holds any operations of search index:
CrownPeak.SearchG2.Query
CrownPeak.SearchG2.Common
CrownPeak.SearchG2.Attribute
4) A new class should be created to hold the search result. This must inherit from CrownPeak.SearchG2.Result.ResultBase, and should implement the properties that you wish to retrieve from the search index. The properties do not have to match exactly, and you can decorate your properties with attributes that describe exactly which field to use to populate it. For example:
public class MyResult: CrownPeak.SearchG2.Result.ResultBase { [Field("url")]
public string Url {
get;
set;
} [Field("title")]
public string Title {
get;
set;
} [Field("content")]
public string Content {
get;
set;
}
/// Match everything else and put it into a dictionary
[Field("*")]
public IDictionary OtherFields {
get;
set;
}
}
5) Creating and Configuring Search Object :
Next step is to create Search object and configure the search properties to customize the result.
var search = new CrownPeak.SearchG2.Search(new Settings(System.Configuration.ConfigurationManager.AppSettings["searchcollection"].ToString()));
FilterQueryCollection filterQueries = new FilterQueryCollection();
filterQueries.Add(new FilterQuery("custom_s_type", "EventsPage"));
QueryOptions options = new QueryOptions() {
Highlighting = true,
// highlights the search term in result
Start = 0,
// start index of the row in result set
Rows = 10,
// number of rows you want to retrieve
SpellCheck = false,
// checks spelling if it is set to true
FacetFields = new[] {
"content",
"url",
"title"
},
FilterQueries = filterQueries,
// defines filter parameters
OrderBy = new[] {
new SortOrder("custom_dt_startdate", CrownPeak.SearchG2.Query.SortDirection.Desc)
}
}; // sorts data
// save data in a variable returned by search query
var resultSet = search.Execute(text, options);
// perform some operation on resultset based on requirement
Once you get the result set of the search, you can perform any operation as per your requirement.
References:
https://developer.crownpeak.com/Documentation/BestPractices/Search-G2/Search-G2-Introduction.html
https://developer.crownpeak.com/Documentation/BestPractices/Search-G2/Search-G2-Realtime-Indexing.html
https://developer.crownpeak.com/Documentation/BestPractices/Search-G2/Search-G2-CMS-Administration-Interface.html