Loading...
Area: Episerver Search & Navigation
Applies to versions: 12 and higher
Other versions:

Search statistics

Recommendations [hide]

This topic describes the autocomplete, spelling suggestion, and related queries functionality provided by the search statistics component of the Episerver Search & Navigation (formerly Episerver Find) API. 

How it works

Search statistics provide an HTTP API for tracking searches and collecting statistical data about them. The aggregated statistics expose functionality you can use to enhance the search: autocomplete, spelling suggestions, and related queries.

Although you can interact with the search statistics API using any programming language that makes HTTP requests, API usage is typically done via JavaScript.

Tracking

To gather search statistics for later analysis, use the Track() method in the search query:

SearchClient.Instance.UnifiedSearchFor(q)
  .Filter(f => f.MatchType(typeof(FashionProduct)))
  .Track()
  .GetResult();

See Tracking for more information.

Autocomplete

After adding some autocomplete text in the Search & Navigation user interface under Manage > Optimization > Autocomplete, the autocomplete feature can be enabled on the search textbox in the front-end. To add autocomplete, add a jQuery script to the page, see example below.

Note: Security restrictions may prevent you from using JSONP with Search & Navigation’s autocomplete.

<script language="javascript">
$(function () 
  {
    $("#txtSeach").autocomplete(
      {
        source: function (request, response)
          {
            $.ajax(
              {
                url: "@Model.PublicProxyPath" 
                   + "/_autocomplete?prefix="
                   + encodeURIComponent(request.term)
                   + "&size=5"
                   + "&tags="
                   + encodeURIComponent("@Model.Tags"),
                success: function (data)
                  {
                    response($.map(data.hits, function (item)
                      {
                        return
                          {
                            label: item.query,
                            value: item.query
                          };
                      }));
                  }
              });
          },
        minLength: 2
      };
  });
</script>

Then, add txtSearch to the search form:

<input id="txtSeach" type="text" tabindex="1" name="q"/>

Note: The autocomplete functionality will only work from the beginning of a phrase, the search term must begin with the first word in the autocomplete phrase. You can get existing autocomplete items from the _autocomplete/list endpoint: /_autocomplete/list?q=(%40%20%7C"")YOURWORD.*

Spellchecking

Based on what other users searched for, search statistics can provide spellchecks, popular queries similar to the one passed to the spellchecker. A jQuery example is below.

<script type="text/javascript">
  $.get('/find_v2/_spellcheck?query=camonix?size=1', function (data)
    {
      $.each(data.hits, function(index, value)
        {
          $('#spellcheck').append("<p>"
                                  +"Spellcheck for 'camonix': <a href=/Search/?q=" 
                                  + value.suggestion 
                                  + ">" 
                                  + value.suggestion 
                                  + "</a></p>");
        })
    });
</script>
<div id="spellcheck"></div>

The spellchecker accepts these parameters:
  • query (mandatory): the query to return spellchecks for.
  • size (optional): the number of spellchecks to return.

Related queries

Sometimes, it is valuable to discover search relationships, for example people who search for a also search for b. In search statistics this is called related queries. You can request them with the following jQuery.

<script type="text/javascript">
  $.get('/find_v2/_didyoumean?query=chamoni&size=1', function (data)
    {
      $.each(data.hits, function(index, value)
        {
          $('#didyoumean').append("<p>"
                                + "Didyoumean for 'chamonix': <a href=/Search/?q=" 
                                + value.suggestion 
                                + ">" 
                                + value.suggestion 
                                + "</a></p>");
        })
    });
</script>
<div id="didyoumean"></div>

Related queries accept these parameters:
  • query (mandatory): the query for which to return related queries.
  • size (optional): the number of related queries to return.

Related topics

Do you find this information helpful? Please log in to provide feedback.

Last updated: Jul 29, 2020

Recommendations [hide]