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 specified parameters. After the transaction containing the CreateIndex is completed, the index is immediately available for reads. (The index may not be used in the transaction it was created, and it may not be created in the same transaction as its source collection(s).) 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 collection(s).

When an index is created, documents are immediately indexed if the associated collection contains up to 128 events (which include document creation, updates, and deletion). When an index contains all of a collection’s documents, including each document’s events, it is an "active" index.

For collections with more than 128 events, or those that use a wildcard in their source definition, 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 check whether an index is "active", run the following query (replacing index_name with the name of the index that you want to check):

Select("active", Get(Index("index_name")))

If you see true in the output, the index is "active" and is ready for your queries. Otherwise, you should wait and check again later, until true appears in the output.

The length of the field values specified for the terms or values fields must not exceed 32k bytes. 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 and values.

Unique name required

The index name must be unique within 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

Argument Type Definition and Requirements

param_object

The param_object fields are described below.

param_object

Field Name Field Type Definition and Requirements

name

The logical name of the index. Cannot be events, sets, self, documents, or _.

source

A Collection reference, or an array of one or more source objects describing source collections and (optional) binding fields.

terms

Optional - An array of Term objects describing the fields that should be searchable. Indexed terms can be used to search for field values, via the Match function. The default is an empty Array.

values

Optional - An array of Value objects describing the fields that should be reported in search results. The default is an empty Array. When no values fields are defined, search results report the indexed document’s Reference.

unique

Optional - If true, maintains a unique constraint on combined terms and values. The default is false.

serialized

Optional - If true, writes to this index are serialized with concurrent reads and writes. The default is true.

permissions

Optional - Indicates who is allowed to read the index. The default is everyone can read the index.

data

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 having no data.

Returns

An object containing the metadata about the CreateIndex operations.

Field Name Field Type Definition and Requirements

ref

The reference is an automatically-generated, unique identifier within the database to the index that was created.

name

The logical name of the index created.

source

Array

The source and binding of the newly created index.

ts

The timestamp, with microsecond resolution, associated with the creation of the index.

active

Indicates if the index has completed building and is fully operational.

partitions

Examples

The following query creates an index on the collection "spells" with the name "new-index":

client.Query(
  CreateIndex(
    Obj("name", "new-index", "source", Collection("spells"))));
{
  "ref": { "@ref": "indexes/new-index" },
  "class": { "@ref": "indexes" },
  "ts": 1509244540009619,
  "active": false,
  "partitions": 8,
  "name": "new-index",
  "source": { "@ref": "classes/spells" }
}
curl https://db.fauna.com/ \
    -u fnAChGwBcAACAO70ziE0cfROosNJHdgBmJU1PgpL: \
    -d '{
          "create_index": {
            "object": { "name": "new-index", "source": { "collection": "spells" } }
          }
        }'
HTTP/1.1 201 Created
{
  "resource": {
    "ref": { "@ref": "indexes/new-index" },
    "class": { "@ref": "indexes" },
    "ts": 1509244540009619,
    "active": false,
    "partitions": 8,
    "name": "new-index",
    "source": { "@ref": "classes/spells" }
  }
}
result, _ := client.Query(
    f.CreateIndex(
        f.Obj{"name": "new-index", "source": f.Collection("spells")},
    ),
)

fmt.Println(result)
map[ref:{new-index 0xc4201ef8e0 <nil>} ts:1509244540009619 active:false partitions:8 name:new-index source:{spells 0xc4201efb40 <nil>}]
System.out.println(
      client.query(
          CreateIndex(
              Obj(
                  "name", Value("new-index"),
                  "source", Collection(Value("spells"))
              )
          )
      ).get());
{
  ref: ref(id = "new-index", collection = ref(id = "indexes")),
  ts: 1526674566835735,
  active: false,
  partitions: 8,
  name: "new-index",
  source: ref(id = "spells", collection = ref(id = "collections"))
}
client.query(
  q.CreateIndex({
    name: 'new-index',
    source: q.Collection('spells'),
  })
)
.then((ret) => console.log(ret))
{ ref: Ref(id=new-index, collection=Ref(id=indexes)),
  ts: 1527275052756370,
  active: false,
  partitions: 8,
  name: 'new-index',
  source: Ref(id=spells, collection=Ref(id=collections)) }
client.query(
  q.create_index(
    {"name": "new-index", "source": q.collection("spells")}
  ))
{
  "ref": { "@ref": "indexes/new-index" },
  "class": { "@ref": "indexes" },
  "ts": 1509244540009619,
  "active": false,
  "partitions": 8,
  "name": "new-index",
  "source": { "@ref": "classes/spells" }
}
client.query(
  CreateIndex(
    Obj("name" -> "new-index", "source" -> Collection("spells"))))
{
  "ref": { "@ref": "indexes/new-index" },
  "class": { "@ref": "indexes" },
  "ts": 1509244540009619,
  "active": false,
  "partitions": 8,
  "name": "new-index",
  "source": { "@ref": "classes/spells" }
}

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!