Search

This section covers the details of how to implement search functionality on your site using Infinity.

To begin with, you need to have your API access set up.

If you want to see the documentation for the underlying API calls for search, you find them in our API reference.

Infinity Search Results

The search results from Infinity are returned in two lists; Results and RelatedResults. Products in the Results list contain the actual direct hits to the user query. Products in the RelatedResults list are products that are contextually similar to the user intent. The RelatedResults list will pick up on synonym products that are not direct hits put related to the users area of interest.

The two lists Results and RelatedResults need to be presented to the user and the best way will vary from implementation to implementation, consult your Customer Success Manager for advice if you are unsure.

Search Parameter Introduction

The search term is simply called the query.

query : the actual query as entered by the user

Paging is implemented using parameters skip and take. Imagine these as operating on a hypothetical search result of a certain length. The take parameter specifies the length of the response returned by the function-call and the skip parameter specifies from where the response starts reading from the result. Both take and skip parameters are applied to Results and RelatedResults independently.

Example

skip  : the number of items in the total result to skip
take  : the maximum length of the returned response

skip = 20
take = 10
query = "user provided query"

'''This call will return results 21-30'''
results = search(query, skip, take)

Search and Render Results

The number of returned results and related results will be between 0 and what you specified for take. You check it by reading the length of the returned results array.

// The search field
//initialize "Search" request and set search query
SearchRequest request = new SearchRequest(query);

//specify number of response items
request.ResultsOptions.Skip = 0;
request.ResultsOptions.Take = 10;
request.RelatedResultsOptions.Skip = 0;
request.RelatedResultsOptions.Take = 9;

//fetch response from engine
SearchResponse response = _InfinityClient.Search(request);

// Check the search results
//if the result does not make sense, show error message
//(note that there may still be results!)
if (!response.MakesSense)
    Debug.WriteLine("We did not understand your query.");

//render spelling suggestions
if (response.SpellingSuggestions.Count > 0)
{
    var queries = response.SpellingSuggestions.Items.Select(o => o.Query);
    var suggestions = string.Join(", ", queries);

    Debug.WriteLine("Did you mean: " + suggestions + "?");
}

//render direct results
var results = response.Results.Items;
if (!results.Any())
    Debug.WriteLine("There were no items matching your search.");

foreach (var resultItem in results)
{
    var productId = resultItem.Id;
    var productTitle = resultItem.GetAttributeValueOrDefault<string>("Title");
    Debug.WriteLine(productId + " " + productTitle); //render a product on the search results page
}

//render related results
var relatedResults = response.RelatedResults.Items;
if (relatedResults.Any())
    Debug.WriteLine("Maybe you also want these?");

foreach (var resultItem in relatedResults)
{
    var productId = resultItem.Id;
    var productTitle = resultItem.GetAttributeValueOrDefault<string>("Title");
    Debug.WriteLine(productId + " " + productTitle); //render a product on the search results page
}

C# source code on Github: SearchController.cs

Check Engine Response

The engine returns a MakesSense value which indicates if the engine could find a reasonable match for the query in the product catalog. A False response means that the engine is not sure what the user meant and the result will be a guess. Best practice in this case is to inform the user somehow. The result may also contain spelling suggestions in which case it is recommended to show said suggestions to the user.

// Check the search results
//if the result does not make sense, show error message
//(note that there may still be results!)
if (!response.MakesSense)
    Debug.WriteLine("We did not understand your query.");

//render spelling suggestions
if (response.SpellingSuggestions.Count > 0)
{
    var queries = response.SpellingSuggestions.Items.Select(o => o.Query);
    var suggestions = string.Join(", ", queries);

    Debug.WriteLine("Did you mean: " + suggestions + "?");
}

C# source code on Github: SearchController.cs

Last updated