Efficiency of getting the number of search results via AJAX

For a Discourse I am managing, I want to highlight topics that need community attention. Currently those are retrieved via a somewhat complex search query (single user, not solved, older than two days). I’ve successfully added a link redirecting to the relevant search in the navigation bar.

Now I would like the item to display the number of matching topics (as the “New” and “Unread” items do). I can get the search results via:

        const ajax = require('discourse/lib/ajax').ajax;
         ajax("/search.json" + search_query).then (function(result){

Nevertheless, this seems wasteful and probably something I don’t want to run for every page load (we have around 15K pageviews a day), so my question is:

  • Should I expect calling this query from the </head> theme part to slow down page loads? Or would the query happen transparently after (most of) the page loads?
  • If efficiency would be an issue, can I query just the number of search results, i.e. without the server also sending me the details of the matching topics? Or even just get a yes/no answer whether there are is at least one matching topic.

Thanks for any hints.

1 Like