Skip to content

Custom Indexes

Sometimes we need to define custom indexes to speedup queries to our custom fields and/or plugin code.

Clarive enables this by allowing plugin developers to create a special index file under the plugin (or feature) folder structure.

Steps to create an index file

  1. If you don't have one already, create a plugin or feature (ex. myplugin) under CLARIVE_BASE/plugins/myplugin or CLARIVE_BASE/features/myfeature

  2. Create the etc/index/ directory where indexes will reside under the plugin or feature, ie CLARIVE_BASE/plugins/myplugin/etc/index/

  3. Create an YAML index file. One file is enough for all your indexes. For instance CLARIVE_BASE/plugins/myplugin/etc/index/indexes.yaml (the same applies to the feature).

Index file contents

The index YAML file has to follow a structure with a hash/object where every key is a collection where your indexed fields will reside:

collection_name:
  - [ { field1: 1, field2: 1 }, { ...Mongo index options...} ]
  # or
  - [ [ field1, 1, field2, 1 ], { ...Mongo index options...} ]

Here are some examples:

topic:
  - [ [ 'id_category', 1, 'title', 1 ] ]
  - [ [ 'myfield', 1, 'anotherfield', -1 ], { unique: 1 } ]
master_doc:
  - [ [ 'myfield', 1, 'anotherfield', 1 ], { name: 'my index', background: 1 } ]
mycoll:
  - [ { foo: 1 } ]   # with just one field, use hashes

When creating compound indexes, the field_name, 1 tuple follows an order that has to do with the way Mongo indexes files in the database.

In the options placeholder, you can send Mongo any allowed index options, such as:

  • { unique: 1 } - make the index unique, checking for unique ids - be careful!
  • { background: 1 } - create the index in the background, without disrupting normal DB usage
  • { name: "my index name" } - put a name to your index

There are more options available, read the MongoDB documentation for more info:

https://www.mongodb.com/docs/manual/indexes/