Autocomplete display Post Categories

Hi,

I looked at this page https://community.algolia.com/wordpress/customize-autocomplete.html and saw that there is an suggestion template for terms on Autocomplete.

I would like to display product brands on autocomplete and I am struggling to get it to work properly. The test site is https://speidersport.staging.wpengine.com/ and I get all the categories listed at the top but when continuing typing the list adds up and does not reload.

I can’t figure out how to fix that.

Does anyone know how I can reset the categories on every typeing?

The code is simple for now

jQuery(function(){
		var client = algoliasearch(algolia.application_id, algolia.search_api_key); /* init Algolia client */
		var sources = []; /* setup default sources */
		var brands = []; /* setup default brands */
	
	jQuery.each(algolia.autocomplete.sources, function (i, config) {
		/* We only want to display Products and Posts */
		if(config['index_id'] !== 'searchable_posts'){
			var suggestion_template = (config['index_id'] == 'posts_product') ? wp.template('autocomplete-product-suggestion') : wp.template('autocomplete-post-suggestion');
			var post_title = (config['index_id'] == 'posts_product') ? "Treff på produkter" : "Treff på turtips";
			var hitsPerPage = (config['index_id'] == 'posts_product') ? 5 : 6;
			var index_id = config['index_id'];
			
			sources.push({
				source: 
					algoliaAutocomplete.sources.hits(client.initIndex(config['index_name']), {
							hitsPerPage: hitsPerPage,
							attributesToSnippet: ['content:10'],
							highlightPreTag: '<em>',
							highlightPostTag: '</em>'
					}),
				templates:{
					header: function(){
						return '<div class="autocomplete-header-title">' + post_title + '</div>';
					},
					suggestion: function(hit){
						// Create widgets for Products
						if(index_id == 'posts_product'){
							var aa_container = document.getElementById('algolia-autocomplete-listbox-0');
							
							// Push the product variables to arrays
							brands.push(hit.product_brands.brands[0]);
							
							// Remove duplicates from the arrays
							var the_brands = Array.from(new Set(brands));
							
							// Remove all previous results
							jQuery('.aa-brands').remove();

							// Append (Insert) the new results variables to the Autocpmplete container
							aa_container.insertAdjacentHTML('afterbegin', '<div class="aa-brands"><p>' + the_brands + '</p></div>');
							
							console.log(the_brands);
							
							// Always reset the main results at the end
							the_brands = [];
						}
						
						// Return the true results
						return suggestion_template(hit);
					}
				}
			});
		}
	});
	
/* Setup dropdown menus */
jQuery(algolia.autocomplete.input_selector).each(function(i){
	var $searchInput = jQuery(this);
	var config = {
		debug: algolia.debug,
		hint: false,
		openOnFocus: true,
		appendTo: 'body',
		templates: {
			empty: '<div class="autocomplete-empty">' + "Ingen resultater matchet søket ditt" + '</div>'
		}
	};
	
	/* Instantiate autocomplete.js */
	var autocomplete = algoliaAutocomplete($searchInput[0], config, sources).on('autocomplete:selected', function(e, suggestion){
		/* Redirect the user when we detect a suggestion selection. */
		window.location.href = suggestion.permalink;
	});
	
	/* Force the dropdown to be re-drawn on scroll to handle fixed containers. */
	jQuery(window).scroll(function() {
		if(autocomplete.autocomplete.getWrapper().style.display === "block") {
			autocomplete.autocomplete.close();
			autocomplete.autocomplete.open();
		}
	});
});