Skip to content

Firestore emulator doesn't properly handle nested vector fields #8077

@jellynoone

Description

@jellynoone

[REQUIRED] Environment info

firebase-tools: 13.29.1
Platform: macOS

[REQUIRED] Test case

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<number> {
    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<any>
): (request: functions.https.Request, response: Response) => Promise<void> {
    return async (_, response) => {
        try {
            const result = await callback();
            response.json({ result });
            response.end();
        } catch (error) {
            console.log(error);
            response.status(500);
            response.end();
        }

    };
}
{
  "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:

{
    "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:

{
    "result": {
        "deepField": 0,
        "deepFieldDirect": 0,
        "shallowField": 1
    }
}

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

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions