title: How to index jsonb integer values
body: <p>I'm trying to use the "newish" JSONB type.</p>
  
  <p>I have a <code>documents</code> table with a <code>properties</code> jsonb field, and in that is a field <code>publication_year</code>.  I want to find all document records within a year range e.g. 2013-2015.  <em>[EDIT: Querying for a range of values is the main challenge here, even though I have used an exact match example below. The requested approach would also apply for, say dollar ranges (price > $20 and price &lt; $40) or timestamp ranges).]</em></p>
  
  <p>I have tried:</p>
  
  <pre><code>create index test1 on documents using gin ((cast(properties-&gt;'announced_on_year' as integer)));
  
  ERROR:  cannot cast type jsonb to integer
  </code></pre>
  
  <p>as well as:</p>
  
  <pre><code>create index test1 on documents using gin (cast(properties-&gt;&gt;'publication_year' as integer));
  
  ERROR:  data type integer has no default operator class for access method "gin"
  HINT:  You must specify an operator class for the index or define a default operator class for the data type.`
  </code></pre>
  
  <p>I saw from this post <a href="http://www.postgresql.org/message-id/10736.1409063604@sss.pgh.pa.us" rel="noreferrer">http://www.postgresql.org/message-id/10736.1409063604@sss.pgh.pa.us</a> that this should be possible, but I can't figure out the right syntax.</p>
  
  <p>When I just do a straightforward index:</p>
  
  <pre><code>create index test1 on documents using gin ((properties-&gt;'publication_year'));
  </code></pre>
  
  <p>an index is created, but I cannot query it using integer values to get a range going, it says</p>
  
  <pre><code>select count(*) from documents where properties-&gt;&gt;'publication_year' = 2015;
  ERROR:  operator does not exist: text = integer
  LINE 1: ...*) from documents where properties-&gt;&gt;'publication_year' = 2015;
                                ^
  HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
  </code></pre>
  
  <p>Any tips and hints highly appreciated. I'm sure others will benefit too. TIA</p>