What would be the appropriate way to index on dynamic key/value pairs in an object? I need to be able to query for a specific field within the document based on a user-inputted key/value pairing that is located within an object in the document. An example of the object would be:
{
"metaData": {
"id": "123",
"source": "survey"
},
"surveyTags": ["fun", "long", "boring"],
"accountId": "0011b71f-2b68-414d-b99b-f40188a1561f"
}
The kicker is that the contents of the metaData object can be distinct to each document itself (dynamic). So you could have different queries for any given document. Two examples queries that should work for the document above would look like:
SELECT accountId
FROM bucket
USE INDEX(metadata_idx)
WHERE metaData.id = "123"
OR
SELECT accountId
FROM bucket
USE INDEX(metadata_idx)
WHERE metaData.source = "survey"
I would think to use adaptive indexing (https://docs.couchbase.com/server/6.0/n1ql/n1ql-language-reference/adaptive-indexing.html) and index on the metaData object itself so something like:
CREATE INDEX `metadata_idx`
ON `bucket`(DISTINCT PAIRS(metaData), accountId)
WHERE metaData IS VALUED
AND accountId IS VALUED
Any suggestions/improvements on the above would be appreciated!