How to Add Scripting to Pipeline Stages

How to Add a New Query/Results Pipeline Stage

  • Add scripting to stages to make it easier to use libraries by importing namespaces and referencing assemblies.
  • The XQL tools library is available by default.

When you add a pipeline stage Pipeline stages offer uniformity to the end user. Various functions include mapping names and values to match local refinements., choose one of the following operations:

For either operation, use the following steps:

  1. Go to the <name of your backend such as MyBackend>> Pipeline Stages:

    1. Click "Add New Query/Results Stage" and the pipeline page appears.



  2. Pipeline Stage: Click the down arrow and select your stage in the list box that appears.

    • Query Scripting Processor:

      • Processes search queries and translates managed property names so that these names map to an existing property on a specific backend.

      • Use this stage to process the received search query requests by providing complete scripting access to the SearchQuery object.

        If you add the Property Mapper or the Refinement Mapper pipeline stage, you must add the same stage as both a Query and a Results stages.

    • Results Scripting Processor:

      • Processes search results and maps the backend refinement names and values to match the local refinements.

      • This stage post-processes the returned search results by providing complete access to the SearchResults object.

    • Suggestion Query Processing:

      • Performs suggestion query processing and allows search queries to be processed.

        Use this stage to process the query suggestion requests by providing complete access to the SearchQuery object.

    • Query Suggestions Results Processor:

      • Runs query suggestion processing.

      • This stage processes query suggestion results by providing complete access to the QuerySuggestions, ResultSuggestions, and PeopleSuggestions members of the SearchResults object.

      All results pipeline stages have access to the ExecutedQuery, which is a component of the SearchResults class.
      For further assistance, contact your BA Insight Support representative.

  3. Referenced assemblies(optional):

    1. Optional.

    2. Specify which assemblies the script needs in order to run.

    3. The format is one line per assembly and you must specify the assembly using the Fully Qualified Assembly format.

      1. For example, BAInsight.ElasticSearch.FederatorExtension, Version=1.0.0.0, Culture=neutral,PublicKeyToken=bd5ed2e4b6962cd0.

  4. Imported Namespaces (optional):

    1. Specify which namespaces to import in your script.

    2. In other words, this option lets you reference classes without specifying the full namespace path.

    3. The format is one line per namespace.

      1. For example, BAInsight.ElasticSearch.FederatorExtension.ElasticBackend.Backends.

  5. Script: Double-click in space in the field to access the large text area:



  6. Compile script: Use this option within the large text area, or as a link on the SmartHub Search Service page. If your script throws an error see the red explanatory text below the Script box.

  7. Submit script: Click after you finalize your script.

Use the C# Scripting Example When You Add Scripting to a Stage

Query Scripting Processor

Example: Block specific queries for a specific backend

Copy
if (Query.QueryText.Contains("ExpSpec"))
 Query.SkipFederation = true;

Example: Enhance troubleshooting/logging

Copy
Logger.Debug(string.Format("Received query {0} for source {1} ", Query.QueryText, Query.SourceName));

Example: Check how large an expanded query is

Copy
Logger.Debug(string.Format("Query Text + Template total size is: {0}", Query.QueryText.Length + Query.QueryTemplate.Length));

Results Scripting Processor

Example: Enhance the returned metadata

Copy
foreach(var searchResult in Results.RelevantResults) {
 if (searchResult.ContainsProperty("Project"))
  searchResult.AddOrChangeProperty(
   "Research Department",
   string.Format("http://server:1234/sites/departments/{0}.aspx",
    searchResult.GetProperty("Project")),
   typeof(System.String));
 else {
  Logger.Warn("Missing Project info for document " + searchResult.GetProperty("Path"));
 }

}

Example: How to Append the Current User to the Query Text

