How to automatically email search results

Fusion uses template expressions to construct email messages, but the built-in TemplateStrings library does not include expressions for inserting search results into a message.  

This article describes a workaround for configuring email messages that include search results.  It goes like this:

  1. Use the SubQuery query pipeline stage to put the response of a Solr query into the pipeline context.
  2. Use the Javascript query pipeline stage to store the textual representation of the query response.
  3. Use the Send Email query pipeline stage to insert the stored objects into an email message.

The detailed instructions below explain how to do this.

How to configure search results in email

  1. Configure the SubQuery stage so the "Key" is "subqueryResults".
  2. Configure the rest of the parameters to match the docs as needed to get the results to email. 

    The SubQuery stage results (Solr response) will be put in the pipeline context as a SearchResponse object, using the key configured in the stage.

    The SearchResponse object consists of Response, ResponseHeader, FacetCountsCollection and Stats objects, which can be referenced by the names "response", "responseHeader", "facetCounts" and "stats" respectively.

    The Response object has "numFound", "start", "maxScore" and "docs" inside, so those can be referenced in the JavaScript stage. (The "docs" is a list of Solr documents found: List<Map<String, Object>>) so in theory it is possible to drill down to any single document and its fields in the response, if needed.)

  3. Configure the JavaScript stage to use a script like this one:

    if (ctx.hasProperty("subqueryResults")) {
    subResponse = ctx.get("subqueryResults");
    ctx.set("subResponseNumFound", subResponse.response.numFound);
    subResponseString = subResponse.response.toString();
    ctx.set("subResponse", subResponseString);
    } else {
    ctx.set("subResponseNumFound", 0);
    ctx.set("subResponse", "No response from Solr stored in Pipeline Context");

    The script above stores the textual representation of the query response (including the list of docs) in the Pipeline Context.

  4. Configure the "Send Email (via SMTP)" stage to use the stored in Context data.

    For example, you can use \<ctx.subResponseString\> in the Message Body template and "Found \<ctx.subResponseNumFound\> documents" in the Message Subject template.

  5. Make sure that the order of stages in the query pipeline is SubQuery->JavaScript->Send Email. You can also add a Log Message stage before the SendEmail stage to log the data stored in context and make sure it is correct.


Have more questions? Submit a request


Please sign in to leave a comment.
Powered by Zendesk