firebase / firebase/firebase-tools

Firestore emulator doesn't properly handle nested vector fields

Open
#8,077 5 comments 1 reaction 1 assignee Claimed by @harshyyy21 View on GitHub
emulators: firestore reproducible type: bug
Dominant language
TypeScript
Stars
4.5k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
84

Description

### [REQUIRED] Environment info
**firebase-tools:** 13.29.1
**Platform:** macOS

### [REQUIRED] Test case
```ts
import { Response } from "express";
import * as functions from "firebase-functions/v2";
import { firestore } from "firebase-admin";
import { FieldPath, FieldValue } from "firebase-admin/firestore";
import * as admin from "firebase-admin";

export const defaultApp = admin.initializeApp();

defaultApp;

export const setUpUsers = functions.https.onRequest(handler(async () => {
await firestore().collection("users").doc("user1").set({
field: {
deep: FieldValue.vector([1.0, 2.0]),
},
fieldShallow: FieldValue.vector([1.0, 2.0]),
});

return 'Set user';
}));

export const search = functions.https.onRequest(handler(async () => {
return {
deepField: await searchForUsers('field.deep'),
deepFieldDirect: await searchForUsers(new FieldPath('field', 'deep')),
shallowField: await searchForUsers('fieldShallow'),
};
}));

async function searchForUsers(path: FieldPath | string): Promise {
const results = await firestore().collection("users").findNearest({
distanceMeasure: "EUCLIDEAN",
queryVector: FieldValue.vector([1.0, 2.0]),
limit: 10,
vectorField: path,
}).get();

return results.size;
}

function handler(
callback: () => Promise
): (request: functions.https.Request, response: Response) => Promise {
return async (_, response) => {
try {
const result = await callback();
response.json({ result });
response.end();
} catch (error) {
console.log(error);
response.status(500);
response.end();
}

};
}
```
```json
{
"name": "functions",
"scripts": {
"build": "tsc",
"build:watch": "tsc --watch",
"serve": "npm run build && firebase emulators:start --only functions",
"shell": "npm run build && firebase functions:shell",
"start": "npm run shell",
"deploy": "firebase deploy --only functions",
"logs": "firebase functions:log"
},
"engines": {
"node": "20"
},
"main": "lib/index.js",
"dependencies": {
"firebase-admin": "^13.0.0",
"firebase-functions": "^6.0.1"
},
"devDependencies": {
"typescript": "^5.0.0"
},
"private": true
}
```

### [REQUIRED] Steps to reproduce

1. Set up project through `firebase init`
2. Install emulators, functions and firestore
3. Run `firebase emulators:start --project demo-project`
4. Run `http://127.0.0.1:5001/demo-project/us-central1/setUpUsers` in browser
5. Run `http://127.0.0.1:5001/demo-project/us-central1/search`

### [REQUIRED] Expected behavior
The expected output of the function is:
```json
{
"result": {
"deepField": 1,
"deepFieldDirect": 1,
"shallowField": 1
}
}
```
(This is the output produced when running against the live instance.)

### [REQUIRED] Actual behavior
The output of the function is:
```json
{
"result": {
"deepField": 0,
"deepFieldDirect": 0,
"shallowField": 1
}
}
```

It seems the emulator doesn't handle nested vector fields.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.