Search.../

createIndex( )

Creates an index for a data collection.

Description

The index can't be used immediately, as the process of generating the index takes time. You can check whether your index is ready using listIndexes().

Note that when an index fails to create, the failed index still occupies a slot. To remove the failed index and free up the slot for a new index, use dropIndex().

Admin Method

This function requires elevated permissions to run. This function is not universal and runs only on the backend.

Syntax

function createIndex(dataCollectionId: string, index: Index): Promise<Index>

createIndex Parameters

NAME
TYPE
DESCRIPTION
dataCollectionId
string

ID of the data collection for which to generate the index.

index
Index

Details of the index to be created.

Returns

Details of the index being generated.

Return Type:

Promise<
Index
>
NAME
TYPE
DESCRIPTION
caseInsensitive
boolean

Whether the index ignores case.

Default: false

failure
Failure

Contains details about the reasons for failure when status is FAILED.

fields
Array<
Field
>

Fields for which the index is defined.

Max: 3 fields (for a unique index: 1 field)

name
string

Name of the index.

status
string

Current status of the index.

  • BUILDING: Index creation is in progress.
  • ACTIVE: Index has been successfully created and can be used in queries.
  • DROPPING: Index is in the process of being dropped.
  • DROPPED: Index has been dropped successfully.
  • FAILED: Index creation has failed.
  • INVALID: Index contains incorrectly indexed data.
unique
boolean

Whether the index enforces uniqueness of values in the field for which it is defined. If true, the index can have only one field.

Default: false

Was this helpful?

Create an index (dashboard page code)

Copy Code
1import { indexes } from 'wix-data.v2';
2
3/*
4 * Sample dataCollectionId value = 'Jackets'
5 *
6 * Sample index value:
7 * {
8 * fields: [
9 * {
10 * path: 'price'
11 * }
12 * ],
13 * name: 'byPrice'
14 * }
15 */
16
17
18export async function myCreateIndexFunction(dataCollectionId, index) {
19 try {
20 const createIndexResponse = await indexes.createIndex(dataCollectionId, index);
21 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);
22
23 return createIndexResponse;
24 } catch (error) {
25 console.error(error);
26 // Handle the error
27 }
28}
29
30/* Promise resolves to:
31 * {
32 * "caseInsensitive": false,
33 * "fields": [
34 * {
35 * "order": "ASC",
36 * "path": "price"
37 * }
38 * ],
39 * "name": "byPrice",
40 * "status": "BUILDING",
41 * "unique": false,
42 * }
43 */
44
Create an index (export from backend code)

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { indexes } from 'wix-data.v2';
3import { elevate } from 'wix-auth';
4
5/*
6 * Sample dataCollectionId value = 'Jackets'
7 *
8 * Sample index value:
9 * {
10 * fields: [
11 * {
12 * path: 'price'
13 * }
14 * ],
15 * name: 'byPrice'
16 * }
17 */
18
19export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {
20 try {
21 const elevatedCreateIndex = elevate(indexes.createIndex);
22 const createIndexResponse = await elevatedCreateIndex(dataCollectionId, index);
23 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);
24
25 return createIndexResponse;
26 } catch (error) {
27 console.error(error);
28 // Handle the error
29 }
30});
31
32/* Promise resolves to:
33 * {
34 * "caseInsensitive": false,
35 * "fields": [
36 * {
37 * "order": "ASC",
38 * "path": "price"
39 * }
40 * ],
41 * "name": "byPrice",
42 * "status": "BUILDING",
43 * "unique": false,
44 * }
45 */
46
Create an index

Create a case-insensitive index based on the item name and price in ascending order, and item size in descending order

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { indexes } from 'wix-data.v2';
3import { elevate } from 'wix-auth';
4
5/*
6 * Sample dataCollectionId value = 'Jackets'
7 *
8 * Sample index value:
9 * {
10 * caseInsensitive: true,
11 * fields: [
12 * {
13 * path: 'itemName'
14 * },
15 * {
16 * path: 'price'
17 * },
18 * {
19 * order: 'DESC',
20 * path: 'size'
21 * }
22 * ],
23 * name: 'byItemNameAndPriceAndSize'
24 * }
25 */
26
27export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {
28 try {
29 const elevatedCreateIndex = elevate(indexes.createIndex);
30 const createIndexResponse = await elevatedCreateIndex(dataCollectionId, index);
31 console.log(`Successfully created an index named ${createIndexResponse.name}. Full response: `, createIndexResponse);
32
33 return createIndexResponse;
34 } catch (error) {
35 console.error(error);
36 // Handle the error
37 }
38});
39
40/* Promise resolves to:
41 * {
42 * "caseInsensitive": true,
43 * "fields": [
44 * {
45 * "order": "ASC",
46 * "path": "itemName"
47 * },
48 * {
49 * "order": "ASC",
50 * "path": "price"
51 * },
52 * {
53 * "order": "DESC",
54 * "path": "size"
55 * }
56 * ],
57 * "name": "byItemNameAndPriceAndSize",
58 * "status": "BUILDING",
59 * "unique": false,
60 * }
61 */
62
Create a unique index

Copy Code
1import { Permissions, webMethod } from 'wix-web-module';
2import { indexes } from 'wix-data.v2';
3import { elevate } from 'wix-auth';
4
5/*
6 * Sample dataCollectionId value = 'shoes';
7 *
8 * Sample index value =
9 * {
10 * name: 'bySerialNum',
11 * unique: true,
12 * fields: [
13 * {
14 * path: 'serial'
15 * }
16 * ]
17 * };
18*/
19
20export const myCreateIndexFunction = webMethod(Permissions.Anyone, async (dataCollectionId, index) => {
21 try {
22 const elevatedCreateIndex = elevate(indexes.createIndex);
23 const createUniqueIndexResponse = await elevatedCreateIndex(dataCollectionId, index);
24 console.log(`Successfully created an index named ${createUniqueIndexResponse.name}. Full response: `, createUniqueIndexResponse);
25
26 return createUniqueIndexResponse;
27 } catch (error) {
28 console.error(error);
29 // Handle the error
30 }
31});
32
33/* Returns a promise that resolves to the index being created:
34 * {
35 * "caseInsensitive": false,
36 * "fields": [
37 * {
38 * "order": "ASC",
39 * "path": "serial"
40 * }
41 * ],
42 * "name": "bySerialNum",
43 * "status": "BUILDING",
44 * "unique": true,
45 * }
46 */
47