<indexName>()
Signature
<indexName>(term: any): set<document>
<indexName>(term: any, range: object): set<document>
Description
An index is a fast lookup mechanism that returns only those documents with fields that match the index parameters defined for a collection.
Parameters
Parameter | Type | Required | Description |
---|---|---|---|
term |
Yes |
Exact index |
|
range |
Range of returned documents The start definition is a single value or an array of values. Arrays are ordered by tuple where entries that match on the first element are then ordered on succeeding elements so anything greater than the first from value and less than the first to value is returned. If matching documents have the same from value, they are compared against the next array element value and returned, accordingly. For example: Given database documents with values of interest as, But if the documents have the following values, where the first values of
interest are equal, The range from and to array element ordering must match the order of the values defined for the index. Array results are sorted in from array element order. If range is omitted, the call returns all documents that match terms. |
Examples
Define an index
-
Define a
bestFlavor
index on theCoffeeBean
collection:{ name: "CoffeeBean", coll: Collection, ts: Time("2023-03-24T15:23:45.330Z"), indexes: { bestFlavor: { terms: [ { field: "Species", }, ], values: [ { field: "Quality_Parameters.Flavor", order: "asc", }, ], queryable: true, status: "complete", }, }, }
The index matches on documents with the terms value provided for
Species
that are also in theQuality_Parameters.Flavor
values field range. Results are returned in ascending,asc
, order. -
Call the
bestFlavor
index method with the species name and a single range value:{ data: [ { id: "360008596716519457", coll: CoffeeBean, ts: Time("2023-03-23T16:43:22.400Z"), Species: "Robusta", Owner: "nishant gurjer", Country_of_Origin: "India", Harvest_Year: 2017, Grading_Date: "October 31st, 2017", Expiration: "October 31st, 2018", Quality_Parameters: { Aroma: 8, Flavor: 7.75, Balance: 7.92, }, Altitude: { unit_of_measurement: "m", mean: 3170, }, }, ], }
It returns the only matching document in the range.
Use indexes with terms and values
-
Change the
bestFlavor
index to match on two values, including documentid
. This allows you to index on multiple values.{ name: "CoffeeBean", coll: Collection, ts: Time("2023-09-10T17:45:08.400Z"), constraints: [], indexes: { bestFlavor: { terms: [ { field: "Species" } ], values: [ { field: "Harvest_Year", order: "asc" }, { field: "id", order: "asc" } ], queryable: true, status: "complete" } } }
-
Get all documents that match the index terms:
{ data: [ { id: "366190504817197124", coll: CoffeeBean, ts: Time("2023-09-05T15:20:49.180Z"), Owner: "metad plc", Country_of_Origin: "Ethiopia", Species: "Arabica", Harvest_Year: 2014, ... elided ... }, { id: "374312704818544708", coll: CoffeeBean, ts: Time("2023-08-28T14:04:33.185Z"), Species: "Arabica", Owner: "Coffee Co., Inc.", Country_of_Origin: "USA", Harvest_Year: 2022, ... elided ... }, { id: "366190711733747780", coll: CoffeeBean, ts: Time("2023-09-10T17:54:49.440Z"), Species: "Arabica", Owner: "Healthy Grounds", Country_of_Origin: "Guatemala", Harvest_Year: 2016, ... elided ... }, { id: "375505851088109633", coll: CoffeeBean, ts: Time("2023-09-10T18:05:35.840Z"), Species: "Arabica", Owner: "Acme Coffee", Country_of_Origin: "Ethiopia", Harvest_Year: 2016 } ] }
-
Filter the results further by the values fields:
{ data: [ { id: "366190711733747780", coll: CoffeeBean, ts: Time("2023-09-10T18:21:37.180Z"), Species: "Arabica", Owner: "Healthy Grounds", Country_of_Origin: "Guatemala", Harvest_Year: 2016, ... elided ... }, { id: "375505851088109633", coll: CoffeeBean, ts: Time("2023-09-10T18:21:54.980Z"), Species: "Arabica", Owner: "Acme Coffee", Country_of_Origin: "Ethiopia", Harvest_Year: 2016 }, { id: "374312704818544708", coll: CoffeeBean, ts: Time("2023-08-28T14:04:33.185Z"), Species: "Arabica", Owner: "Coffee Co., Inc.", Country_of_Origin: "USA", Harvest_Year: 2022, ... elided ... } ] }
All documents that match the index terms and the first element of the values from range are returned.
-
Filter the results further by matching on the second values field, the
id
field, of the most recent documents.{ data: [ { id: "375505851088109633", coll: CoffeeBean, ts: Time("2023-09-10T18:21:54.980Z"), Species: "Arabica", Owner: "Acme Coffee", Country_of_Origin: "Ethiopia", Harvest_Year: 2016 }, { id: "374312704818544708", coll: CoffeeBean, ts: Time("2023-08-28T14:04:33.185Z"), Species: "Arabica", Owner: "Coffee Co., Inc.", Country_of_Origin: "USA", Harvest_Year: 2022, Quality_Parameters: { Aroma: 8.67, Flavor: 8.83, Balance: 8.42 } } ] }
This removes one of the documents with the same
Harvest_Year
value from the results.
Is this article helpful?
Tell Fauna how the article can be improved:
Visit Fauna's forums
or email docs@fauna.com
Thank you for your feedback!