Displaying popular searches
Managed by | Updated .
The Classic UI shipped with a plugin that allowed the top N queries to be displayed as a widget within the search results page. This functionality was removed when the Modern UI was release.
This article shows how this functionality can be implemented in the Modern UI allowing the top queries to be displayed within the search results display.
Process - Funnelback v14+
We can make calls to the Data API outside the collection update workflow to retrieve top queries outside a collection update workflow, using a request like:
https://SEARCH-ANALYTICS-SERVER:8443/data-api/v1.0/queries/topQueries?collection=COLLECTION&format=json
A 'PopularSearches' Freemarker macro can then be used to:
- Request data for popular searches from API (and cache if necessary)
- Render data from popular searches
<#macro PopularSearches collection format="json" startDate endDate username password cache=3600 numQueries=5>
<#-- Retrieve JSON object representing popular queries -->
<script>
var popularQueries = jQuery.parseJSON(
<@fb.IncludeUrl url="https://localhost:8443/data-api/v1.0/queries/topQueries?collection=COLLECTION&format=json" username="api-username" password="api-password" />
);
// Transform object into markup. Default to ordered list, as API returns queries ordered by frequency
var popularQueriesHTML = "<ol>";
for (var i=0; i < ${numQueries}; i++) {
popularQueriesHTML += "<li>" + popularQueries.content[i] + "</li">;
}
popularQueriesHTML += "</ol>";
document.write("popularQueriesHTML");
</script>
</#macro>
Pre-v14
As part of the collection update process, the Admin API can be accessed to save a JSON view of popular queries into the collections web resources folder using a command similar to:
post_update_command=wget "https://localhost:8443//search/admin/analytics/Dashboard?r=QUERIES&collection=COLLECTION&time=at&timeframe=day&f=JSON&export=true&n=200" -username API-USERNAME -password API-PASSWORD -O "$SEARCH_HOME/conf/_default/web/popularSearches.json"
A Freemarker macro is then used to request this JSON file at template render time, with the data only changing on each collection update:
<#macro PopularSearches collection format="json" startDate endDate username password cache=3600 numQueries=5>
<#-- Retrieve JSON object representing popular queries -->
<script>
var popularQueries = jQuery.parseJSON(
<@fb.IncludeUrl url="http://localhost/s/resources/COLLECTION/popularSearches.json" cache=${cache} />
);
// Transform object into markup. Default to ordered list, as API returns queries ordered by frequency
var popularQueriesHTML = "<ol>";
for (var i=0; i < ${numQueries}; i++) {
popularQueriesHTML += "<li>" + popularQueries.reportData[i] + "</li">;
}
popularQueriesHTML += "</ol>";
document.write("popularQueriesHTML");
</script>
</#macro>