Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create firestore index from firebase admin SDK with emulator

The recommended way to create indexes for firestore is to use the query you're trying to do in your source code and then clicking on the generated link.

However, when using the emulator, and running the desired query, nothing happens. This is the query I am trying to do:

await firestore.collectionGroup('collectionName').where('specificField', 'array-contains-any', ['']).get();

Since I'm using the emulator, it returns the correct response, but doesn't give me a link to create the actual index in firestore. Do I have to run the code on production in order to get that link?

If anyone also knows how this index should look like in the firestore.indexes.json file I would appreciate that too!

like image 465
joshpetit Avatar asked Oct 27 '25 05:10

joshpetit


2 Answers

See: https://github.com/firebase/firebase-tools/issues/2027

tl;dr - as of Sept '22, the local emulator 'just works' and does not require (or warn/prompt for) composite indexes.

I cannot find any info on how to tease out the required indexes, so it appears devs using the emulator need to 'eyeball' the problem or otherwise somehow catch it themselves.

(Really sketchy aspect of Firestore emulator dev imo - yeesh.)

like image 125
toblerpwn Avatar answered Oct 28 '25 21:10

toblerpwn


As of August 2024, you can use the emulator to obtain all indexes required for the queries that were executed. To retrieve the indexes go to this link after running your code (make sure to replace the {placeholders}):

http://127.0.0.1:8080/emulator/v1/projects/{projectId}:indexUsage?database=projects/{projectId}/databases/(default)

Please note that this is an experimental feature. See this comment on github for details.

like image 25
user3707125 Avatar answered Oct 28 '25 20:10

user3707125



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!