Skip to main content

Search Modeling

The first step is to define search models as part of your application code. The data conforming to these models is stored as JSON documents and indexed in Tigris to facilitate searches.

note

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.

Declaring a model is as simple as defining Go struct type in the code. Defining search index data models is no different from defining regular collection data model.

type Review struct {
Author string `json:"author"`
Rating float64 `json:"rating" tigris:"sort"`
}

type Catalog struct {
Brand string `json:"brand"`
Id *uuid.UUID `json:"id,omitempty"`
Labels string `json:"labels"`
Name string `json:"name" tigris:"sort"`
Popularity int64 `json:"popularity" tigris:"facet,sort"`
Price float64 `json:"price"`
Review Review `json:"review"`
VecField [1536]float64 `json:"vec_field" tigris:"vector"`
}

By default, all fields in the search index are indexes. No explicit tigris:"searchIndex" annotation required as it would be for regular collection. The following optional annotation can be explicitly used to refine how the document field is indexed.

OptionTypeDefaultUsage
sortbooleanFalseTo enable/disable sorting on a field. Not supported for Arrays and Objects without a predefined schema.
facetbooleanFalseTo enable/disable faceting on a field. Not supported for Array of Objects.
info

Documents that you store in a search index can have tigris:"searchIndex:false" annotation. These fields are not searchable. A common use-case is where you have a document with ten fields and you only want five of the fields to be searchable but would like to store the entire document.

Additionally, it is possible to turn sorting or faceting on or off for the model in the search index at any time without the need to drop the index.

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.

  type ProductItem struct {
// Id The product identifier
Id int64 `json:"id"`
// Quantity The quantity of this product in this order
Quantity int64 `json:"quantity"`
}

// Order Collection of documents with details of an order
type Order struct {
// Id A unique identifier for the order
Id int64 `json:"id"`
// OrderTotal The total cost of the order
OrderTotal float64 `json:"order_total"`
// ProductItems The list of products that are part of this order
ProductItems []ProductItem `json:"productItems"`
// UserId The identifier of the user that placed the order
UserId int64 `json:"user_id" tigris:"required"`
}

Vector Field

Tigris supports vector fields. Vector fields are used to store the vector embeddings of the documents. These embeddings are used to perform vector search.

type CatalogDetail struct {
Id int `tigris:"primaryKey,autoGenerate"`
Spec string
Description string
VecField [1536]float64 `json:"vec_field" tigris:"vector"`
}

Arrays in documents

Arrays are defined as usual no additional annotations required.

type City struct {
Name string

Neighborhoods []string
}

same with multidimensional arrays

type Cell struct {
x int64
y int64

Value string
}

export class Matrix {
ID string

Cells [][][]Cell;
}

Create a Search Index

Tigris provides an easy to use way to create or modify search indexes.

client, err := tigris.NewClient(ctx, &tigris.Config{Project: "test"})
if err != nil {
panic(err)
}

defer func() { _ = client.Close() }()

s := client.GetSearch()

err := s.CreateIndexes(ctx, &Catalog{})

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.