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

Please clarify about findNearest() method. #2217

Closed
pogorv12 opened this issue Oct 14, 2024 · 2 comments
Closed

Please clarify about findNearest() method. #2217

pogorv12 opened this issue Oct 14, 2024 · 2 comments
Assignees
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.

Comments

@pogorv12
Copy link

pogorv12 commented Oct 14, 2024

Hello!

I want to ask you to clarify guideline for vector based search findNearest() method.

There is difference between how it is described in the doc and how it is implemented in the library (I have the latest firebase-admin 12.6.0 installed).

So, the doc (https://firebase.google.com/docs/firestore/vector-search#make_a_nearest-neighbor_query) isays :

// Requires a single-field vector index
const vectorQuery: VectorQuery = coll.findNearest({
  vectorField: 'embedding_field',
  queryVector: [3.0, 1.0, 2.0],
  limit: 10,
  distanceMeasure: 'EUCLIDEAN'
});

While the working definition is:

const vectorQuery: VectorQuery = coll.findNearest({
  'embedding_field',
   [3.0, 1.0, 2.0],
  { limit: 10,
  distanceMeasure: 'EUCLIDEAN'
});

The vector distance value and threshold as described in https://firebase.google.com/docs/firestore/vector-search#vector-distance and https://firebase.google.com/docs/firestore/vector-search#distance-threshold not available at all.

Method description from the package looks like this:

/**
     * Returns a query that can perform vector distance (similarity) search with given parameters.
     *
     * The returned query, when executed, performs a distance (similarity) search on the specified
     * `vectorField` against the given `queryVector` and returns the top documents that are closest
     * to the `queryVector`.
     *
     * Only documents whose `vectorField` field is a `VectorValue` of the same dimension as `queryVector`
     * participate in the query, all other documents are ignored.
     *
     * @example
     * ```typescript
     * // Returns the closest 10 documents whose Euclidean distance from their 'embedding' fields are closed to [41, 42].
     * const vectorQuery = col.findNearest('embedding', [41, 42], {limit: 10, distanceMeasure: 'EUCLIDEAN'});
     *
     * const querySnapshot = await aggregateQuery.get();
     * querySnapshot.forEach(...);
     * ```
     *
     * @param vectorField The field path this vector query executes on.
     * @param queryVector The vector value used to measure the distance from `vectorField` values in the documents.
     * @param options Options control the vector query. `limit` specifies the upper bound of documents to return, must
     * be a positive integer with a maximum value of 1000. `distanceMeasure` specifies what type of distance is
     * calculated when performing the query.
     */
    findNearest(
      vectorField: string | FieldPath,
      queryVector: VectorValue | Array<number>,
      options: {
        limit: number;
        distanceMeasure: 'EUCLIDEAN' | 'COSINE' | 'DOT_PRODUCT';
      }
    ): VectorQuery<AppModelType, DbModelType>;

No distanceResultField or distanceThreshold fields.

Please advice how to get distanceResultField and distanceThreshold fields working.
Thanks

@pogorv12 pogorv12 added priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns. labels Oct 14, 2024
@product-auto-label product-auto-label bot added the api: firestore Issues related to the googleapis/nodejs-firestore API. label Oct 14, 2024
@MarkDuckworth MarkDuckworth self-assigned this Oct 14, 2024
@MarkDuckworth
Copy link
Contributor

What version of @google-cloud/firestore are you using? Check with npm list @google-cloud/firestore.

Support for distanceResultField and distanceThreshold was added in @google-cloud/[email protected]. If you don't have this version, upgrade with npm upgrade (or if you're not using npm, then use the appropriate command for the package manager you are using).

FWIW, the older definition of findNearest(vectorField, queryVector, options) is still supported alongside the new definition findNearest(vectorQueryOptions).

@pogorv12
Copy link
Author

Thank you @MarkDuckworth ! You explanation helps.

I had @google-cloud/[email protected] despite the latest [email protected]. Update to @google-cloud/[email protected] solved the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: firestore Issues related to the googleapis/nodejs-firestore API. priority: p2 Moderately-important priority. Fix may not be included in next release. type: bug Error or flaw in code with unintended results or allowing sub-optimal usage patterns.
Projects
None yet
Development

No branches or pull requests

2 participants