CreateIndex
CreateIndex( param_object )
CreateIndex( param_object )
CreateIndex( param_object )
CreateIndex( param_object )
create_index( param_object )
CreateIndex( param_object )
Description
The CreateIndex
function adds a new index to the database with the
provided parameters. After the transaction with the CreateIndex
is
complete, the index is then available for reads. (The index may not be
used in its creation transaction, and it may not be created in the same
transaction as its source collections.) The index may return incomplete
results until it is fully built and marked as active. Fauna builds
the index asynchronously by scanning over relevant documents of the
source collections.
When an index is created, documents are indexed synchronously if the associated collection has up to 128 events (which include document creation, updates, and deletions). Once an index has completed scanning document events, it becomes an "active" index and is available for querying. For collections with more than 128 events (which include document creation, updates, and deletions), indexing is handled by a background task, and you may have to wait a short period before the index returns values. Until the indexing task is complete, the index is an "inactive" index. To see whether an index is "active," run the following query (replacing
If you see |
The maximum size of an index entry, which is comprised of the terms
and values content (and some overhead to distinguish multiple
fields), must not exceed 64k bytes. If an index
entry is too large, the query that created/updated the index
entry fails.
|
Updating index fields
-
It is possible to rename an index by updating its name field. Renaming an index changes its reference, but preserves inbound references to the index. Index data is not rebuilt.
-
If you update the
unique
field, it does not remove existing duplicated items from the index, but does prevent future duplicates from being added. -
The fields of an index which may not be modified are
terms
andvalues
.
Unique name required
The index name must be unique in the database. If you try to lookup an index by name and only create it if it does not exist, the operation might fail if another transaction created the index in the meantime.
Deleting an index
When an index is deleted, it becomes inaccessible, and its data is deleted asynchronously.
Parameters
Parameter | Type | Definition and Requirements |
---|---|---|
|
The |
param_object
Field Name | Field Type | Definition and Requirements | ||
---|---|---|---|---|
|
The logical name of the index. Cannot be |
|||
|
A Collection reference, or an array of one or more source objects describing source collections and (optional) binding fields.
|
|||
|
Optional - An array of Term
objects describing the fields that should be searchable. Indexed
terms can be used to search for field values using the |
|||
|
Optional - An array of Value
objects describing the fields that should be reported in search
results. The default is an empty Array. When there is no
|
|||
|
Optional - If |
|||
|
Optional - If If The default is See Isolation levels for details. |
|||
|
Optional - Indicates who is allowed to read the index. The default is everyone can read the index. |
|||
|
Optional - This is user-defined metadata for the index. It is provided for the developer to store information at the index level. The default is an empty object with no data. |
Returns
An object with the metadata about the CreateIndex
operations.
Field Name | Field Type | Definition and Requirements |
---|---|---|
|
The Reference of the created index. |
|
|
The name of the index created. |
|
|
The source and binding of the newly created index. |
|
|
The timestamp, with microsecond resolution, associated with the creation of the index. |
|
|
Indicates if the index has completed building and is fully operational. |
|
|
The number of partitions used by this index. This value can be 8 or 1:
|
Examples
Create an index with terms
and values
The following example creates an index called
spells_by_element_with_name
for the collection spells
.
The index includes the element
field in its terms
definition, which
means that you can use this index to search for documents in the
spells
collection by values in the documents' element
field.
The index also includes the name
field in its values
definition,
meaning that matching entries from this index are sorted by the values
of the indexed documents' name
field.
ObjectV(ref: RefV(id = "spells_by_element_with_name", collection = RefV(id = "indexes")),ts: LongV(1655934674090000),active: BooleanV(True),serialized: BooleanV(True),name: StringV(spells_by_element_with_name),source: RefV(id = "spells", collection = RefV(id = "collections")),terms: Arr(ObjectV(field: Arr(StringV(data), StringV(element)))),values: Arr(ObjectV(field: Arr(StringV(data), StringV(name)))),partitions: LongV(1))
map[active:true name:spells_by_element_with_name partitions:1 ref:{spells_by_element_with_name 0x140000924b0 0x140000924b0 <nil>} serialized:true source:{spells 0x140000925a0 0x140000925a0 <nil>} terms:[map[field:[data element]]] ts:1657754535190000 values:[map[field:[data name]]]]
{ref: ref(id = "spells_by_element_with_name", collection = ref(id = "indexes")), ts: 1655935684020000, active: true, serialized: true, name: "spells_by_element_with_name", source: ref(id = "spells", collection = ref(id = "collections")), terms: [{field: ["data", "element"]}], values: [{field: ["data", "name"]}], partitions: 1}
{
ref: Index("spells_by_element_with_name"),
ts: 1655934099560000,
active: true,
serialized: true,
name: 'spells_by_element_with_name',
source: Collection("spells"),
terms: [ { field: [ 'data', 'element' ] } ],
values: [ { field: [ 'data', 'name' ] } ],
partitions: 1
}
{'ref': Ref(id=spells_by_element_with_name, collection=Ref(id=indexes)), 'ts': 1655935888700000, 'active': True, 'serialized': True, 'name': 'spells_by_element_with_name', 'source': Ref(id=spells, collection=Ref(id=collections)), 'terms': [{'field': ['data', 'element']}], 'values': [{'field': ['data', 'name']}], 'partitions': 1}
{
ref: Index("spells_by_element_with_name"),
ts: 1655934099560000,
active: true,
serialized: true,
name: 'spells_by_element_with_name',
source: Collection("spells"),
terms: [ { field: [ 'data', 'element' ] } ],
values: [ { field: [ 'data', 'name' ] } ],
partitions: 1
}
To use this index to search for all spells with the water
element:
ObjectV(data: Arr(StringV(Hippo's Wallow), StringV(Water Dragon's Claw)))
map[data:[Hippo's Wallow Water Dragon's Claw]]
{data: ["Hippo's Wallow", "Water Dragon's Claw"]}
{ data: [ "Hippo's Wallow", "Water Dragon's Claw" ] }
{'data': ["Hippo's Wallow", "Water Dragon's Claw"]}
{ data: [ "Hippo's Wallow", "Water Dragon's Claw" ] }
Create a collection index
The following example creates a "collection" index — an index with no
terms
or values
defined — called new-index
for the collection
spells
:
ObjectV(ref: RefV(id = "new-index", collection = RefV(id = "indexes")),ts: LongV(1603756181200000),active: BooleanV(True),serialized: BooleanV(True),name: StringV(new-index),source: RefV(id = "spells", collection = RefV(id = "collections")),partitions: LongV(8))
map[active:true name:new-index partitions:8 ref:{new-index 0x1400011d950 0x1400011d950 <nil>} serialized:true source:{spells 0x1400011da40 0x1400011da40 <nil>} ts:1657754502810000]
{ref: ref(id = "new-index", collection = ref(id = "indexes")), ts: 1593464646510000, active: true, serialized: true, name: "new-index", source: ref(id = "spells", collection = ref(id = "collections")), partitions: 8}
{
ref: Index("new-index"),
ts: 1591996190530000,
active: true,
serialized: true,
name: 'new-index',
source: Collection("spells"),
partitions: 8
}
{'ref': Ref(id=new-index, collection=Ref(id=indexes)), 'ts': 1592856422060000, 'active': True, 'serialized': True, 'name': 'new-index', 'source': Ref(id=spells, collection=Ref(id=collections)), 'partitions': 8}
{
ref: Index("new-index"),
ts: 1624310362210000,
active: true,
serialized: true,
name: 'new-index',
source: Collection("spells"),
partitions: 8
}
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!