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

后端 未结 5 1668
轻奢々
轻奢々 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:58

    This already has an answer, but in Django 2.2 you can do this much easier:

    class MyModel(models.Model):
       name = models.TextField()
       class Meta:
           indexes = [GistIndex(name="gist_trgm_idx", fields=("name",), opclasses=("gist_trgm_ops",))]
    
    

    Alternatively you can use GinIndex.

提交回复
热议问题