Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jellynoone opened this issue Dec 21, 2024 · 1 comment
Open

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

jellynoone opened this issue Dec 21, 2024 · 1 comment

Comments

@jellynoone
Copy link

[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.

@jellynoone
Copy link
Author

There was an issue with nested vector fields, before googleapis/nodejs-firestore#2081
Linking in case it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants