#community-help

Syncing Firebase 'ref' Field with Typesense

TLDR Guillermo wanted to index a 'ref' field from firebase with typesense. Jason provided a solution which involved syncing ref.path as an addition to the existing fields. Guillermo confirmed the solution worked.

Powered by Struct AI
+12
raised_hands1
white_check_mark1
40
14mo
Solved
Join the chat
Jun 29, 2022 (15 months ago)
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
01:48 PM
I’ am working with typesense and firebase and I want to index a field type ‘ref’, Is there a way to transform the ref field to string when indexing? I was trying defining it with “string*” but no luck
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:51 PM
Guillermo ref is an internal firebase field right?
01:52
Jason
01:52 PM
The extension just goes off of whatever fields Firestore returns as part of this call: https://github.com/typesense/firestore-typesense-search/blob/1b9d22bc1e3fbb55fd548003abbadcbf2be8a3fc/functions/src/utils.js#L32
01:53
Jason
01:53 PM
So if ref isn't returned by Firestore there, then the extension won't be able to sync it
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
02:02 PM
I see,
else if (value instanceof admin.firestore.DocumentReference) {
return null;
}
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:03 PM
Ahh, hmmm... May be we should just remove that else if block...
02:03
Jason
02:03 PM
So it returns value from the else block
02:08
Jason
02:08 PM
Could you open a Github issue for this? Happy to accept a PR for this, or I can take a look in a few days.
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
02:11 PM
Great! I’m on it. We will have to see if then we can index that field to typesense in some way since its basically an object
02:11
Guillermo
02:11 PM
Thanks Jason
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:13 PM
Oh I see, if it is an object, then may be we should add that type to this else if block which handles objects: https://github.com/typesense/firestore-typesense-search/blob/1b9d22bc1e3fbb55fd548003abbadcbf2be8a3fc/functions/src/utils.js#L16
02:13
Jason
02:13 PM
something like else if ((typeof value === "object" && value !== null) || value instanceof admin.firestore.DocumentReference)
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
02:17 PM
the documentReference has a property path that returns a string with the path inside the DB, maybe we could work with that
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:18 PM
So may be we just sync ref.path as a field into Typesense?
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
02:19 PM
That would work for me at least, I’m not sure if there’s another functionality of the ref field that somebody will need later
02:49
Guillermo
02:49 PM
I created the issue, thanks Jason
+11
Jul 05, 2022 (14 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:45 PM
Following up from this:

I think I misunderstood that you were going to do a PR for it... I'll take take a look later today and get back to you.
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
06:52 PM
Ahh! I see, sorry I guess I could give it a try!
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
06:53 PM
That would be great! Especially if you can test it as well with your project
06:53
Jason
06:53 PM
Jul 06, 2022 (14 months ago)
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
01:55 PM
Hi Jason, I can’t seem to be able to create a branch for the issue I created, maybe you have to assign it to me?
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
01:56 PM
You would have to fork the repo, and then create a branch in your forked repo
+11
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
03:48 PM
what params should I be giving the npm run emulator command?

npm run emulator
gives me
error: unknown option '--test-params'
03:52
Guillermo
03:52 PM
With the typesense server I had no problem
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:06 PM
Hmm, could you run firebase --open-sesame extdev first, and then try running the emulator?
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
04:25 PM
npm run emulator:
ext:dev:emulators:start is no longer supported. Instead, navigate to a Firebase project directory and add this extension to the extensions manifest by running:
firebase ext:install
04:26
Guillermo
04:26 PM
and when I try firebase ext:install:
i extensions: ensuring required API <http://firebaseextensions.googleapis.com|firebaseextensions.googleapis.com> is enabled...

Error: HTTP Error: 403, Permission denied to get service [<http://firebaseextensions.googleapis.com|firebaseextensions.googleapis.com>]
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
04:37 PM
Hmmm, not sure why that error shows up. Could you just do a PR for whatever you have? I can then take it test it and take it from there
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
05:27 PM
done, I literally just changed a single line in the code but I cannot test it 😅
Jul 07, 2022 (14 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:38 AM
Thank you for the PR Guillermo. I've posted an update on Github
raised_hands1
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
01:35 PM
It works great! You have to name the field ‘fieldname.path’ on the schema.
01:35
Guillermo
01:35 PM
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
02:15 PM
Hmmm... In my tests it shows up as ref.path
02:15
Jason
02:15 PM
Is "consumer" the name of your firestore collection?
Guillermo
Photo of md5-6f48d9114b22b98994ccbbd9192d9718
Guillermo
02:19 PM
yes
02:22
Guillermo
02:22 PM
This is the error on firestore when the typesense field was the same as the firestore collection
02:22
Guillermo
02:22 PM
but naming the schema field consumer.path worked
02:24
Guillermo
02:24 PM
That’s maybe something to add to the documentation.
Jul 12, 2022 (14 months ago)
Jason
Photo of md5-8813087cccc512313602b6d9f9ece19f
Jason
03:38 AM
Guillermo May I know what your firestore collection structure looks like that generates a ref key in the Firestore doc? I'm trying to replicate this on my Firestore collection