| 
Safari Search Syntax
Simple Search
The most basic search with the Safari API can simply be a single search term. If you'd like to find books that contain the term "XML" somewhere in the title or text, you can send "XML" as your search query:
http://safari.oreilly.com/xmlapi/?search=XML
The Safari Search Syntax also allows you to combine several terms with the logical operators AND, OR, NEAR, or ANDNOT. So to find all books that contain the term "XML" near the term "XSLT", you can combine them into the query XML NEAR XSLT. Once escaped for a URL, the API request for this query looks like:
http://safari.oreilly.com/xmlapi/?search=XML%20NEAR%20XSLT
You can also go beyond using keyword searches by creating queries with various operators available in the Safari Search Syntax.
Search Operators
The Safari Search Syntax offers a number of search operators to refine requests even further. These operators can be used alone or in combination to return very specific search results.
| Operator | Description | Query Example |
CODE | Finds matches within code fragments. | CODE "XML::Simple" |
NOTE | Finds matches within Tips and How-Tos. | NOTE "web services" |
TITLE | Finds matches within section titles. | TITLE "web services" |
BOOKTITLE | Finds matches within book titles. | BOOKTITLE LIKE "XML" |
CATEGORY | Finds matches with the given category ID. | CATEGORY=itbooks.security |
AUTHOR | Finds matches by author. | AUTHOR=Wall |
ISBN | Finds matches by ISBN. | ISBN LIKE 1565921496 |
PUBLDATE | Finds matches by publication date. | PUBLDATE > 20020101 |
PUBLISHER | Finds matches with the given publisher name. | PUBLISHER=O'Reilly |
To use several operators in combination, enclose each expression in parenthesis and join them with the logical operators AND, OR, or NEAR.
For example, to return all books with "XML" in the title published by O'Reilly Media, Inc., the following query would work:
(BOOKTITLE LIKE XML) AND (PUBLISHER=O'Reilly)
By changing the operator, the example query can return all books with "XML" in the title that weren't published by O'Reilly Media, Inc.:
(BOOKTITLE LIKE XML) NOT (PUBLISHER=O'Reilly)
Encoding Queries
Because these queries will be part of a URL for request, they need to be properly escaped, or URLEncoded. Characters like spaces, parenthesis, and the equals sign need to be converted to theirhexadecimall numeric value. For a space, that's %20. Luckily all programming environments have shortcuts to encoding strings for URLs. Here are some quick examples in three scripting languages that escape the query and use it to build a request URL:
ASP (VBScript)
Active Server Pages has a built-in Server function called URLEncode.
baseurl = "http://safari.oreilly.com/xmlapi/?search="
q = "BOOKTITLE LIKE XML"
q = Server.URLEncode(q)
'build the request URL
requesturl = baseurl & q
PHP
PHP also has a built-in function called urlencode().
$baseurl = "http://safari.oreilly.com/xmlapi/?search=";
$q = "BOOKTITLE LIKE XML";
$q = urlencode($q);
//build the request URL
$requesturl = $baseurl . $q;
Perl
Perl doesn't have a built-in function, but the URI::Escape module is available with most Perl installations.
use URI::Escape;
my $baseurl = "http://safari.oreilly.com/xmlapi/?search=";
my $q = "BOOKTITLE LIKE XML";
$q = uri_escape($q);
#build the request URL
my $requesturl = $baseurl . $q;
Categories
To download a list of all Safari IT Books category metadata abbreviations, click here.
|