Resources



Articles and Tutorials

Refining Your Search Results with Browse Nodes

Click for a printer friendly version of this document Printer Friendly Save to del.icio.us
Average Review:

A basic search using Amazon ECS returns results from all categories of a search index. For example, a search for books on "Ruby" returns books on the programming language and a strategy guide for Pokemon. This article covers techniques for refining your search results and includes code for implementing the techniques in PHP.

by Jason Levitt
2006-08-08

Searching with Browse Nodes

Browse node searches are very powerful because they let you refine your search to a sspecific product category beneath a search index. For example, if you are only interested in books about the Holocaust, you would likely want to search in browse node ID 4994 (Books > Subjects > History > Jewish > Holocaust) in the Books search index.

A common search strategy is to use the BrowseNode parameter to narrow searches down to a specific browse node, then add the Keywords and Title parameters to furtherfocus the search. The goal is to try to get Amazon ECS to return item listings that closely match what we’re looking for.

To see what Amazon ECS returns for a search within browse node ID 4994, you can put this request in your web browser:

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&
AssociateTag=ws&SubscriptionId=1A7XKHR5BYD0WPJVQEG2&Operation=ItemSearch&
BrowseNode=4994&SearchIndex=Books&Version=2006-06-28

This search will return books from the Holocaust browse node using the default sort order of bestselling and the default response groups of Small and Request.

If you wanted to narrow down the search to books that mention Auschwitz in various product fields, such as the product title and description, you can add the Keywords parameter and set it to the word “Auschwitz”.

You can look at the results of this request by putting this URL in your browser:

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&
AssociateTag=ws&SubscriptionId=1A7XKHR5BYD0WPJVQEG2&
Operation=ItemSearch&BrowseNode=4994&SearchIndex=Books&
Keywords=Auschwitz&Version=2006-06-28

This search will return only books from browse node 4994 that have the word Auschwitz in the product title or description. If there was a browse node for Holocaust books about Auschwitz, you could use that, but there isn’t one.

Note: Such a book would likely be in the category: Books > Subjects > History > Jewish > Holocaust > Auschwitz

Finding Usable Browse Nodes

The question that may come to mind right now is: How do I find the Holocaust browse node ID? Amazon ECS has two helpful mechanisms for finding browse nodes:

  • The BrowseNodeLookup operation returns a list of child browse nodes for a given browse node ID.
  • The BrowseNode response group can be used with the ItemSearch operation to return a list of browse nodes for each item in the response.

The BrowseNodeLookup operation is very useful for creating search trees that you can use to help users browse Amazon’s product categories, but it doesn’t directly help us find usable browse node IDs.

Note: There's a list of popular browse nodes in the Developer Guide under Browse Node Values.

The BrowseNode response group, however, will return a list of browse node IDs that match each item returned in ItemSearch and ItemLookup requests. So, to findusable browse nodes for a certain class of product, we can use the ItemLookup operation with the BrowseNodes response group and analyze the browse node IDs returned in the response.

First, we go to Amazon.com and find some ASINs that are typical of the products we want. We can then write a simple PHP script that runs an item lookup on the items and lists the browse nodes they appear under. Taking the intersection of those browse nodes is a good basis for searching.

In this example, we’re looking for sushi preparation knives. I found two of them on Amazon.com by searching for “sushi knives”. I can use their ASINs to find what browse nodes they’re listed under.

Here's a program to list product browse nodes:

<?php
// Note: The tools.inc.php file is attached to this article
require_once('tools.inc.php');

// List of ASINs (up to 10) to find browse nodes for
$asins='B000288KD4,B0000CE0IA';

// Our Amazon request. Note use of BrowseNodes Response Group
$request='http://webservices.amazon.com/onca/xml?' .
'Service=AWSECommerceService&AssociateTag=ws&SubscriptionId=1A7XKHR5BYD0WPJVQEG2&' .
'Operation=ItemLookup&MerchantId=All&ItemId=' .$asins.
'&ResponseGroup=BrowseNodes&Version=2005-03-23';

// Fetch and process the request
$xml = GetData($request, 10);
$Result = xmlparser($xml);

