{
"animals": [
{
"type": "mammal",
"species": "dog",
"name": "FIDO"
},
{
"type": "reptile",
"species": "lizard",
"name": "GEICO"
},
{
"type": "mammal",
"species": "cat",
"name": "DUSTY"
}
]
}
Let’s say I have JSON documents of animals in various shelters (example above).
I want to create index on all documents that contain a mammal and also contain a specific species of animal (user-inputted).
Essentially, the query would look something like this:
SELECT name
FROM shelters
USE INDEX(shelter_idx)
WHERE ANY animal in animals SATISFIES animal.type = "mammal"
AND animal.species = ${species_value} END # this could be "cat" for example
limit 100;
My idea for an index would look something like this:
CREATE INDEX `shelter_idx`
ON `bucket`(DISTINCT ARRAY animal.type FOR animal IN animals WHEN animal.type = "mammal"
AND animal.species END,
name)
WHERE animals IS VALUED
AND name IS VALUED;
I’m honestly wondering if this can be simplified/improved past what I have currently and any suggestions/improvements on the topic would be appreciated!
Also I’ve looked online and found some relatively similar forum posts, but if anyone knows of a question similar to mine, a link would be appreciated as well!