Global Secondary Index with null key

n1ql
query

#1

With couchbase views, you can emit a null key to the view so that you can optimally build a view of all the documents that meet a given criteria (not taking up memory to store the keys). Then you can easily retrieve all the documents that meet that criteria by querying the view for everything.

I would like to do the same with with GSI, but it seems that you cannot create a secondary index without giving it some kind of key. In my case, any key I give it would just be useless and a waste of memory.

Is there any way that I can create a secondary index without a key?

e.g. CREATE INDEX users ON bucket WHERE type="user" USING GSI
or CREATE INDEX users ON bucket() WHERE type="user" USING GSI
or CREATE INDEX users ON bucket(NULL) WHERE type="user" USING GSI


#2

You can do it, but you must be careful that your queries match your index. You can use one these indexes.

CREATE INDEX idx_users ON mybucket( type ) WHERE type="user";

CREATE INDEX idx_users ON mybucket( NULLIF( type, type ) ) WHERE type="user";

CREATE INDEX idx_users ON mybucket( SUBSTR( type, 0, 0 ) ) WHERE type="user";

#3

Well that’s a little silly! Thanks for the workarounds though.
I assume the one with mybucket( type ) would store “user” for every entry in the index? Or is that somehow optimized?


#4

It would store “user”. You are right, we should relax the limitation and allow constants as index fields. Your use case is not typical.