// Go through each item listing
foreach ($Result['ItemLookupResponse']['Items'][0]['Item'] as $item) {

   // The ASINs should always be returned, but you check anyway
   $asin= (isset($item['ASIN'][0])) ? $item['ASIN'][0] : ' No ASIN!??';
   echo '<br />ASIN = '.$asin.'<br />';

   // Loop through the browsenodes of each item
   foreach ($item['BrowseNodes'][0]['BrowseNode'] as $bnode) {

      // Check for browse node name
      $name=(isset($bnode['Name']))?$bnode['Name']:'No browse node name';
       echo 'Browse Node = '.$bnode['BrowseNodeId'].'<br />';
       echo 'Browse Name = '.$name .'<br />';
      echo '<br />';
   }
}
?>

The ItemLookup method can accept a comma-separated list of up to 10 ASINs. So, you could look at the browse nodes for up to ten products in a single request. The browse nodes associated with an item changes over time, so here's a sample of what you might see:

ASIN = B000288KD4
Browse Node = 289852
Browse Name = Asian Knives

Browse Node = 289854
Browse Name = Boning & Fillet Knives

Browse Node = 1063512
Browse Name = Great Gadgets

Browse Node = 3000591
Browse Name = Under $25

Browse Node = 3375131
Browse Name = Travel

Browse Node = 3737371
Browse Name = All Knives

Browse Node = 13466181
Browse Name = Komachi

Browse Node = 13875901
Browse Name = All Specialty Knives

Browse Node = 13875931
Browse Name = Seafood Knives

ASIN = B0000CE0IA
Browse Node = 289852
Browse Name = Asian Knives

Browse Node = 712216
Browse Name = Bunmei

Browse Node = 3737371
Browse Name = All Knives

The first knife appears in nine browse nodes, while the second one appears in three browse nodes. The two browse nodes they share in common, 3737371 and 289852 are the likely the best ones to use for searching for sushi preparation knifes. The Asian Knives browse node seems like a more specific choice than the All Knives browse node, but only testing them in actual item searches will prove which one works better.

If we added more ASINs to our example, manually determining the browse nodes they have in common would be tedious. Fortunately, we can change our PHP code around a bit to return only the browse nodes that both products appear under.

Here's a program to find the intersection of product browse nodes among several products:

<?php
// Note: The tools.inc.php file is attached to this article
require_once('tools.inc.php');

// List of ASINs (up to 10) to find browse nodes for
$asins='B000288KD4,B0000CE0IA';

// Our Amazon request. Note use of BrowseNodes Response Group
$request='http://webservices.amazon.com/onca/xml?' .
'Service=AWSECommerceService&AssociateTag=ws&SubscriptionId=1A7XKHR5BYD0WPJVQEG2&' .
'Operation=ItemLookup&MerchantId=All&ItemId='.$asins.
'&ResponseGroup=BrowseNodes&Version=2006-06-28';

// Fetch and preocess the request
$xml = GetData($request, 10);
$Result = xmlparser($xml);

// Index to keep track of number of items
$i=0;

// Array of browse nodes in each item
$allnodes=array();

// Loop through items
foreach ($Result['ItemLookupResponse']['Items'][0]['Item'] as $item) {

   // Loop through browse nodes for each item
   foreach ($item['BrowseNodes'][0]['BrowseNode'] as $bnode) {

       // Store browse nodes for each item in the allnodes array
       $allnodes[$i][]=trim($bnode['BrowseNodeId']);
   }

   // Increment index for the next item
   $i++;
}

// Find intersection of browse nodes for all items
$i=0;
$arr1=$allnodes[$i];
$arr2=$allnodes[$i+1];

// Use array_intersect on each set of browse nodes
While (true) {
   $arr2=array_intersect($arr1, $arr2);
   $i++;
   if (!isset($allnodes[$i+1])) break;
   $arr1=$allnodes[$i+1];
}
echo 'Intersection of browse nodes: ';
print_r($arr2);
?>

Running it yields:

Intersection of browse nodes: Array ( [1] => 3737371 [5] => 14309821 )

