# Creating a GIN index using SearchVector on JSONB field without casting it to text

**URL:** <https://forum.djangoproject.com/t/creating-a-gin-index-using-searchvector-on-jsonb-field-without-casting-it-to-text/11749>\
**Category:** ORM\
**Created:** [January 23, 2022, 4:09pm UTC](https://forum.djangoproject.com/t/creating-a-gin-index-using-searchvector-on-jsonb-field-without-casting-it-to-text/11749 "2022-01-23T16:09:23Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![strogonoff](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/strogonoff/32/7119_2.png) [@strogonoff](https://forum.djangoproject.com/u/strogonoff)\
**Post date:** [January 23, 2022, 4:09pm UTC](https://forum.djangoproject.com/t/creating-a-gin-index-using-searchvector-on-jsonb-field-without-casting-it-to-text/11749/1 "2022-01-23T16:09:23Z")

</div>

I have an index defined like this on my model:

```python
 GinIndex( 
     SearchVector( 
         'some_jsonfield', 
         config='english'), 
     name='jsonfield_ts_gin', 
 ),

```

And the queries made against it are like this (hoping to use more ORM in the future, but resorting to raw SQL for now):

```sql
SELECT id FROM my_table
WHERE to_tsvector('english', some_jsonfield) @@ websearch_to_tsquery('english', %s)

```

One would think the index should apply, but I had a suspicion it didn’t.

Turns out, the following SQL is being generated by the migration that adds the above index:

```auto
CREATE INDEX "some_jsonfield" ON "my_table"
USING gin ((to_tsvector('english'::regconfig, COALESCE(("some_jsonfield")::text, ''))));

```

Django seems (?) to think we want to cast it as `::text` . However, we query without casting body to text, which makes me believe this index does not end up being used.

It’s possible to create the required index using raw SQL, but I wonder whether I’m missing something or it’s a bug in Django.
