title: Why index on json field not working? Django, large database
body: <p>I have a model in django that has a json field that holds the nutritional values of a meal.</p>
  <pre><code>from django.db import models
  
  
  class Meal(models.Model):
      name = models.CharField(max_length=200)
      portions_count = models.IntegerField()
      attributes = models.JSONField()
  </code></pre>
  <p>I have a million meal records and would like to search based on attributes (gte, lte). Example atrributes field:</p>
  <pre><code>{&quot;cep&quot;: 21.71, &quot;eng&quot;: 23.37, &quot;fep&quot;: 40.91, &quot;pep&quot;: 27.29}
  </code></pre>
  <p>Example view:</p>
  <pre><code>class MealView(View):
      def get(self, request, *args, **kwargs):
          meals = Meal.objects.filter(attributes__eng__gte=12, attributes__cep__gte=12)[:50]
          return render(request, 'meals.html', {
              'meals': meals
          })
  </code></pre>
  <p>...and more advanced queries (attributes, portions count and name together).</p>
  <p>Queries can take a few seconds and if the database is overloaded, it takes longer.</p>
  <p>We tried:</p>
  <pre><code>GinIndex(fields=['attributes']),
  </code></pre>
  <p>or</p>
  <pre><code>Index(fields=['attributes'])
  </code></pre>
  <p>But indexes was not used. (checked in postgres via sql query, pg_stat_all_indexes table and explain select)</p>
  <p>I know that it would definitely be better if the attributes were not in JSON. Ultimately we will try to change it, but at the moment we are looking for a JSON solution.</p>