# Can I Show Category Pages in Autocomplete Results?

**URL:** <https://community.swiftype.com/t/can-i-show-category-pages-in-autocomplete-results/115>\
**Category:** Installation\
**Tags:** jquery\
**Created:** [December 22, 2015, 6:29pm UTC](https://community.swiftype.com/t/can-i-show-category-pages-in-autocomplete-results/115 "2015-12-22T18:29:08Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex019/uploads/swiftype/original/1X/fd652d1735c60057fa71c371375511028f9a1832.png) [@system](https://community.swiftype.com/u/system)\
**Post date:** [December 22, 2015, 6:29pm UTC](https://community.swiftype.com/t/can-i-show-category-pages-in-autocomplete-results/115/1 "2015-12-22T18:29:08Z")

</div>

> **My products have a “tags” field I use to categorize them. Each category has a dedicated “list” page at [http://mysite.com/tags/[tag\_name]](http://mysite.com/tags/%5Btag_name%5D) where [tag\_name] is the name of the tag. How can I show these tag pages in my autocomplete?**

You can extend the [Swiftype jQuery autocomplete plugin](https://github.com/swiftype/swiftype-autocomplete-jquery) using the following code. An explanation of what it’s doing is below:

```auto
// Given a collection of Swiftype results (results), return a
// collection of count hashes like:
// [{tag: "jacket", count: "5", url:"http://mysite.com/tags/jacket"}, ...]
//
// Requires Underscore.js.
//
var mostPopular = function(results, attribute) {
  var allTags = [];
  var base_url = "http://MYSITE.com/tag/";

  jQuery.each(results, function(idx, result) {
    allTags = allTags.concat(result[attribute]);
  });

  // Removes undefined, null, "" etc...
  allTags = allTags.filter(function(e){return e});

  var counts = {};
  for (var i = 0, j = allTags.length; i < j; i++) {
     counts[allTags[i]] = (counts[allTags[i]] || 0) + 1;
  }

  // Customize this to how you URLify tags.
  var urlify = function(tag) {
    return tag.toLowerCase().replace(/ /g,'-');
  };

  counts = jQuery.map(counts, function(count, tag) {
    return {tag: tag, count: count, url: base_url + urlify(tag)};
  });

  return _.sortBy(counts, function(c) {
    return c.count;
  }).reverse();
};

// Returns the highlighted title if it exists. Falls back to the
// title field if not.
var getTitle = function(item) {
  return item.highlight['title'] || item['title'];
}

var customResultRenderFunction = function(ctx, results) {
  var $list = ctx.list,
    config = ctx.config;

  // Change this if using the Swiftype API.
  var document_type = "page";
  var field_name = "tags";
  var maximum_results = 5;
  var maximum_categories = 3;

  var categories = mostPopular(results[document_type], field_name);
  results = results[document_type];

  categories = categories.slice(0, maximum_categories);
  results = results.slice(0, maximum_results);

  jQuery('<li class="heading">Categories</li>').appendTo($list);
  jQuery.each(categories, function(idx, category) {
    ctx.registerResult(jQuery('<li class="result">' + category.tag + '</li>').appendTo($list), category);
  });

  jQuery('<li class="heading">Products</li>').appendTo($list);
  jQuery.each(results, function(idx, item) {
    ctx.registerResult(jQuery('<li class="result">' + getTitle(item) + '</li>').appendTo($list), item);
  });
};

jQuery(handle).swiftype({
  resultRenderFunction: customResultRenderFunction,
  engineKey: "YOUR_KEY"
});

```

How does this work?  
First, tell the autocomplete plugin to render results using a custom function **customResultRenderFunction**

```auto
jQuery(handle).swiftype({
  resultRenderFunction: customResultRenderFunction,
  engineKey: "YOUR_KEY"
});

```

**customResultRenderFunction** first takes a list of records and passes them to the **mostPopular** function. **mostPopular** returns an array of results and an attribute name and converts them to a counted array, ordered by count, formatted like:

```auto
[{tag: "jacket", count: "5", url:"http://mysite.com/tags/jacket"}, ...

```

**customResultRenderFunction** then limits the categories and results using the **maximum\_categories** and **maximum\_results** variables and prints them according to the code below:

```auto
jQuery('<li class="heading">Categories</li>').appendTo($list);
jQuery.each(categories, function(idx, category) {
  ctx.registerResult(jQuery('<li class="result">' + category.tag + '</li>').appendTo($list), category);
});

```

The actual results are then rendered in their own section.

```auto
jQuery('<li class="heading">Products</li>').appendTo($list);
jQuery.each(results, function(idx, item) {
  ctx.registerResult(jQuery('<li class="result">' + getTitle(item) + '</li>').appendTo($list), item);
});

```

It’s likely you’ll want to change the following variables in **customResultRenderFunction** :

```auto
var document_type = "page";
var field_name = "tags";
var maximum_results = 5;
var maximum_categories = 3;

```

You’ll probably also want to change these items in **mostPopular** :

```auto
var base_url = "http://MYSITE.com/tag/";

// Customize this to how you URLify tags.
var urlify = function(tag) {
  return tag.toLowerCase().replace(/ /g,'-');
};

```