Copy
var profileProvider = UnityHelper.Resolve<IProfileProvider>();
IDictionary<string, string> userProfileProperties =
profileProvider.GetUserProfileProperties(null);
 if(userProfileProperties == null || userProfileProperties.Count == 0)
 {
  Logger.Warn("UserIdentityStage: No user profile loaded");
  return;
 }
 if(userProfileProperties.Keys. Contains("samaccountname"))
 {
  var username = userProfileProperties["samaccountname"];  
  Logger.Debug("UserIdentityStage: username found: " + username);
  var queryAddition = string.Format(@" userIdentity=""{0}""", username);
  Logger.Debug("query fragment to add: " + queryAddition);    
  Query.QueryText += queryAddition;
 }
 else
 {
  Logger.Warn(
    "samaccountname not found, properties available are: "
    string.Join(", ", userProfileProperties.Keys));
 }

Example: Display the author alias in the refinement results

Copy
var processedRefinementResults = newList < RefinementResult > ();

var sqlConnection = newSqlConnection("Data Source=SERVER;Initial Catalog=DATABASE;Integrated Security=True");

foreach(var refinementResult in Results.RefinementResults) {
 if (refinementResult.RefinerName.Equals("DisplayAuthor")) {
  var sqlQ = "Select TOP 1 Alias FROM EmployeeAliases";

  var conn = newSqlCommand(sqlQ, sqlConnection);
  object aliasResult = conn.ExecuteScalar();
  if (aliasResult != null) {
   var alias = Convert.ToString(aliasResult);

   processedRefinementResults.Add(
    newRefinementResult(
     refinementResult.RefinerName,
     alias,
     refinementResult.RefinementValue,
     refinementResult.RefinementCount));

   continue;
  }
 }
 processedRefinementResults.Add(refinementResult);

}
Results.RefinementResults = processedRefinementResults;

Example: Improve visibility and see the logging details

Copy
var sb = newStringBuilder();
sb.Append("<refinementresults>");
foreach(var refinementResult in Results.RefinementResults.OrderBy(r => r.RefinerName)) {
 string lastRefiner = String.Empty;
 if (!lastRefiner.Equals(refinementResult.RefinerName)) {
  if (!string.IsNullOrEmpty(lastRefiner))
   sb.Append("</refiner>");
  lastRefiner = refinementResult.RefinerName;
  sb.Append("<refiner>");
  sb.Append("<name>" + lastRefiner + "</name>");
 }
 sb.Append("<value>");
 sb.Append("<refinementvalue>" + refinementResult.RefinementValue + "</refinementvalue>");
 sb.Append("<refinementcount>" + refinementResult.RefinementCount + "</refinementcount>");
 sb.Append("<hitcount>" + refinementResult.HitCount + "</hitcount>");
 sb.Append("</value>");
}

sb.Append("</refinementresults>");
Logger.Debug(string.Format("The retrieved refiners are: {0}{1}", Environment.NewLine, sb));

Suggestion Query Processor

Expand Synonyms Example

Copy
//synonymThesaurus is considered a previously populatedDictionary<string, List<string>>if (sysnonymThesaurus.ContainsKey(Query.QueryText))
 Query.QueryText = string.Join(" OR ", sysnonymThesaurus[Query.QueryText]);

Query Suggestions Results Processor

Example: Suggest searching for experts using people search centers

Copy
Results.ResultSuggestions.Add(newFederatorPersonalResultSuggestion {
   Title = string.Format("Search for experts in {0}", Results.ExecutedQuery.QueryText),
    Url = string.Format("http://baisp2013dev05/sites/Expertise/Pages/results.aspx?k={0}"    });

Example: Check news about the executed query

Copy
Results.ResultSuggestions.Add(newFederatorPersonalResultSuggestion {
 Title = string.Format("Check news about {0}", Results.ExecutedQuery.QueryText),
  Url = string.Format("https://www.google.com/search?q={0}&&tbm=nws", Results.ExecutedQuery.QueryText)
});

Example: Generate custom query suggestions that are returned from the out-of-the-box SharePoint search center

Copy
var querySuggestions = new [] {
 "this",
 "that",
 "other"};
foreach(var suggestion in querySuggestions) {
 Results.QuerySuggestions.Add(new FederatorQuerySuggestion {
 Query = suggestion
 });
}

Example: Generate people suggestions

Copy
Results.PeopleSuggestions.AddRange(new[] {"John Doe", "Somebody Else"});