Using Interfaces
We recommend using Classes and Decorators to define the data model. However, if you prefer to not use class decorators you can define the collection schema using Interfaces. This section describes how to define the data model using Interfaces.
You do not need to have all the fields present in your document, defined in the search model, only the fields that you need to be searchable. When you store documents in the search index, we store the entire document including the fields that are not defined in the search model
Declaring Models
Below we describe how the search models can be defined.
// data container
interface Catalog extends TigrisIndexType {
name: string;
price: number;
brand: string;
labels: string;
popularity: number;
entryDate: Date;
available: boolean;
}
// schema definition
const catalogSchema: TigrisIndexSchema<Catalog> = {
name: {
type: TigrisDataTypes.STRING,
},
price: {
type: TigrisDataTypes.NUMBER,
},
brand: {
type: TigrisDataTypes.STRING,
},
labels: {
type: TigrisDataTypes.STRING,
},
popularity: {
type: TigrisDataTypes.INT32,
},
entryDate: {
type: TigrisDataTypes.DATE_TIME,
},
available: {
type: TigrisDataTypes.BOOLEAN,
},
};
Along with defining the interface for the search model, you will also need to define the TigrisIndexSchema definition for the interface. There are additional properties that can be specified to control how the fields in the search model are indexed.
Option | Type | Default | Usage |
---|---|---|---|
searchIndex | boolean | True | To enable/disable indexing on a field. Any non-indexed field won't be searchable. |
sort | boolean | False | To enable/disable sorting on a field. |
facet | boolean | False | To enable/disable faceting on a field. |
// data container
interface Catalog extends TigrisIndexType {
name: string;
price: number;
brand: string;
}
// schema definition
const catalogSchema: TigrisIndexSchema<Catalog> = {
name: {
type: TigrisDataTypes.STRING,
},
number: {
type: TigrisDataTypes.NUMBER,
sort: true,
},
brand: {
types: TigrisDataTypes.STRING,
facet: true,
},
};
Make sure you always keep the search model interface and the TigrisIndexSchema definition in sync.
Indexing Embedded Documents
Tigris supports rich document structures. For example, you can embed related data in a single document. Embedded models allow application to fetch data with fewer queries, thus reducing query activity and increasing efficiency.
Below is an example of embedded model. We first define the ProductItem
type
and then embed it inside the Order
type.
// data containers
export interface ProductItem {
productId: string;
quantity: number;
}
export interface Order extends TigrisIndexType {
userId: bigint;
orderTotal: number;
productItems: ProductItem[];
}
// schema definitions
const productItemSchema: TigrisIndexSchema<ProductItem> = {
productId: {
type: TigrisDataTypes.STRING,
},
quantity: {
type: TigrisDataTypes.NUMBER,
},
};
export const orderSchema: TigrisIndexSchema<Order> = {
userId: {
type: TigrisDataTypes.NUMBER_BIGINT,
},
orderTotal: {
type: TigrisDataTypes.NUMBER,
},
productItems: {
type: TigrisDataTypes.ARRAY,
items: {
type: productItemSchema,
},
},
};
Arrays in documents
Including arrays in your schema requires items
attribute to identify its contents.
export interface City extends TigrisIndexType {
name: string;
neighborhoods: Array<string>;
}
export const CitySchema: TigrisIndexSchema<City> = {
name: {
type: TigrisDataTypes.STRING,
},
neighborhoods: {
type: TigrisDataTypes.ARRAY,
items: {
type: TigrisDataTypes.STRING
},
},
};
Array of array(s)
Following example demonstrates a search model with nested arrays:
export interface Cell {
x: number;
y: number;
value: string;
}
export interface Matrix extends TigrisIndexType {
id: string;
cells: Array<Array<Array<Cell>>>;
}
export const MatrixSchema: TigrisIndexSchema<Matrix> = {
id: {
type: TigrisDataTypes.STRING,
facet: false,
},
cells: {
type: TigrisDataTypes.ARRAY,
items: {
type: TigrisDataTypes.ARRAY,
items: {
type: TigrisDataTypes.ARRAY,
items: {
type: {
x: {
type: TigrisDataTypes.NUMBER,
},
y: {
type: TigrisDataTypes.NUMBER,
},
value: {
type: TigrisDataTypes.STRING,
},
},
},
},
},
},
};
Create a Search Index
Tigris provides an easy to use way to create or modify search indexes.
const client = new Tigris();
const search = client.getSearch();
const catalog = await db.createOrUpdateIndex<Catalog>(
"catalog",
catalogSchema
);
id
field in a document
Every indexed document in Tigris has an id
field of string
type to uniquely
identify it. The id
can be used to get, replace or delete a document in search
index. id
is immutable.
Tigris assigns an auto-generated id
to each document. However, there might be
cases where you want to supply your own ID, for example to avoid duplicate
documents to be indexed. In such cases you can specify an id
field in the schema.