When you call the
search() method using a query string alone, the results are
returned according to the default query options:
- Documents are returned sorted in order of descending rank
- Documents are returned in groups of 20 at a time
- Retrieved documents contain all of their original fields
You can use an instance of the
as the argument to
to change these options.
The Query class allows you to specify how many documents to return at a time. It also lets you customize the contents of the retrieved documents. You can ask for document identifiers only, or request that documents contain only a subset of their fields. You can also create custom fields in the retrieved documents: snippets (fragments of text fields showing the text surrounding a matched string), and field expressions (fields with values derived from other fields in the document).
Apart from the query options, the
can also include an instance of the
class. Using sort options you can
change the sort order, and sort the results on multiple keys.
Searching with the Query class
When you search with an instance of the Query class, you need to construct an instance of the class in several steps. This is the general order:
- Create a query string.
- Create a Query object that includes the query string and the (optional)
- Call the search method on the Query object.
SortOptions constructors use named arguments, as shown
in this example:
def query_options(): index = search.Index('products') query_string = "product: piano AND price < 5000" # Create sort options to sort on price and brand. sort_price = search.SortExpression( expression='price', direction=search.SortExpression.DESCENDING, default_value=0) sort_brand = search.SortExpression( expression='brand', direction=search.SortExpression.DESCENDING, default_value="") sort_options = search.SortOptions(expressions=[sort_price, sort_brand]) # Create field expressions to add new fields to the scored documents. price_per_note_expression = search.FieldExpression( name='price_per_note', expression='price/88') ivory_expression = search.FieldExpression( name='ivory', expression='snippet("ivory", summary, 120)') # Create query options using the sort options and expressions created # above. query_options = search.QueryOptions( limit=25, returned_fields=['model', 'price', 'description'], returned_expressions=[price_per_note_expression, ivory_expression], sort_options=sort_options) # Build the Query and run the search query = search.Query(query_string=query_string, options=query_options) results = index.search(query) for scored_document in results: print(scored_document)
These properties control how many results are returned and in what order. The offset and cursor options, which are mutually exclusive, support pagination. They specify which selected documents to return in the results.
||The maximum number of documents to return in the results.||20||1000|
||This property determines the accuracy of the result returned by
If the number of matches in the index is less than or equal to the limit, the count returned is exact. Otherwise, the count is an estimate based on the matches that were found and the size and structure of the index. Note that setting a high value for this property can affect the complexity of the search operation and may cause timeouts.
|If unspecified or set to
||The offset of the first document in the results to return.||0. Results will contain all matching documents (up to limit).||1,000|
||A cursor can be used in lieu of an offset to retrieve groups of documents in sorted order. A cursor is updated as it is passed into and out of consecutive queries, allowing each new search to be continued from the end of the previous one. Cursor and offset are discussed on the Handling Results page.||Null. Results will contain all matching documents (up to limit).||-|
||Null. Sort by decreasing document rank.||-|
These properties control what document fields appear in the results.
||Specifies which document fields to include in the results. No more than 100 fields can be specified.||Return all document fields (up to 100 fields).|
||Field expressions describing computed fields that are added to each document returned in the search results. These fields are added to the expressions property of the document. The field value is specified by writing an expression which may include one or more document fields.||None|
||A list of text field names. A snippet is generated for each field. This is a computed field that is added to the expressions property of the documents in the search results. The snippet field has the same name as its source field.
This option implicitly uses the snippet function with only two arguments, creating a snippet with at most one matching string, based on the same query string that the search used to retrieve the results:
You can also create customized snippets with the
The properties of
SortOptions control the ordering and scoring of the search
||A list of
||Maximum number of objects to score and/or sort. Cannot be more than 10,000.||1,000|
Sorting on multiple keys
You can order the search results on multiple sort keys. Each key can be a simple
field name, or a value that is computed from several fields.
Note that the term 'expression' is used with multiple
meanings when speaking about sort options: The
SortOption itself has an
expressions attribute. This attribute is a list of
which correspond to sort keys. Finally, each
SortExpression object contains an
expression attribute which specifies how to calculate the value of the sort
key. This expression is constructed according to the rules in the
SortExpression also defines the direction of the sort and a default key
value to use if the expression cannot be calculated for a document. Here is the
complete list of properties:
||An expression to be evaluated when sorting results for each matching document.||None|
||The direction to sort the search results, either
||The default value of the expression, if no field is present and cannot be calculated for a document. A text value must be specified for text sorts. A numeric value must be specified for numeric sorts.||None|
Sorting on multi-valued fields
When you sort on a multi-valued field of a particular type, only the first value assigned to the field is used. For example, consider two documents, DocA and DocB that both have a text field named "color". Two values are assigned to the DocA "color" field in the order (red, blue), and two values to DocB in the order (green, red). When you perform a sort specifying the text field "color", DocA is sorted on the value "red" and DocB on the value "green". The other field values are not used in the sort.
To sort or not to sort
If you do not specify any sort options, your search results are automatically returned sorted by descending rank. There is no limit to the number of documents that are returned in this case. If you specify any sorting options, the sort is performed after all the matching documents have been selected. There is an explicit property, `SortOptions.limit` , that controls the size of the sort. You can never sort more than 10,000 docs, the default is 1,000. If there are more matching documents than the number specified by `SortOptions.limit` , search only retrieves, sorts, and returns that limited number. It selects the documents to sort from the list of all matching documents, which is in descending rank order. It is possible that a query might select more matching documents than you can sort. If you are using sort options and it is important to retrieve every matching document, you should try to ensure that your query will return no more documents than you can sort.
Expressions are used to define field expressions (which are set in the
) and sort expressions, which are
set in the
SortOptions. They are written as strings:
"price * quantity" "(men + women)/2" "min(daily_use, 10) * rate" "snippet('rose', flower, 120)"
Expressions involving Number fields can use the arithmetical operators (+, -, *, /) and the built-in numeric functions listed below. Expressions involving geopoint fields can use the geopoint and distance functions. Expressions for Text and HTML fields can use the snippet function.
Expressions can also include these special terms:
||A document's rank property. It can be used in field expressions and sort expressions.|
||The score assigned to a document when you include a
The expressions to define numeric values for
SortExpressions can use these built-in functions. The arguments must be numbers, field names, or expressions using numbers and field names.
||Returns the largest of its arguments.||
||Returns the smallest of its arguments.||
||Returns the natural logarithm.||
||Returns the absolute value.||
||Takes two numeric arguments. The call pow(x, y) computes the value of x raised to the y power.||
||Takes a field name as its argument. Returns the number of fields in the document with that name. Remember that a document can contain multiple fields of different types with the same name. Note:
These functions can be used for expressions involving geopoint fields.
||Defines a geopoint given a latitude and longitude.||
||Computes the distance in meters between two geopoints. Note that either of the two arguments can be the name of a geopoint field or an invocation of the geopoint function. However, only one argument can be a field name.||
A snippet is a fragment of a text field that matches a query string and includes
the surrounding text. Snippets are created by calling the
- A quoted query string specifying the text to find in the field.
- The name of a text, HTML, or atom field.
- The maximum number of characters to return in the snippet. This argument is optional; it defaults to 160 characters.
The function returns an HTML string. The string contains a snippet of the body field's value, with the text that matched the query in boldface.