Sorting product Google CSE results with a custom attribute

Google Custom Search Engine

Google offers a custom search engine service for business and private users to easily integrate the Google search engine into their own websites. You can implement this in multiple ways going from only adding a search box in your website to implementing your own and sending JSON/Atom requests, styling the result yourself. This makes it easy to integrate the power of Google search into your own website without having to spend a lot of money.

But what if you want to sort these results in an E-commerce environment based on custom data, for example on product type or price. That is what this post is about and in this example I will add an attribute “weight” to the product pages and use that to sort my search results.

Adding the attribute to the product pages

The first thing you need to do is tell Google what data it needs to index so it can provide it to you again for filters and sorting. There are 2 ways to do this without manually submitting the data, but in my experience only one works.

The sitemap

The first way to do it is to add the attribute to your sitemap:

<pagemap xmlns="">
<dataobject type="document">
<attribute name="weight">1</attribute>
To add more data to a page you need to add “structured data“. In sitemaps you do this by adding a “pagemap” element to it with a child element called “dataobject“. The “dataobject” is the container for all your attributes that you want to add for a specific type. There are more types available in the pagemap, but for this example we only use “document“.

I added an attribute named “weight” as a custom attribute in the sitemap. Although this is an example that you can find on the Google support page this did not work for me and did not end up showing in my query results.

The HTML page

The second way to add structured data to your search results is to add the pagemap data directly into your HTML page like this:
<DataObject type="document">
<Attribute name="weight">1</Attribute>

You need to add the same thing as in the sitemap (some small changes are required though) to the <head> of your website. You can test if the structure is ok by making use of the Google structured data tester.

Google structured data tester
If your filters show up in the same format as in the above image, your data has been implemented correctly.

Sorting your results

Now all you need to do is wait for Google to re-index your website with this new data. Once that is done you will see the following in your JSON responses:

Now the last step: you add the “sort” parameter to your request URL like this:

Now the results with the highest weight will be shown on top and the lowest at the bottom, hope this post helped implementing this in your website!

Share this post

Share on facebook
Share on google
Share on twitter
Share on linkedin
Share on pinterest
Share on print
Share on email

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.