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

fix $search highlight path option type #11373

Merged
merged 1 commit into from
Feb 13, 2022

Conversation

lmX2015
Copy link
Contributor

@lmX2015 lmX2015 commented Feb 9, 2022

Summary

Fix this issue #11372 by adding all the possible types to highlight path ($search operator) following MongoDB specs

Examples

Code provided in the issue is now working fine

import {Model, model, Schema} from "mongoose";

interface IUser {
    name: string;
    firstName: string;
    search(query: string): any;
}

// 2. Create a Schema corresponding to the document interface.
const schema = new Schema<IUser>({
    firstName: { type: String, required: true },
    name: { type: String, required: true },

});

schema.static("search", function(this: Model<IUser>, query: string) {
    return this.aggregate([
        {
            $search: {
                highlight: {
                       path : ["name", "firstname"], // "name" works fine
                },
                index: "searchLucene",
                text: {
                    path: ["name", "firstname"],
                    query,
                },
            },
        },
        {
            $project: {
                firstName: 1,
                highlights: { $meta: "searchHighlights" },
                name: 1,
                score: { $meta: "searchScore" },
        }},

    ]);
});
// 3. Create a Model.
const UserModel = model<IUser>("User", schema);

Note that this code requires an search index to be created on the database to be run.

@Uzlopak
Copy link
Collaborator

Uzlopak commented Feb 9, 2022

Please also add typings test for this modification

Copy link
Collaborator

@vkarpov15 vkarpov15 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks 👍

@vkarpov15 vkarpov15 added this to the 6.2.2 milestone Feb 13, 2022
@vkarpov15 vkarpov15 merged commit 7834c84 into Automattic:master Feb 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants