Amazon CloudSearch
Developer Guide (API Version 2011-02-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Using Boolean Operators in Amazon CloudSearch Text Searches

When searching text fields with either the q or bq parameter, you can use the Boolean operators + (AND), | (OR), and - (NOT). These shortcuts only work for text searches. To create Boolean queries that search uint and literal fields, you need to use the Boolean query syntax described in Constructing Boolean Search Queries in Amazon CloudSearch.

If you separate search terms with + or a space, Amazon CloudSearch matches documents that contain all of the specified search terms—they are ANDed together. You can use the | (OR) operator to separate terms when you want to match documents that contain either the preceding term(s) or the following term(s).

To exclude documents that contain a particular term from the search results, prefix the term with the - (NOT) operator. For example, to search for all of the documents that don't contain the term star in the default search field, you would specify: search?q=-star. The NOT operator only applies to individual terms. Searching for search?q=-star+wars retrieves all documents that do not contain the term star, but do contain the term wars.

Note

To retrieve all of the documents in your domain, you can prefix a term that you know doesn't exist in your domain's data with the NOT operator, for example -1234567. However, keep in mind that this is a resource intensive operation if you have a large dataset and might be subject to timeouts.

For example, when searching the sample movie data:

  • search?q=star|wars matches movies that contain either star or wars in the default search field.

  • search?bq=title:'story funny|underdog' matches movies that contain both the terms story and funny or the term underdog in the title field.

  • search?bq=title:'red|white|blue' matches movies that contain either red, white, or blue in the title field.

  • search?bq=actor:'"evans, chris"|"Garity, Troy"' matches movies that contain either the phrase evans, chris or the phrase Garity, Troy in the actor field.

  • search?bq='title:-star+war|world' matches movies whose titles do not contain star, but do contain either war or world.

You can also use the Boolean operators when constructing queries using the full Boolean query syntax. For example, search?bq=(and director:'Lucas|Spielberg' (not actor:'"Ford, Harrison"')) matches movies that either Lucas or Speilberg directed, but did not star Harrison Ford. For more information about the Boolean query syntax, see Constructing Boolean Search Queries in Amazon CloudSearch.