Sunday 26 September 2021

ArangoDB: Create an index

'collection.ensureIndex(index-description)' method creates an index if none exists with the given description.

 

'index-description' is a JSON object, following table summarizes the attributes of 'index-description' object.

 

Attribute

Description

type

It is one of the following values.

 

a.   persistent: persistent index

b.   fulltext: fulltext index

c.    geo: geo index, with one or two attributes

name

Specifies index name. It must be unique within the collection. If name is not specified, then a unique name will be generated by ArsangoDB.

sparse

Boolean value either true or false. A sparse index does not contain documents for which at least one of the index attribute is not set or contains a value of null.

unique

Boolean value either true or false.

deduplicate

Boolean value either true or false. It controls whether inserting duplicate index values from the same document into a unique array index will lead to a unique constraint error or not.

 

Let’s create ‘employees’ collection to experiment with.

127.0.0.1:8529@abc_org> db._create("employees")
[ArangoCollection 482, "employees" (type document, status loaded)]

 

ArongoDB create an index on _key attribute by default.

127.0.0.1:8529@abc_org> db.employees.getIndexes()
[ 
  { 
    "fields" : [ 
      "_key" 
    ], 
    "id" : "employees/0", 
    "name" : "primary", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "primary", 
    "unique" : true 
  } 
]

 

Example 1: Create a persistent index on field age

127.0.0.1:8529@abc_org> db.employees.ensureIndex({ type: "persistent", fields: ["age"] });
{ 
  "deduplicate" : true, 
  "fields" : [ 
    "age" 
  ], 
  "id" : "employees/608", 
  "isNewlyCreated" : true, 
  "name" : "idx_1699387292727115776", 
  "selectivityEstimate" : 1, 
  "sparse" : false, 
  "type" : "persistent", 
  "unique" : false, 
  "code" : 201 
}

 

Example 2: Create a persistent index on fields firstName, lastName

127.0.0.1:8529@abc_org> db.employees.ensureIndex({ type: "persistent", fields: ["firstName", "lastName"], name: "firstNameLastNameIndex" });
{ 
  "deduplicate" : true, 
  "fields" : [ 
    "firstName", 
    "lastName" 
  ], 
  "id" : "employees/648", 
  "isNewlyCreated" : true, 
  "name" : "firstNameLastNameIndex", 
  "selectivityEstimate" : 1, 
  "sparse" : false, 
  "type" : "persistent", 
  "unique" : false, 
  "code" : 201 
}

Now query for all the indexes created for the collection employees.

127.0.0.1:8529@abc_org> db.employees.getIndexes()
[ 
  { 
    "fields" : [ 
      "_key" 
    ], 
    "id" : "employees/0", 
    "name" : "primary", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "primary", 
    "unique" : true 
  }, 
  { 
    "deduplicate" : true, 
    "fields" : [ 
      "age" 
    ], 
    "id" : "employees/608", 
    "name" : "idx_1699387292727115776", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "persistent", 
    "unique" : false 
  }, 
  { 
    "deduplicate" : true, 
    "fields" : [ 
      "firstName", 
      "lastName" 
    ], 
    "id" : "employees/648", 
    "name" : "firstNameLastNameIndex", 
    "selectivityEstimate" : 1, 
    "sparse" : false, 
    "type" : "persistent", 
    "unique" : false 
  } 
]



 

Previous                                                    Next                                                    Home

No comments:

Post a Comment