Example dataset

All the examples in this page use an index composed of the following documents:

[{ 
  "key": "/cs/demo/documentA",
  "body": "Hello world. I'm the content of the 'body' field. It's the default field for queries.",
  "city": "San Francisco, CA",
  "views": 1283,
  "author": "PHP Power Coder"
},

{
  "key": "/cs/demo/documentB",
  "body": "Good morning! The default field of this document contains 'tea'.",
  "city": "Auckland, NZ",
  "views": 892,
  "author": "World Class .NET Developer"
},

{
  "key": "/cs/demo/documentC",
  "body": "Goodbye world. This is the default field of the last document.",
  "city": "Cape Town, SA",
  "views": 8922,
  "author": "Java Power Coder"
}];

Basic queries

There are two types of basic queries: Terms and Phrases. A Term is a single word such as "hello" or "world". For example:

hello

will match: documentA

and

world

will match: documentA, documentC

Notice that documentB does not match, even when the field "author" contains world. See Specifying fields for queries for queries for an explanation.

A Phrase is a group of words surrounded by double quotes such as "hello world". Documents matching phrase queries will contain the exact phrase.

For example:

"hello world"

will match:

documentA

and

"default field"

will match: documentA, documentB, documentC

Specifying fields

By default, all basic queries are executed against the 'body' field of the indexed documents. It's possible to select a different field, by prefixing a basic query with a field name.

For example:

author:world

will match: documentB

and

author:"power coder"

will match: documentA, documentC

Boolean operators

You can combine basic queries with Boolean operators to form a more complex query. Boolean operators define the relationships between Terms or Phrases. CloudSearch supports the following Boolean operators: AND, "+", OR, NOT and "-". Please note that Boolean operators must be all uppercase.

OR

This is the default operator. It will be used if there is no Boolean operator between two terms. This operator makes its surrounding terms optional, but at least one must match the document. For example:

hello last

is the same as

hello OR last

and will match:documentA, documentC

AND

This operator makes its surrounding terms mandatory. For example:

default AND document

will match: documentB, documentC

NOT

The NOT operator excludes documents that contain the term (or phrase) after NOT. For example:

world NOT content

will match: documentC

and

world NOT "default field"

will match:documentA

You can use the NOT operator several times in the same query. For example:

world NOT content NOT bye

will not match any document. CloudSearch DOES NOT support queries that ONLY have NOT terms.

Precedence

The order in which you enter search terms does not effect your results. AND and OR operators have the same precedence and group from left to right. For example:

hello OR last

is the same as

last OR hello

and will match the same documents: documentA, documentC

If you need to modify precedence for complex queries, you can use parentheses. For example:

(good AND world) OR author:power

will match:

documentA, documentC

and

good AND (world OR author:power)

will match:

documentC

Field grouping

You can also use parentheses to specify the field only once.

For example:

author:(world class)

is the same as:

author:world author:class

and will match:documentB

Term weights

You can boost terms in a query by appending the caret operator at the end of a term. Example:

author:world^2 OR city:san^10

This will match all documents, and documentC will be the last result.

Range queries

You can search for documents with field values that are between a lower and upper bound with a range query. For example:

views:[1000 TO 2000]

Will match: documentA

Note that range queries are not reserved for numeric fields. You can also use range queries with text fields:

city:[Beijing TO London]

Will match: documentC