Limit Document Search Result

Hi team,

Is it possible to limit the result returned via document search? Currently the entire body of a help article is returned, however my client only wants to show a preview of the article so that there is more room to return other results. I tried restricting the height of the .HitchhikerDocumentSearchStandard class but had no luck.

Hey Jason,

Yes, it is definitely possible to limit the length of Featured Snippets. The recommended way of doing this will depend on the type of Featured Snippet that is generated. I’ve outlined each method below depending on the type of snippet.

Plain Text Featured Snippets
For any snippet that does not contain rich text, it is recommended to handle each of these snippets individually using Experience Training for Featured Snippets. This provides the most control over exactly how long you want each snippet to be.

To take advantage of this powerful feature, navigate to your Answers backend configuration. In the left sidebar, under the “Training” sub-header, click on “Featured Snippets”. For each snippet, you’ll notice a wand icon on the far right:
Screen Shot 2022-01-11 at 11.46.46 AM

When you click this wand icon, a modal will appear that allows you to edit the Featured Snippet. If the snippet generated by the algorithm is too long for your liking, you can simply highlight a smaller portion of the identified snippet to use going forward.

Note: As mentioned above, this feature is only available for plain text snippets. For all rich text snippets, you’ll notice the wand icon is not clickable. For rich text, refer to the below instructions.

Rich Text Featured Snippets
It is fairly common for rich text snippets to be longer than plain text snippets. Here is one example:

Due to this, you may want to ensure all rich text snippets are truncated after a certain character limit. To do this, follow these steps:

  1. Use the “Add Direct Answer Card” Jambo Command to create a new card for Featured Snippets. Use “directanswercards/documentsearch-standard” as the template folder.
    Screen Shot 2022-01-11 at 11.54.15 AM

  2. In the component.js file of your newly created card, there is a dataForRender function. Within that function, there is one particular piece of code we will be editing, which currently looks like this:

if (answer.fieldType === "rich_text" && snippet) {
      snippetValue = ANSWERS.formatRichText(snippet.value, 'snippet', linkTarget);
} else if (snippet) {
      snippetValue = Formatter.highlightField(snippet.value, snippet.matchedSubstrings);
}

This checks to see if the content is rich text or plain text and formats it accordingly. Since we only want to truncate rich text snippets, we will only change the first portion of the if statement. The block of code should be changed to look something like this:

if (answer.fieldType === "rich_text" && snippet) {
      snippetValue = Formatter.truncate(ANSWERS.formatRichText(snippet.value, 'snippet', linkTarget), 450);
} else if (snippet) {
      snippetValue = Formatter.highlightField(snippet.value, snippet.matchedSubstrings);
}

You’ll see that I wrapped the existing content within Formatter.truncate(). This will truncate the content if it exceeds the character limit you provide, which in this case is 450.

Now you just have to ensure your index.json file is referencing this new card for Featured Snippet Direct Answers instead of the default documentsearch-standard card.

Once you do this, you’ll see the snippet has been significantly truncated:

Hope this proves helpful!
DJ

1 Like