FQL v4 will be decommissioned on June 30, 2025. Ensure that you complete your migration from FQL v4 to FQL v10 by that date.

For more details, review the migration guide. Contact support@fauna.com with any questions.

Update an index

Problem

You need to store additional metadata in an index document.

Solution

Set the data field in an index document:

client.query(
  q.Update(q.Index('all_of_the_people'), { data: { team: 'Wizards' } })
)
.then((ret) => console.log(ret))
.catch((err) => console.error(
  'Error: [%s] %s: %s',
  err.name,
  err.message,
  err.errors()[0].description,
))
{
  ref: Index("all_of_the_people"),
  ts: 1631909688850000,
  active: true,
  serialized: true,
  name: 'all_of_the_people',
  source: Collection("People"),
  values: [
    { field: [ 'data', 'first' ] },
    { field: [ 'data', 'last' ] },
    { field: [ 'ref' ] }
  ],
  partitions: 8,
  data: { team: 'Wizards' }
}
result = client.query(
  q.update(q.index("all_of_the_people"), {"data": {"team": "Wizards"}})
)
print(result)
{'ref': Ref(id=all_of_the_people, collection=Ref(id=indexes)), 'ts': 1631911056420000, 'active': True, 'serialized': True, 'name': 'all_of_the_people', 'source': Ref(id=People, collection=Ref(id=collections)), 'values': [{'field': ['data', 'first']}, {'field': ['data', 'last']}, {'field': ['ref']}], 'partitions': 8, 'data': {'team': 'Wizards'}}
result, err := client.Query(
	f.Update(
		f.Index("all_of_the_people"),
		f.Obj{"data": f.Obj{"team": "Wizards"}},
	))

if err != nil {
	fmt.Fprintln(os.Stderr, err)
} else {
	fmt.Println(result)
}
map[active:true data:map[team:Wizards] name:all_of_the_people partitions:8 ref:{all_of_the_people 0xc000109a10 0xc000109a10 <nil>} serialized:true source:{People 0xc000109b00 0xc000109b00 <nil>} ts:1631911525160000 values:[map[field:[data first]] map[field:[data last]] map[field:[ref]]]]
try
{
    Value result = await client.Query(
        Update(
            Index("all_of_the_people"),
            Obj("data", Obj("team", "Wizards"))
        )
    );
    Console.WriteLine(result);
}
catch (Exception e)
{
    Console.WriteLine($"ERROR: {e.Message}");
}
ObjectV(ref: RefV(id = "all_of_the_people", collection = RefV(id = "indexes")),ts: LongV(1632032541470000),active: BooleanV(True),serialized: BooleanV(True),name: StringV(all_of_the_people),source: RefV(id = "People", collection = RefV(id = "collections")),values: Arr(ObjectV(field: Arr(StringV(data), StringV(first))), ObjectV(field: Arr(StringV(data), StringV(last))), ObjectV(field: Arr(StringV(ref)))),partitions: LongV(8),data: ObjectV(team: StringV(Wizards)))
Update(Index('all_of_the_people'), { data: { team: 'Wizards' } })
{
  ref: Index("all_of_the_people"),
  ts: 1631835756760000,
  active: true,
  serialized: true,
  name: 'all_of_the_people',
  source: Collection("People"),
  values: [
    { field: [ 'data', 'first' ] },
    { field: [ 'data', 'last' ] },
    { field: [ 'ref' ] }
  ],
  partitions: 8,
  data: { team: 'Wizards' }
}
Query metrics:
  •    bytesIn:      99

  •   bytesOut:     376

  • computeOps:       1

  •    readOps:       0

  •   writeOps:       1

  •  readBytes:   1,787

  • writeBytes:     221

  •  queryTime: 3,053ms

  •    retries:       0

Discussion

You can Rename an index, add metadata, or change the unique field. However, you cannot change an index’s terms or values definitions after the index has been created.

If you need to modify an index’s terms or values definition, you should create a new index with the new definition, then rename the old index out of the way, and rename the new index to use the original name of the old index.

If you change unique to true, any existing duplicate items in the index are not automatically removed.

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!