We can test how well the browse nodes work using the ItemSearch operation. The ItemSearch operation only lets us search in one browse node at a time, but you can do a batch search to do two item searches at once. This batch request returns productsin the two browse nodes 289852 and 3737371.

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&
SubscriptionId=1A7XKHR5BYD0WPJVQEG2&AssociateTag=ws&Operation=ItemSearch&
ItemSearch.1.BrowseNode=289852&ItemSearch.2.BrowseNode=3737371&
ItemSearch.Shared.ResponseGroup=Medium&ItemSearch.Shared.SearchIndex=Kitchen&
Version=2006-06-28

We can do four ItemSearch operations at once by combining two batch searches in a multi-operation request. This specifies two ItemSearch operations. The ItemSearch operations just return products from the four browse nodes: 289852, 3737371, 289854, and 289862.

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&
SubscriptionId=1A7XKHR5BYD0WPJVQEG2&AssociateTag=ws&
Version=2006-06-28&Operation=ItemSearch,ItemSearch&
ItemSearch.1.BrowseNode=289852&ItemSearch.2.BrowseNode=3737371&
ItemSearch.3.BrowseNode=289854&ItemSearch.4.BrowseNode=289862&
ItemSearch.Shared.ResponseGroup=Medium&ItemSearch.Shared.SearchIndex=Kitchen

Since the “All Knives”, “Specialty Knives”, and “Boning and Filet Knives” categories are rather broad, we’ll narrow them down by adding a Keywords parameter to each of those categories. Here, I added the search keyword “sushi” to those requestparameters.

http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&
SubscriptionId=1A7XKHR5BYD0WPJVQEG2&AssociateTag=ws&
Operation=ItemSearch,ItemSearch&ItemSearch.1.BrowseNode=289852&
ItemSearch.2.BrowseNode=3737371&ItemSearch.3.BrowseNode=289854&
ItemSearch.4.BrowseNode=289862&ItemSearch.Shared.ResponseGroup=Medium&
ItemSearch.Shared.SearchIndex=Kitchen&ItemSearch.2.Keywords=sushi&
ItemSearch.3.Keywords=sushi&ItemSearch.4.Keywords=sushi&Version=2006-06-28

As a general rule of thumb, if the browse node is exactly the type of product you want, you can just use it as is, but if the browse nodes category is too broad or not specific, you can add the Keywords parameter to focus the search.

This article is an excerpt from the book, The Web Developer's Guide to Amazon E-commerce Service: Developing Web Applications Using Amazon Web Services And Php. Buy the book from Amazon or find out more about the book at http://awsbook.com.

 



Attachments
Click to download this attachment tools.inc.php (6.3 K)

Discussion
Click to start a discussion on this document Create a New Discussion
No discussion has been created for this document.

Reviews
Create Review Write a Review

aws book, Oct 3, 2006 4:48 AM
Reviewer: Johannes P. Wilbrand
this is just one solution offered by Jason Levitt about how to apply a request to Amazon E-Commerce Service and render the results in PHP. his awsbook has been a constant companion for some time both for it's code examples as well as a resource guide. the internet is moving so fast that i would feel it is time for a second release including the latest developments. one feature very rarely discussed is the Shared request (MultiOperation or BatchRequest) which he explains in great detail and which i have included in some of my example pages. these are however only examples and once one plays a bit more with them one discovers a lack in certain areas. the pagination example in the book should use a > feature rather than listing numbers 1 - 60 or more if there are that many pages within a search category. another feature nicely portrayed is the "find similar items" feature and the "variations" feature. on the downside one has to carefully assemble many of the code snippets before one has a complete store software up and running, which can be hard on the code-novice. all in all, the examples are easy to learn from and they work. Johannes P Wilbrand http://www.downintheflood.com/amazon-ecs-test/

Excellent example, May 6, 2007 7:20 PM
Reviewer: amruthp
This is one of the classic example of using Browsenode's .. I had been trying to use it and was uncessfull. Basically AWS programming guide gives no infrormation of how browsenodes can be used. I feel that AWS should put more emphasis on using Browsenodes. Anyway this was a really good tutorial on how browsenode's can be used. one more good thing I have notices is that author has given the working example which is more helpful

Amazon Webservices is a joke, Aug 30, 2009 11:38 PM
Reviewer: "hinduwebsite"
Why we have to dig through this site to find right information to implement the new API signature? Why don't you provide the right example and documentation that people can understand?
Welcome, Guest Help
Login Login