Creating a Gin Index with Trigram (gin_trgm_ops) in Django model

后端 未结 5 1730
轻奢々
轻奢々 2021-02-02 01:29

The new TrigramSimilarity feature of the django.contrib.postgres was great for a problem I had. I use it for a search bar to find hard to spell latin names. The problem is that

5条回答
  •  不要未来只要你来
    2021-02-02 01:53

    I found a 12/2020 article that uses the newest version of Django ORM as such:

    class Author(models.Model):
        first_name = models.CharField(max_length=100)
        last_name = models.CharField(max_length=100)
    
        class Meta:
            indexes = [
                GinIndex(
                    name='review_author_ln_gin_idx', 
                    fields=['last_name'], 
                    opclasses=['gin_trgm_ops'],
                )
            ]
    

    Inspired from an old article on this subject, I landed to a current one which gives the following solution for a GistIndex:

    Update: From Django-1.11 things seem to be simpler, as this answer and django docs sugest:

    from django.contrib.postgres.indexes import GinIndex
    
    class MyModel(models.Model):
        the_field = models.CharField(max_length=512, db_index=True)
    
        class Meta:
            indexes = [GinIndex(fields=['the_field'])]
    

    From Django-2.2, an attribute opclasses will be available in class Index(fields=(), name=None, db_tablespace=None, opclasses=()) for this purpose.


    from django.contrib.postgres.indexes import GistIndex
    
    class GistIndexTrgrmOps(GistIndex):
        def create_sql(self, model, schema_editor):
            # - this Statement is instantiated by the _create_index_sql()
            #   method of django.db.backends.base.schema.BaseDatabaseSchemaEditor.
            #   using sql_create_index template from
            #   django.db.backends.postgresql.schema.DatabaseSchemaEditor
            # - the template has original value:
            #   "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s)%(extra)s"
            statement = super().create_sql(model, schema_editor)
            # - however, we want to use a GIST index to accelerate trigram
            #   matching, so we want to add the gist_trgm_ops index operator
            #   class
            # - so we replace the template with:
            #   "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s gist_trgrm_ops)%(extra)s"
            statement.template =\
                "CREATE INDEX %(name)s ON %(table)s%(using)s (%(columns)s gist_trgm_ops)%(extra)s"
    
            return statement
    

    Which you can then use in your model class like this:

    class YourModel(models.Model):
        some_field = models.TextField(...)
    
        class Meta:
            indexes = [
                GistIndexTrgrmOps(fields=['some_field'])
            ]
    

提交回复
热议问题