Default tortoise use BTree index when define index use db_index=True in field, or define indexes use in Meta class, but if you want use other index types, like FullTextIndex in MySQL, or GinIndex in Postgres, you should use tortoise.indexes.Index and its subclasses.


Following is example which use FullTextIndex and SpatialIndex of MySQL:

from tortoise import Model, fields
from tortoise.contrib.mysql.fields import GeometryField
from tortoise.contrib.mysql.indexes import FullTextIndex, SpatialIndex

class Index(Model):
    full_text = fields.TextField()
    geometry = GeometryField()

    class Meta:
        indexes = [
            FullTextIndex(fields={"full_text"}, parser_name="ngram"),

Some built-in indexes can be found in tortoise.contrib.mysql.indexes and tortoise.contrib.postgres.indexes.

Extending Index

Extending index is simply, you just need to inherit the tortoise.indexes.Index, following is example how to create FullTextIndex:

from typing import Optional, Set
from pypika.terms import Term
from tortoise.indexes import Index

class FullTextIndex(Index):

    def __init__(
        *expressions: Term,
        fields: Optional[Set[str]] = None,
        name: Optional[str] = None,
        parser_name: Optional[str] = None,
        super().__init__(*expressions, fields=fields, name=name)
        if parser_name:
            self.extra = f" WITH PARSER {parser_name}"

Differently for Postgres, you should inherit tortoise.contrib.postgres.indexes.PostgresIndex:

class BloomIndex(PostgreSQLIndex):