Wednesday, August 29, 2018

Re: How to create complex gin index in postgres db using django

you need to make a migration to apply the index.


something like 

class Migration(migrations.Migration):

    dependencies = [
        ('blog', '0001_initial'),
    ]

    operations = [
        migrations.RunSQL(
            "CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));",
        )
    ]

On Wednesday, August 29, 2018 at 7:16:15 AM UTC-4, Максим Родин wrote:
Hello,
I'm trying to make the text search index like this:
"CREATE INDEX pgweb_idx ON pgweb USING GIN (to_tsvector('english', title || ' ' || body));"

I cannot imagine how to do it in django
Here is my table:

from django.contrib.postgres.indexes import GinIndex
from django.contrib.postgres.search import SearchQuery
from django.contrib.postgres.search import SearchVector
import django.contrib.postgres.search as pg_search


class Request(models.Model):

    open_date
= models.DateTimeField(verbose_name='Creation Date',
                                     blank
=True)
    close_date
= models.DateTimeField(verbose_name='Closing Date',
                                      blank
=True,
                                     
null=True,
                                     
)


   
def in_work_time(self):
       
return timezone.now() - self.open_date


   
def set_open_time(self):
       
self.open_date = timezone.now()


    subject
= models.CharField(max_length=500,
                               verbose_name
='Request subject')
    author
= models.CharField(max_length=500,
                              verbose_name
='Request author')
    content
= models.TextField(verbose_name='Request content')
    engineer
= models.ForeignKey(User, on_delete=models.PROTECT,
                                 blank
=True,
                                 verbose_name
='Engineer assigned')
    priority
= models.IntegerField(default=10,
                                   verbose_name
='Priority')


   
def __str__(self):
       
return self.subject


And here are my thoughts how it could look like.
(It doesn't work):

    en_sv = pg_search.SearchVectorField(SearchVector(
        '
subject', 'content', config='english'), null=True)
    ru_sv = pg_search.SearchVectorField(SearchVector(
        '
subject', 'content', config='russian'), null=True)


    class Meta:
        indexes = [
            GinIndex(fields=['
ru_sv'],
                     fastupdate=False, name='
gin_search_ru_idx'),
            GinIndex(fields=['
en_sv'],
                     fastupdate=False, name='
gin_search_en_idx'),
     ]


Any thoughts?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/101662a8-770b-4b39-8185-015c2f236656%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment