Class TableFindAndRerankCursor<T, TRaw>

Overview (preview)

A lazy iterator over the results of some generic findAndRerank operation on the Data API.

Shouldn't be directly instantiated, but rather spawned via Collection.findAndRerank.


Typing

For most intents and purposes, you may treat the cursor as if it is typed simply as Cursor<T>.

If you're using a projection, it is heavily recommended to provide an explicit type representing the type of the document after projection.

In full, the cursor is typed as FindCursor<T, TRaw>, where

  • T is the type of the mapped records, and
  • TRaw is the type of the raw records before any mapping.

If no mapping function is provided, T and TRaw will be the same type. Mapping is done using the FindAndRerankCursor.map method.


Options

Options may be set either through the findAndRerank({}, options) method, or through the various fluent builder methods, which, unlike Mongo, do not mutate the existing cursor, but rather return a new, uninitialized cursor with the new option(s) set.

Example

const collection = db.collection('hybrid_coll');

const cursor: Cursor<Person> = collection.findAndRerank({}, {
sort: { $hybrid: 'what is a car?' },
includeScores: true,
});

for await (const res of cursor) {
console.log(res.document);
console.log(res.scores);
}

See

CollectionFindAndRerankCursor

Type Parameters

Hierarchy (view full)

Constructors

Properties

_filter: SerializedFilter
_mapping?: ((doc) => T)

Type declaration

    • (doc): T
    • Parameters

      • doc: any

      Returns T

_nextPageState: QueryState<string> = ...
_serdes: SerDes<any, any>
_state: CursorState = 'idle'
bufferedCount: "ERROR: `.bufferedCount()` has been renamed to be simply `.buffered()`"

This temporary error-ing property exists for migration convenience, and will be removed in a future version.

Deprecated

  • .bufferedCount() has been renamed to simply be .buffered().
map: (<R>(map) => TableFindAndRerankCursor<R, TRaw>)

Type declaration

project: (<RRaw>(projection) => TableFindAndRerankCursor<RRaw, RRaw>)

Type declaration

readBufferedDocuments: "ERROR: `.readBufferedDocuments()` has been renamed to be `.consumeBuffer()`"

This temporary error-ing property exists for migration convenience, and will be removed in a future version.

Deprecated

  • .readBufferedDocuments() has been renamed to be .consumeBuffer().

Accessors

Methods

  • An async iterator that lazily iterates over all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was broken early.

    Returns AsyncGenerator<T, void, void>

    Example

    for await (const doc of cursor) {
      console.log(doc);
    }
  • Overview

    Sets the filter for the cursor, overwriting any previous filter.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new filter set.

    Parameters

    Returns this

    A new cursor with the new filter set.

    Example

    await table.insertOne({ name: 'John', ... });

    const cursor = table.findAndRerank({})
    .sort({ $hybrid: 'big burly man' })
    .filter({ name: 'John' });

    // The cursor will only return records with the name 'John'
    const john = await cursor.next();
    john.name === 'John'; // true
  • Iterates over all records in the cursor, calling the provided consumer for each record.

    If the consumer returns false, iteration will stop.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return immediately.

    It will close the cursor when iteration is complete, even if it was stopped early.

    Parameters

    • consumer: ((doc) => boolean | Promise<boolean>) | ((doc) => void | Promise<void>)

      The consumer to call for each record.

    Returns Promise<void>

    A promise that resolves when iteration is complete.

    Remarks

    If you get an IDE error "Promise returned from forEach argument is ignored", it is a known WebStorm bug.

  • Overview

    Retrieves the vector used to perform the vector search, if applicable.

    • If includeSortVector is not true, this will unconditionally return null. No find request will be made.

    • If sort: { $hybrid: { $vector } } was used, getSortVector() will simply regurgitate that same $vector.

    • If sort: { $hybrid: { $vectorize } } was used, getSortVector() will return the $vector that was created from the text.

    • If vector search is not used, getSortVector() will simply return null. A find request will still be made.

    If includeSortVector is true, and this function is called before any other cursor operation (such as .next() or .toArray()), it'll make an API request to fetch the sort vector, filling the cursor's buffer in the process.

    If the cursor has already been executed before this function has been called, no additional API request will be made to fetch the sort vector, as it has already been cached.

    But to reiterate, if includeSortVector is false, and this function is called, no API request is made, and the cursor's buffer is not populated; it simply returns null.

    Returns Promise<null | DataAPIVector>

    The sort vector, or null if none was used (or if includeSortVector !== true).

  • Overview

    Sets the maximum number of records to consider from the underlying vector and lexical searches.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new limit set.


    Different formats

    Either a single number, or an object may be provided as a limit definition.

    If a single number is specified, it applies to both the vector and lexical searches.

    To set different limits for the vector and lexical searches, an object containing limits for each vector and lexical column must be provided.

    • For collections, it looks like this: { $vector: number, $lexical: number }

    Parameters

    • hybridLimits: number | Record<string, number>

      The hybrid limits for this cursor.

    Returns this

    A new cursor with the new hybrid limits set.

    Example

    await collection.insertMany([
    { name: 'John', age: 30, $vectorize: 'an elder man', $lexical: 'an elder man' },
    { name: 'Jane', age: 25, $vectorize: 'a young girl', $lexical: 'a young girl' },
    ]);

    const cursor = collection.find({})
    .sort({ $hybrid: 'old man' })
    .hybridLimits(1);

    // The cursor will return only one record
    const all = await cursor.toArray();
    all.length === 1; // true
  • Overview

    Determines whether the RerankedResult.scores is returned for each document.

    If this is not set, then the scores will be an empty object for each document.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new setting.

    Parameters

    • Optional includeScores: boolean

      Whether the scores should be included in the result.

    Returns this

    A new cursor with the new scores inclusion setting.

    Example

    const cursor = table.findAndRerank({ name: 'John' })
    .sort({ $hybrid: 'old man' })
    .includeScores();

    for await (const res of cursor) {
    console.log(res.document);
    console.log(res.scores);
    }
  • Overview

    Sets whether the sort vector should be fetched on the very first API call. Note that this is a requirement to use FindAndRerankCursor.getSortVector—it'll unconditionally return null if this is not set to true.

    • This is only applicable when using vector search, and will be ignored if the cursor is not using vector search.
    • See FindAndRerankCursor.getSortVector to see exactly what is returned when this is set to true.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new setting.

    Parameters

    • Optional includeSortVector: boolean

      Whether the sort vector should be fetched on the first API call

    Returns this

    A new cursor with the new sort vector inclusion setting.

    Example

    const cursor = table.findAndRerank({)
    .sort({ $hybrid: 'old man' })
    .includeSortVector();

    // The cursor will return the sort vector used
    // Here, it'll be the embedding for the vector created from the term 'old man'
    const sortVector = await cursor.getSortVector();
    sortVector; // DataAPIVector([...])
  • Overview

    Sets the maximum number of records to return.

    If limit == 0, there will be no limit on the number of records returned (beyond any that the Data API may itself enforce).

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new limit set.

    Parameters

    • limit: number

      The limit for this cursor.

    Returns this

    A new cursor with the new limit set.

    Example

    await collection.insertMany([
    { name: 'John', age: 30, $vectorize: 'an elder man', $lexical: 'an elder man' },
    { name: 'Jane', age: 25, $vectorize: 'a young girl', $lexical: 'a young girl' },
    ]);

    const cursor = collection.find({})
    .sort({ $hybrid: 'old man' })
    .limit(1);

    // The cursor will return only one record
    const all = await cursor.toArray();
    all.length === 1; // true
  • Fetches the next record from the cursor. Returns null if there are no more records to fetch.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return null.

    Returns Promise<null | T>

    The next record, or null if there are no more records.

  • Overview

    Specifies the document field to use for the reranking step. Often used with FindAndRerankCursor.rerankQuery.

    Optional if you query through the $vectorize field instead of the $vector field; otherwise required.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new setting.


    Under the hood

    Once the underlying vector and lexical searches complete, the reranker compares the rerankQuery text with each document’s rerankOn field.

    The reserved $lexical field is often used for this parameter, but you can specify any field that stores a string.

    Any document lacking the field is excluded.


    Parameters

    • rerankOn: string

      The document field to use for the reranking step.

    Returns this

    A new cursor with the new rerankOn set.

    Example

    const cursor = await coll.findAndRerank({})
    .sort({ $hybrid: { $vector: vector([...]), $lexical: 'what is a dog?' } })
    .rerankOn('$lexical')
    .rerankQuery('I like dogs');

    for await (const res of cursor) {
    console.log(res.document);
    }
  • Overview

    Specifies the query text to use for the reranking step. Often used with FindAndRerankCursor.rerankOn.

    Optional if you query through the $vectorize field instead of the $vector field; otherwise required.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new setting.


    Under the hood

    Once the underlying vector and lexical searches complete, the reranker compares the rerankQuery text with each document’s rerankOn field.


    Parameters

    • rerankQuery: string

      The query text to use for the reranking step.

    Returns this

    A new cursor with the new rerankQuery set.

    Example

    const cursor = await coll.findAndRerank({})
    .sort({ $hybrid: { $vector: vector([...]), $lexical: 'what is a dog?' } })
    .rerankOn('$lexical')
    .rerankQuery('I like dogs');

    for await (const res of cursor) {
    console.log(res.document);
    }
  • Rewinds the cursor to its uninitialized state, clearing the buffer and any state.

    Any configuration set on the cursor will remain, but iteration will start from the beginning, sending new queries to the server, even if the resultant data was already fetched by this cursor.

    Returns void

  • Overview

    Sets the sort criteria for prioritizing records.

    This option must be set, and must contain a $hybrid key.

    Note: this method does NOT mutate the cursor; it simply returns a new, uninitialized cursor with the given new sort set.


    The $hybrid key

    The $hybrid key is a special key that specifies the query(s) to use for the underlying vector and lexical searches.

    If your collection doesn’t have vectorize enabled, you must pass separate query items for $vector and $lexical:

    • { $hybrid: { $vector: vector([...]), $lexical: 'A house on a hill' } }

    If your collection has vectorize enabled, you can query through the $vectorize field instead of the $vector field. You can also use a single search string for both the $vectorize and $lexical queries.

    • { $hybrid: { $vectorize: 'A tree in the woods', $lexical: 'A house on a hill' } }
    • { $hybrid: 'A tree in the woods' }

    Parameters

    • sort: HybridSort

      The hybrid sort criteria to use for prioritizing records.

    Returns this

    A new cursor with the new sort set.

    Example

    await collection.insertMany([
    { name: 'John', age: 30, $vectorize: 'an elder man', $lexical: 'an elder man' },
    { name: 'Jane', age: 25, $vectorize: 'a young girl', $lexical: 'a young girl' },
    ]);

    const cursor = collection.find({})
    .sort({ $hybrid: 'old man' });

    // The cursor will return records sorted by the hybrid query
    const oldest = await cursor.next();
    oldest.nane === 'John'; // true
  • Returns an array of all matching records in the cursor. The user should ensure that there is enough memory to store all records in the cursor.

    Note that there'll only be partial results if the cursor has been previously iterated over. You may use FindCursor.rewind to reset the cursor.

    If the cursor is uninitialized, it will be initialized. If the cursor is closed, this method will return an empty array.

    Returns Promise<T[]>

    An array of all records in the cursor.