I have a person
table where arbitrary data must be inserted. Since I'm using Postgres 9.4, jsonb
appears to be the right choice.
Example data:
id: 1, name: "Joe Doe", preferences: { color: "red" , toy: "car"}
id: 2, name: "Jane Doe", preferences: { color: "blue", food: "hamburguer" }
The problem is that I need to query it by the variable values, example:
All the people who has "hamburger" on preferences, and partial queries must be possible too, so searching for "burg" must bring me record 2.
Using json functions, I can search for values, as long as I known the key, right? Or there is a fast way to search for the values?
What I'm thinking to do is to create a tsvector column and dump the json to use fulltext search to do the partial queries.
Best Answer
I suggest a
MATERIALIZED VIEW
with unnested values and a trigram index as search tools.hstore
If you don't need nested values,
hstore
may be even better for you. Use the functionsvals(hstore)
to unnesthstore
values.You need to install the additional module
hstore
once per database:Table
Materialized view
This is an implicit
CROSS JOIN LATERAL
to the set-returning functionsvals()
.Trigram Index
You need to install the additional module
pg_trgm
once per database:Then:
Details:
Query
Be aware that this is pretty fast.
jsonb
If you have nested values or
numeric
orboolean
values,jsonb
may be more efficient. You can do the same as above withjsonb_each_text(jsonb)
:Same index, same query. You might want to add the key to the MV and search for that, too: