[Django]-How to index just selected json key using Django functionality instead of all keys?

2👍

In PostgreSQL, the feature which allows creating indices on parts of a JSON-structured field is called expression index. It needs to be used together with GIN index to achieve the desired effect. Such a usage is briefly mentioned in the jsonb documentation:

CREATE INDEX idxgintags ON api USING GIN ((jdoc -> 'tags'));

To the best of my knowledge, currently (December 2020) the only way to create such an index in Django is to issue the SQL statements manually.

However, there is an open ticket which asks for adding the support for expression indices to Django. The feature has been accepted but there is no patch yet and the ticket has not been updated for more than a year.

2👍

Functional indexes is going to be available in Django 3.2 in April. This finally should allow to build needed indexes from Django.

1👍

Yes, it is possible, you can read the documentation for django indexing here:
https://docs.djangoproject.com/en/3.0/ref/models/indexes/

Change this line:

indexes = [GinIndex(fields=["search_vector"], name="user_full_name_gin_idx")]

to this:

indexes = [GinIndex(fields=["name"], name="user_full_name_gin_idx")]

Leave a comment