I am trying to query data stored in an array in a field of type jsonb as follows:
Data description:
There is a table named items which has 4 columns: name, owner, seller, and metadata (metadata is the column that I want to query on). The data in metadata looks like this:
metadata {
...
"attributes": [
{
"trait_type": "rareity"
"value": "ultra-rare",
},
{
"trait_type": "color"
"value": "red",
},
{
"trait_type": "attract"
"value": 6,
},
{
"trait_type": "defend"
"value": 5,
},
...
],
...
}
There are usually many pairs of trait_type and value in attributes , a attributes in level 1 of metadata column. Pairs in attributes can be added arbitrarily. For example, I can add pair { "trait_type": "material", "value": "silk" } into attributes in example above.
Question:
I want to perform a mix of multiple string matching searches and number in range searches. For example: trait_type = material and 10 < defend < 20 and attract > 5.
I have thought of separating attributes into a table with 2 colums trait_type and value and querying SQL using intersect but when adding more conditions, I have to add more intersect also and It is very ineffective.
Is there any table design, indexing strategy, tools... so I can effectively query that kind of data?